View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.nio;
29  
30  import java.io.IOException;
31  
32  import javax.net.ssl.SSLContext;
33  import javax.net.ssl.SSLException;
34  
35  import org.apache.http.HttpRequestFactory;
36  import org.apache.http.impl.DefaultHttpRequestFactory;
37  import org.apache.http.impl.nio.reactor.SSLIOSession;
38  import org.apache.http.impl.nio.reactor.SSLIOSessionHandler;
39  import org.apache.http.impl.nio.reactor.SSLMode;
40  import org.apache.http.nio.NHttpServerIOTarget;
41  import org.apache.http.nio.NHttpServiceHandler;
42  import org.apache.http.nio.reactor.IOEventDispatch;
43  import org.apache.http.nio.reactor.IOSession;
44  import org.apache.http.nio.util.ByteBufferAllocator;
45  import org.apache.http.nio.util.HeapByteBufferAllocator;
46  import org.apache.http.params.HttpParams;
47  import org.apache.http.protocol.ExecutionContext;
48  import org.apache.http.util.Args;
49  
50  /**
51   * Default implementation of {@link IOEventDispatch} interface for SSL
52   * (encrypted) server-side HTTP connections.
53   *
54   * @since 4.0
55   *
56   * @deprecated (4.2) use {@link org.apache.http.impl.nio.ssl.SSLServerIOEventDispatch}
57   */
58  @Deprecated
59  public class SSLServerIOEventDispatch implements IOEventDispatch {
60  
61      private static final String SSL_SESSION = "SSL_SESSION";
62  
63      protected final NHttpServiceHandler handler;
64      protected final SSLContext sslcontext;
65      protected final SSLIOSessionHandler sslHandler;
66      protected final HttpParams params;
67  
68      /**
69       * Creates a new instance of this class to be used for dispatching I/O event
70       * notifications to the given protocol handler using the given
71       * {@link SSLContext}. This I/O dispatcher will transparently handle SSL
72       * protocol aspects for HTTP connections.
73       *
74       * @param handler the server protocol handler.
75       * @param sslcontext the SSL context.
76       * @param sslHandler the SSL handler.
77       * @param params HTTP parameters.
78       */
79      public SSLServerIOEventDispatch(
80              final NHttpServiceHandler handler,
81              final SSLContext sslcontext,
82              final SSLIOSessionHandler sslHandler,
83              final HttpParams params) {
84          super();
85          Args.notNull(handler, "HTTP service handler");
86          Args.notNull(sslcontext, "SSL context");
87          Args.notNull(params, "HTTP parameters");
88          this.handler = handler;
89          this.params = params;
90          this.sslcontext = sslcontext;
91          this.sslHandler = sslHandler;
92      }
93  
94      /**
95       * Creates a new instance of this class to be used for dispatching I/O event
96       * notifications to the given protocol handler using the given
97       * {@link SSLContext}. This I/O dispatcher will transparently handle SSL
98       * protocol aspects for HTTP connections.
99       *
100      * @param handler the server protocol handler.
101      * @param sslcontext the SSL context.
102      * @param params HTTP parameters.
103      */
104     public SSLServerIOEventDispatch(
105             final NHttpServiceHandler handler,
106             final SSLContext sslcontext,
107             final HttpParams params) {
108         this(handler, sslcontext, null, params);
109     }
110 
111     /**
112      * Creates an instance of {@link HeapByteBufferAllocator} to be used
113      * by HTTP connections for allocating {@link java.nio.ByteBuffer} objects.
114      * <p>
115      * This method can be overridden in a super class in order to provide
116      * a different implementation of the {@link ByteBufferAllocator} interface.
117      *
118      * @return byte buffer allocator.
119      */
120     protected ByteBufferAllocator createByteBufferAllocator() {
121         return HeapByteBufferAllocator.INSTANCE;
122     }
123 
124     /**
125      * Creates an instance of {@link DefaultHttpRequestFactory} to be used
126      * by HTTP connections for creating {@link org.apache.http.HttpRequest}
127      * objects.
128      * <p>
129      * This method can be overridden in a super class in order to provide
130      * a different implementation of the {@link HttpRequestFactory} interface.
131      *
132      * @return HTTP request factory.
133      */
134     protected HttpRequestFactory createHttpRequestFactory() {
135         return DefaultHttpRequestFactory.INSTANCE;
136     }
137 
138     /**
139      * Creates an instance of {@link DefaultNHttpServerConnection} based on the
140      * given {@link IOSession}.
141      * <p>
142      * This method can be overridden in a super class in order to provide
143      * a different implementation of the {@link NHttpServerIOTarget} interface.
144      *
145      * @param session the underlying SSL I/O session.
146      *
147      * @return newly created HTTP connection.
148      */
149     protected NHttpServerIOTarget createConnection(final IOSession session) {
150         return new DefaultNHttpServerConnection(
151                 session,
152                 createHttpRequestFactory(),
153                 createByteBufferAllocator(),
154                 this.params);
155     }
156 
157     /**
158      * Creates an instance of {@link SSLIOSession} decorating the given
159      * {@link IOSession}.
160      * <p>
161      * This method can be overridden in a super class in order to provide
162      * a different implementation of SSL I/O session.
163      *
164      * @param session the underlying I/O session.
165      * @param sslcontext the SSL context.
166      * @param sslHandler the SSL handler.
167      * @return newly created SSL I/O session.
168      */
169     protected SSLIOSession createSSLIOSession(
170             final IOSession session,
171             final SSLContext sslcontext,
172             final SSLIOSessionHandler sslHandler) {
173         return new SSLIOSession(session, sslcontext, sslHandler);
174     }
175 
176     public void connected(final IOSession session) {
177 
178         final SSLIOSession sslSession = createSSLIOSession(
179                 session,
180                 this.sslcontext,
181                 this.sslHandler);
182 
183         final NHttpServerIOTarget conn = createConnection(
184                 sslSession);
185 
186         session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
187         session.setAttribute(SSL_SESSION, sslSession);
188 
189         this.handler.connected(conn);
190 
191         try {
192             sslSession.bind(SSLMode.SERVER, this.params);
193         } catch (final SSLException ex) {
194             this.handler.exception(conn, ex);
195             sslSession.shutdown();
196         }
197     }
198 
199     public void disconnected(final IOSession session) {
200         final NHttpServerIOTarget conn =
201             (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
202 
203         if (conn != null) {
204             this.handler.closed(conn);
205         }
206     }
207 
208     public void inputReady(final IOSession session) {
209         final NHttpServerIOTarget conn =
210             (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
211         final SSLIOSession sslSession =
212             (SSLIOSession) session.getAttribute(SSL_SESSION);
213 
214         try {
215             if (sslSession.isAppInputReady()) {
216                 conn.consumeInput(this.handler);
217             }
218             sslSession.inboundTransport();
219         } catch (final IOException ex) {
220             this.handler.exception(conn, ex);
221             sslSession.shutdown();
222         }
223     }
224 
225     public void outputReady(final IOSession session) {
226         final NHttpServerIOTarget conn =
227             (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
228         final SSLIOSession sslSession =
229             (SSLIOSession) session.getAttribute(SSL_SESSION);
230 
231         try {
232             if (sslSession.isAppOutputReady()) {
233                 conn.produceOutput(this.handler);
234             }
235             sslSession.outboundTransport();
236         } catch (final IOException ex) {
237             this.handler.exception(conn, ex);
238             sslSession.shutdown();
239         }
240     }
241 
242     public void timeout(final IOSession session) {
243         final NHttpServerIOTarget conn =
244             (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
245         final SSLIOSession sslSession =
246             (SSLIOSession) session.getAttribute(SSL_SESSION);
247 
248         this.handler.timeout(conn);
249         synchronized (sslSession) {
250             if (sslSession.isOutboundDone() && !sslSession.isInboundDone()) {
251                 // The session failed to cleanly terminate
252                 sslSession.shutdown();
253             }
254         }
255     }
256 
257 }