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.io;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
24 import java.util.concurrent.Executor;
26 import javax.net.ssl.SSLEngine;
28 import org.eclipse.jetty.util.BufferUtil;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.log.Logger;
32 public abstract class NegotiatingClientConnection extends AbstractConnection
34 private static final Logger LOG = Log.getLogger(NegotiatingClientConnection.class);
36 private final SSLEngine engine;
37 private final ClientConnectionFactory connectionFactory;
38 private final Map<String, Object> context;
39 private volatile boolean completed;
41 protected NegotiatingClientConnection(EndPoint endp, Executor executor, SSLEngine sslEngine, ClientConnectionFactory connectionFactory, Map<String, Object> context)
43 super(endp, executor);
44 this.engine = sslEngine;
45 this.connectionFactory = connectionFactory;
46 this.context = context;
49 protected SSLEngine getSSLEngine()
54 protected void completed()
65 getEndPoint().flush(BufferUtil.EMPTY_BUFFER);
74 throw new RuntimeIOException(x);
79 public void onFillable()
84 if (filled == 0 && !completed)
86 if (filled <= 0 || completed)
97 return getEndPoint().fill(BufferUtil.EMPTY_BUFFER);
107 private void replaceConnection()
109 EndPoint endPoint = getEndPoint();
112 Connection oldConnection = endPoint.getConnection();
113 Connection newConnection = connectionFactory.newConnection(endPoint, context);
114 ClientConnectionFactory.Helper.replaceConnection(oldConnection, newConnection);
126 // Gentler close for SSL.
127 getEndPoint().shutdownOutput();