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   *
50   * @since 4.2
51   */
52  @SuppressWarnings("deprecation")
53  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
54  public class DefaultHttpServerIODispatch
55                      extends AbstractIODispatch<DefaultNHttpServerConnection> {
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.
60       *
61       * @param handler the client protocol handler.
62       * @param sslContext an SSLContext or null (for a plain text connection.)
63       * @param config a connection configuration
64       * @return a new instance
65       * @since 4.4.7
66       */
67      public static DefaultHttpServerIODispatch create(final NHttpServerEventHandler handler,
68              final SSLContext sslContext,
69              final ConnectionConfig config) {
70          return sslContext == null ? new DefaultHttpServerIODispatch(handler, config)
71                  : new DefaultHttpServerIODispatch(handler, sslContext, config);
72      }
73  
74      /**
75       * Creates a new instance of this class to be used for dispatching I/O event
76       * notifications to the given protocol handler.
77       *
78       * @param handler the client protocol handler.
79       * @param sslContext an SSLContext or null (for a plain text connection.)
80       * @param sslHandler customizes various aspects of the TLS/SSL protocol.
81       * @param config a connection configuration
82       * @return a new instance
83       * @since 4.4.7
84       */
85      public static DefaultHttpServerIODispatch create(final NHttpServerEventHandler handler,
86              final SSLContext sslContext,
87              final SSLSetupHandler sslHandler,
88              final ConnectionConfig config) {
89          return sslContext == null ? new DefaultHttpServerIODispatch(handler, config)
90                  : new DefaultHttpServerIODispatch(handler, sslContext, sslHandler, config);
91      }
92  
93      private final NHttpServerEventHandler handler;
94      private final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connFactory;
95  
96      public DefaultHttpServerIODispatch(
97              final NHttpServerEventHandler handler,
98              final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connFactory) {
99          super();
100         this.handler = Args.notNull(handler, "HTTP client handler");
101         this.connFactory = Args.notNull(connFactory, "HTTP server connection factory");
102     }
103 
104     /**
105      * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
106      *   NHttpServerEventHandler, ConnectionConfig)}
107      */
108     @Deprecated
109     public DefaultHttpServerIODispatch(
110             final NHttpServerEventHandler handler,
111             final HttpParams params) {
112         this(handler, new DefaultNHttpServerConnectionFactory(params));
113     }
114 
115     /**
116      * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
117      *   NHttpServerEventHandler, SSLContext, SSLSetupHandler, ConnectionConfig)}
118      */
119     @Deprecated
120     public DefaultHttpServerIODispatch(
121             final NHttpServerEventHandler handler,
122             final SSLContext sslContext,
123             final SSLSetupHandler sslHandler,
124             final HttpParams params) {
125         this(handler, new SSLNHttpServerConnectionFactory(sslContext, sslHandler, params));
126     }
127 
128     /**
129      * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
130      *   NHttpServerEventHandler, SSLContext, ConnectionConfig)}
131      */
132     @Deprecated
133     public DefaultHttpServerIODispatch(
134             final NHttpServerEventHandler handler,
135             final SSLContext sslContext,
136             final HttpParams params) {
137         this(handler, sslContext, null, params);
138     }
139 
140     /**
141      * @since 4.3
142      */
143     public DefaultHttpServerIODispatch(final NHttpServerEventHandler handler, final ConnectionConfig config) {
144         this(handler, new DefaultNHttpServerConnectionFactory(config));
145     }
146 
147     /**
148      * @since 4.3
149      */
150     public DefaultHttpServerIODispatch(
151             final NHttpServerEventHandler handler,
152             final SSLContext sslContext,
153             final SSLSetupHandler sslHandler,
154             final ConnectionConfig config) {
155         this(handler, new SSLNHttpServerConnectionFactory(sslContext, sslHandler, config));
156     }
157 
158     /**
159      * @since 4.3
160      */
161     public DefaultHttpServerIODispatch(
162             final NHttpServerEventHandler handler,
163             final SSLContext sslContext,
164             final ConnectionConfig config) {
165         this(handler, new SSLNHttpServerConnectionFactory(sslContext, null, config));
166     }
167 
168     @Override
169     protected DefaultNHttpServerConnection createConnection(final IOSession session) {
170         return this.connFactory.createConnection(session);
171     }
172 
173     @Override
174     protected void onConnected(final DefaultNHttpServerConnection conn) {
175         try {
176             this.handler.connected(conn);
177         } catch (final Exception ex) {
178             this.handler.exception(conn, ex);
179         }
180     }
181 
182     @Override
183     protected void onClosed(final DefaultNHttpServerConnection conn) {
184         this.handler.closed(conn);
185     }
186 
187     @Override
188     protected void onException(final DefaultNHttpServerConnection conn, final IOException ex) {
189         this.handler.exception(conn, ex);
190     }
191 
192     @Override
193     protected void onInputReady(final DefaultNHttpServerConnection conn) {
194         conn.consumeInput(this.handler);
195     }
196 
197     @Override
198     protected void onOutputReady(final DefaultNHttpServerConnection conn) {
199         conn.produceOutput(this.handler);
200     }
201 
202     @Override
203     protected void onTimeout(final DefaultNHttpServerConnection conn) {
204         try {
205             this.handler.timeout(conn);
206         } catch (final Exception ex) {
207             this.handler.exception(conn, ex);
208         }
209     }
210 
211 }