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.Immutable;
35  import org.apache.http.config.ConnectionConfig;
36  import org.apache.http.impl.nio.reactor.AbstractIODispatch;
37  import org.apache.http.nio.NHttpConnectionFactory;
38  import org.apache.http.nio.NHttpServerEventHandler;
39  import org.apache.http.nio.reactor.IOSession;
40  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
41  import org.apache.http.params.HttpParams;
42  import org.apache.http.util.Args;
43  
44  /**
45   * Default {@link org.apache.http.nio.reactor.IOEventDispatch} implementation
46   * that supports both plain (non-encrypted) and SSL encrypted server side HTTP
47   * connections.
48   *
49   * @since 4.2
50   */
51  @SuppressWarnings("deprecation")
52  @Immutable // provided injected dependencies are immutable
53  public class DefaultHttpServerIODispatch
54                      extends AbstractIODispatch<DefaultNHttpServerConnection> {
55  
56      private final NHttpServerEventHandler handler;
57      private final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
58  
59      public DefaultHttpServerIODispatch(
60              final NHttpServerEventHandler handler,
61              final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory) {
62          super();
63          this.handler = Args.notNull(handler, "HTTP client handler");
64          this.connFactory = Args.notNull(connFactory, "HTTP server connection factory");
65      }
66  
67      /**
68       * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
69       *   NHttpServerEventHandler, ConnectionConfig)}
70       */
71      @Deprecated
72      public DefaultHttpServerIODispatch(
73              final NHttpServerEventHandler handler,
74              final HttpParams params) {
75          this(handler, new DefaultNHttpServerConnectionFactory(params));
76      }
77  
78      /**
79       * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
80       *   NHttpServerEventHandler, SSLContext, SSLSetupHandler, ConnectionConfig)}
81       */
82      @Deprecated
83      public DefaultHttpServerIODispatch(
84              final NHttpServerEventHandler handler,
85              final SSLContext sslcontext,
86              final SSLSetupHandler sslHandler,
87              final HttpParams params) {
88          this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, params));
89      }
90  
91      /**
92       * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
93       *   NHttpServerEventHandler, SSLContext, ConnectionConfig)}
94       */
95      @Deprecated
96      public DefaultHttpServerIODispatch(
97              final NHttpServerEventHandler handler,
98              final SSLContext sslcontext,
99              final HttpParams params) {
100         this(handler, sslcontext, null, params);
101     }
102 
103     /**
104      * @since 4.3
105      */
106     public DefaultHttpServerIODispatch(final NHttpServerEventHandler handler, final ConnectionConfig config) {
107         this(handler, new DefaultNHttpServerConnectionFactory(config));
108     }
109 
110     /**
111      * @since 4.3
112      */
113     public DefaultHttpServerIODispatch(
114             final NHttpServerEventHandler handler,
115             final SSLContext sslcontext,
116             final SSLSetupHandler sslHandler,
117             final ConnectionConfig config) {
118         this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, config));
119     }
120 
121     /**
122      * @since 4.3
123      */
124     public DefaultHttpServerIODispatch(
125             final NHttpServerEventHandler handler,
126             final SSLContext sslcontext,
127             final ConnectionConfig config) {
128         this(handler, new SSLNHttpServerConnectionFactory(sslcontext, null, config));
129     }
130 
131     @Override
132     protected DefaultNHttpServerConnection createConnection(final IOSession session) {
133         return this.connFactory.createConnection(session);
134     }
135 
136     @Override
137     protected void onConnected(final DefaultNHttpServerConnection conn) {
138         try {
139             this.handler.connected(conn);
140         } catch (final Exception ex) {
141             this.handler.exception(conn, ex);
142         }
143     }
144 
145     @Override
146     protected void onClosed(final DefaultNHttpServerConnection conn) {
147         this.handler.closed(conn);
148     }
149 
150     @Override
151     protected void onException(final DefaultNHttpServerConnection conn, final IOException ex) {
152         this.handler.exception(conn, ex);
153     }
154 
155     @Override
156     protected void onInputReady(final DefaultNHttpServerConnection conn) {
157         conn.consumeInput(this.handler);
158     }
159 
160     @Override
161     protected void onOutputReady(final DefaultNHttpServerConnection conn) {
162         conn.produceOutput(this.handler);
163     }
164 
165     @Override
166     protected void onTimeout(final DefaultNHttpServerConnection conn) {
167         try {
168             this.handler.timeout(conn);
169         } catch (final Exception ex) {
170             this.handler.exception(conn, ex);
171         }
172     }
173 
174 }