2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.io.IOException;
22 import java.util.List;
23 import javax.net.ssl.SSLEngine;
24 import javax.net.ssl.SSLEngineResult;
26 import org.eclipse.jetty.io.AbstractConnection;
27 import org.eclipse.jetty.io.Connection;
28 import org.eclipse.jetty.io.EndPoint;
29 import org.eclipse.jetty.server.ConnectionFactory;
30 import org.eclipse.jetty.server.Connector;
31 import org.eclipse.jetty.util.BufferUtil;
32 import org.eclipse.jetty.util.log.Log;
33 import org.eclipse.jetty.util.log.Logger;
35 public abstract class NegotiatingServerConnection extends AbstractConnection
37 private static final Logger LOG = Log.getLogger(NegotiatingServerConnection.class);
39 private final Connector connector;
40 private final SSLEngine engine;
41 private final List<String> protocols;
42 private final String defaultProtocol;
43 private String protocol; // No need to be volatile: it is modified and read by the same thread
45 protected NegotiatingServerConnection(Connector connector, EndPoint endPoint, SSLEngine engine, List<String> protocols, String defaultProtocol)
47 super(endPoint, connector.getExecutor());
48 this.connector = connector;
49 this.protocols = protocols;
50 this.defaultProtocol = defaultProtocol;
54 protected List<String> getProtocols()
59 protected String getDefaultProtocol()
61 return defaultProtocol;
64 protected SSLEngine getSSLEngine()
69 protected String getProtocol()
74 protected void setProtocol(String protocol)
76 this.protocol = protocol;
87 public void onFillable()
95 if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING)
97 // Here the SSL handshake is finished, but the protocol has not been negotiated.
98 LOG.debug("{} could not negotiate protocol, SSLEngine: {}", this, engine);
103 // Here the SSL handshake is not finished yet but we filled 0 bytes,
104 // so we need to read more.
110 ConnectionFactory connectionFactory = connector.getConnectionFactory(protocol);
111 if (connectionFactory == null)
113 LOG.debug("{} application selected protocol '{}', but no correspondent {} has been configured",
114 this, protocol, ConnectionFactory.class.getName());
119 EndPoint endPoint = getEndPoint();
120 Connection oldConnection = endPoint.getConnection();
121 Connection newConnection = connectionFactory.newConnection(connector, endPoint);
122 LOG.debug("{} switching from {} to {}", this, oldConnection, newConnection);
123 oldConnection.onClose();
124 endPoint.setConnection(newConnection);
125 getEndPoint().getConnection().onOpen();
131 // Something went bad, we need to close.
132 LOG.debug("{} closing on client close", this);
137 // Must never happen, since we fill using an empty buffer
138 throw new IllegalStateException();
146 return getEndPoint().fill(BufferUtil.EMPTY_BUFFER);
148 catch (IOException x)
159 // Gentler close for SSL.
160 getEndPoint().shutdownOutput();