2 // ========================================================================
3 // Copyright (c) 1995-2016 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.nio.ByteBuffer;
23 import java.util.List;
25 import javax.net.ssl.SSLEngine;
26 import javax.net.ssl.SSLEngineResult;
28 import org.eclipse.jetty.io.AbstractConnection;
29 import org.eclipse.jetty.io.Connection;
30 import org.eclipse.jetty.io.EndPoint;
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 if (LOG.isDebugEnabled())
99 LOG.debug("{} could not negotiate protocol, SSLEngine: {}", this, engine);
104 // Here the SSL handshake is not finished yet but we filled 0 bytes,
105 // so we need to read more.
111 ConnectionFactory connectionFactory = connector.getConnectionFactory(protocol);
112 if (connectionFactory == null)
114 if (LOG.isDebugEnabled())
115 LOG.debug("{} application selected protocol '{}', but no correspondent {} has been configured",
116 this, protocol, ConnectionFactory.class.getName());
121 EndPoint endPoint = getEndPoint();
122 Connection newConnection = connectionFactory.newConnection(connector, endPoint);
123 endPoint.upgrade(newConnection);
129 // Something went bad, we need to close.
130 if (LOG.isDebugEnabled())
131 LOG.debug("{} closing on client close", this);
136 // Must never happen, since we fill using an empty buffer
137 throw new IllegalStateException();
145 return getEndPoint().fill(BufferUtil.EMPTY_BUFFER);
147 catch (IOException x)
158 // Gentler close for SSL.
159 getEndPoint().shutdownOutput();