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  
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.annotation.ThreadingBehavior;
36  import org.apache.http.config.ConnectionConfig;
37  import org.apache.http.impl.nio.reactor.AbstractIODispatch;
38  import org.apache.http.nio.NHttpConnectionFactory;
39  import org.apache.http.nio.NHttpServerEventHandler;
40  import org.apache.http.nio.reactor.IOSession;
41  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
42  import org.apache.http.params.HttpParams;
43  import org.apache.http.util.Args;
44  
45  /**
46   * Default {@link org.apache.http.nio.reactor.IOEventDispatch} implementation
47   * that supports both plain (non-encrypted) and SSL encrypted server side HTTP
48   * connections.
49   * @param <H> an implementation of {@link NHttpServerEventHandler}.
50   *
51   * @since 4.2
52   */
53  @SuppressWarnings("deprecation")
54  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
55  public class DefaultHttpServerIODispatch<H extends NHttpServerEventHandler>
56                      extends AbstractIODispatch<DefaultNHttpServerConnection> {
57  
58      /**
59       * Creates a new instance of this class to be used for dispatching I/O event
60       * notifications to the given protocol handler.
61       *
62       * @param handler the client protocol handler.
63       * @param sslContext an SSLContext or null (for a plain text connection.)
64       * @param config a connection configuration
65       * @return a new instance
66       * @since 4.4.7
67       */
68      public static <T extends NHttpServerEventHandler> DefaultHttpServerIODispatch<T> create(final T handler,
69              final SSLContext sslContext,
70              final ConnectionConfig config) {
71          return sslContext == null ? new DefaultHttpServerIODispatch<T>(handler, config)
72                  : new DefaultHttpServerIODispatch<T>(handler, sslContext, config);
73      }
74  
75      /**
76       * Creates a new instance of this class to be used for dispatching I/O event
77       * notifications to the given protocol handler.
78       *
79       * @param handler the client protocol handler.
80       * @param sslContext an SSLContext or null (for a plain text connection.)
81       * @param sslHandler customizes various aspects of the TLS/SSL protocol.
82       * @param config a connection configuration
83       * @return a new instance
84       * @since 4.4.7
85       */
86      public static <T extends NHttpServerEventHandler> DefaultHttpServerIODispatch<T> create(final T handler,
87              final SSLContext sslContext,
88              final SSLSetupHandler sslHandler,
89              final ConnectionConfig config) {
90          return sslContext == null ? new DefaultHttpServerIODispatch<T>(handler, config)
91                  : new DefaultHttpServerIODispatch<T>(handler, sslContext, sslHandler, config);
92      }
93  
94      private final H handler;
95      private final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connectionFactory;
96  
97      public DefaultHttpServerIODispatch(
98              final H handler,
99              final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connFactory) {
100         super();
101         this.handler = Args.notNull(handler, "HTTP server handler");
102         this.connectionFactory = Args.notNull(connFactory, "HTTP server connection factory");
103     }
104 
105     /**
106      * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
107      *   NHttpServerEventHandler, ConnectionConfig)}
108      */
109     @Deprecated
110     public DefaultHttpServerIODispatch(
111             final H handler,
112             final HttpParams params) {
113         this(handler, new DefaultNHttpServerConnectionFactory(params));
114     }
115 
116     /**
117      * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
118      *   NHttpServerEventHandler, SSLContext, SSLSetupHandler, ConnectionConfig)}
119      */
120     @Deprecated
121     public DefaultHttpServerIODispatch(
122             final H handler,
123             final SSLContext sslContext,
124             final SSLSetupHandler sslHandler,
125             final HttpParams params) {
126         this(handler, new SSLNHttpServerConnectionFactory(sslContext, sslHandler, params));
127     }
128 
129     /**
130      * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
131      *   NHttpServerEventHandler, SSLContext, ConnectionConfig)}
132      */
133     @Deprecated
134     public DefaultHttpServerIODispatch(
135             final H handler,
136             final SSLContext sslContext,
137             final HttpParams params) {
138         this(handler, sslContext, null, params);
139     }
140 
141     /**
142      * @since 4.3
143      */
144     public DefaultHttpServerIODispatch(final H handler, final ConnectionConfig config) {
145         this(handler, new DefaultNHttpServerConnectionFactory(config));
146     }
147 
148     /**
149      * @since 4.3
150      */
151     public DefaultHttpServerIODispatch(
152             final H handler,
153             final SSLContext sslContext,
154             final SSLSetupHandler sslHandler,
155             final ConnectionConfig config) {
156         this(handler, new SSLNHttpServerConnectionFactory(sslContext, sslHandler, config));
157     }
158 
159     /**
160      * @since 4.3
161      */
162     public DefaultHttpServerIODispatch(
163             final H handler,
164             final SSLContext sslContext,
165             final ConnectionConfig config) {
166         this(handler, new SSLNHttpServerConnectionFactory(sslContext, null, config));
167     }
168 
169     @Override
170     protected DefaultNHttpServerConnection createConnection(final IOSession session) {
171         return this.connectionFactory.createConnection(session);
172     }
173 
174     /**
175      * Gets the connection factory used to construct this dispatch.
176      *
177      * @return the connection factory used to construct this dispatch.
178      * @since 4.4.9
179      */
180     public NHttpConnectionFactory<? extends DefaultNHttpServerConnection> getConnectionFactory() {
181         return connectionFactory;
182     }
183 
184     /**
185      * Gets the handler used to construct this dispatch.
186      *
187      * @return the handler used to construct this dispatch.
188      * @since 4.4.9
189      */
190     public H getHandler() {
191         return handler;
192     }
193 
194     @Override
195     protected void onConnected(final DefaultNHttpServerConnection conn) {
196         try {
197             this.handler.connected(conn);
198         } catch (final Exception ex) {
199             this.handler.exception(conn, ex);
200         }
201     }
202 
203     @Override
204     protected void onClosed(final DefaultNHttpServerConnection conn) {
205         this.handler.closed(conn);
206     }
207 
208     @Override
209     protected void onException(final DefaultNHttpServerConnection conn, final IOException ex) {
210         this.handler.exception(conn, ex);
211     }
212 
213     @Override
214     protected void onInputReady(final DefaultNHttpServerConnection conn) {
215         conn.consumeInput(this.handler);
216     }
217 
218     @Override
219     protected void onOutputReady(final DefaultNHttpServerConnection conn) {
220         conn.produceOutput(this.handler);
221     }
222 
223     @Override
224     protected void onTimeout(final DefaultNHttpServerConnection conn) {
225         try {
226             this.handler.timeout(conn);
227         } catch (final Exception ex) {
228             this.handler.exception(conn, ex);
229         }
230     }
231 
232 }