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.ssl;
29  
30  import javax.net.ssl.SSLContext;
31  import javax.net.ssl.SSLException;
32  
33  import org.apache.http.impl.nio.DefaultServerIOEventDispatch;
34  import org.apache.http.impl.nio.reactor.SSLIOSession;
35  import org.apache.http.impl.nio.reactor.SSLSetupHandler;
36  import org.apache.http.nio.NHttpServerIOTarget;
37  import org.apache.http.nio.NHttpServiceHandler;
38  import org.apache.http.nio.reactor.IOSession;
39  import org.apache.http.params.HttpConnectionParams;
40  import org.apache.http.params.HttpParams;
41  import org.apache.http.util.Args;
42  
43  /**
44   * Default implementation of {@link org.apache.http.nio.reactor.IOEventDispatch}
45   * interface for SSL (encrypted) server-side HTTP connections.
46   *
47   * @since 4.1
48   *
49   * @deprecated (4.2) use {@link org.apache.http.impl.nio.DefaultHttpServerIODispatch}
50   */
51  @Deprecated
52  public class SSLServerIOEventDispatch extends DefaultServerIOEventDispatch {
53  
54      private final SSLContext sslContext;
55      private final SSLSetupHandler sslHandler;
56  
57      /**
58       * Creates a new instance of this class to be used for dispatching I/O event
59       * notifications to the given protocol handler using the given
60       * {@link SSLContext}. This I/O dispatcher will transparently handle SSL
61       * protocol aspects for HTTP connections.
62       *
63       * @param handler the server protocol handler.
64       * @param sslContext the SSL context.
65       * @param sslHandler the SSL setup handler.
66       * @param params HTTP parameters.
67       */
68      public SSLServerIOEventDispatch(
69              final NHttpServiceHandler handler,
70              final SSLContext sslContext,
71              final SSLSetupHandler sslHandler,
72              final HttpParams params) {
73          super(handler, params);
74          Args.notNull(sslContext, "SSL context");
75          Args.notNull(params, "HTTP parameters");
76          this.sslContext = sslContext;
77          this.sslHandler = sslHandler;
78      }
79  
80      /**
81       * Creates a new instance of this class to be used for dispatching I/O event
82       * notifications to the given protocol handler using the given
83       * {@link SSLContext}. This I/O dispatcher will transparently handle SSL
84       * protocol aspects for HTTP connections.
85       *
86       * @param handler the server protocol handler.
87       * @param sslContext the SSL context.
88       * @param params HTTP parameters.
89       */
90      public SSLServerIOEventDispatch(
91              final NHttpServiceHandler handler,
92              final SSLContext sslContext,
93              final HttpParams params) {
94          this(handler, sslContext, null, params);
95      }
96  
97      /**
98       * Creates an instance of {@link SSLIOSession} decorating the given
99       * {@link IOSession}.
100      * <p>
101      * This method can be overridden in a super class in order to provide
102      * a different implementation of SSL I/O session.
103      *
104      * @param session the underlying I/O session.
105      * @param sslContext the SSL context.
106      * @param sslHandler the SSL setup handler.
107      * @return newly created SSL I/O session.
108      */
109     protected SSLIOSession createSSLIOSession(
110             final IOSession session,
111             final SSLContext sslContext,
112             final SSLSetupHandler sslHandler) {
113         return new SSLIOSession(session, sslContext, sslHandler);
114     }
115 
116     protected NHttpServerIOTarget createSSLConnection(final SSLIOSession sslioSession) {
117         return super.createConnection(sslioSession);
118     }
119 
120     @Override
121     protected NHttpServerIOTarget createConnection(final IOSession session) {
122         final SSLIOSession sslioSession = createSSLIOSession(session, this.sslContext, this.sslHandler);
123         session.setAttribute(SSLIOSession.SESSION_KEY, sslioSession);
124         final NHttpServerIOTarget conn = createSSLConnection(sslioSession);
125         try {
126             sslioSession.initialize();
127         } catch (final SSLException ex) {
128             this.handler.exception(conn, ex);
129             sslioSession.shutdown();
130         }
131         return conn;
132     }
133 
134     @Override
135     public void onConnected(final NHttpServerIOTarget conn) {
136         final int timeout = HttpConnectionParams.getSoTimeout(this.params);
137         conn.setSocketTimeout(timeout);
138         this.handler.connected(conn);
139     }
140 
141 }