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.ThreadingBehavior;
35  import org.apache.http.annotation.Contract;
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      private final NHttpServerEventHandler handler;
58      private final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connFactory;
59  
60      public DefaultHttpServerIODispatch(
61              final NHttpServerEventHandler handler,
62              final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connFactory) {
63          super();
64          this.handler = Args.notNull(handler, "HTTP client handler");
65          this.connFactory = Args.notNull(connFactory, "HTTP server connection factory");
66      }
67  
68      /**
69       * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
70       *   NHttpServerEventHandler, ConnectionConfig)}
71       */
72      @Deprecated
73      public DefaultHttpServerIODispatch(
74              final NHttpServerEventHandler handler,
75              final HttpParams params) {
76          this(handler, new DefaultNHttpServerConnectionFactory(params));
77      }
78  
79      /**
80       * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
81       *   NHttpServerEventHandler, SSLContext, SSLSetupHandler, ConnectionConfig)}
82       */
83      @Deprecated
84      public DefaultHttpServerIODispatch(
85              final NHttpServerEventHandler handler,
86              final SSLContext sslcontext,
87              final SSLSetupHandler sslHandler,
88              final HttpParams params) {
89          this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, params));
90      }
91  
92      /**
93       * @deprecated (4.3) use {@link DefaultHttpServerIODispatch#DefaultHttpServerIODispatch(
94       *   NHttpServerEventHandler, SSLContext, ConnectionConfig)}
95       */
96      @Deprecated
97      public DefaultHttpServerIODispatch(
98              final NHttpServerEventHandler handler,
99              final SSLContext sslcontext,
100             final HttpParams params) {
101         this(handler, sslcontext, null, params);
102     }
103 
104     /**
105      * @since 4.3
106      */
107     public DefaultHttpServerIODispatch(final NHttpServerEventHandler handler, final ConnectionConfig config) {
108         this(handler, new DefaultNHttpServerConnectionFactory(config));
109     }
110 
111     /**
112      * @since 4.3
113      */
114     public DefaultHttpServerIODispatch(
115             final NHttpServerEventHandler handler,
116             final SSLContext sslcontext,
117             final SSLSetupHandler sslHandler,
118             final ConnectionConfig config) {
119         this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, config));
120     }
121 
122     /**
123      * @since 4.3
124      */
125     public DefaultHttpServerIODispatch(
126             final NHttpServerEventHandler handler,
127             final SSLContext sslcontext,
128             final ConnectionConfig config) {
129         this(handler, new SSLNHttpServerConnectionFactory(sslcontext, null, config));
130     }
131 
132     @Override
133     protected DefaultNHttpServerConnection createConnection(final IOSession session) {
134         return this.connFactory.createConnection(session);
135     }
136 
137     @Override
138     protected void onConnected(final DefaultNHttpServerConnection conn) {
139         try {
140             this.handler.connected(conn);
141         } catch (final Exception ex) {
142             this.handler.exception(conn, ex);
143         }
144     }
145 
146     @Override
147     protected void onClosed(final DefaultNHttpServerConnection conn) {
148         this.handler.closed(conn);
149     }
150 
151     @Override
152     protected void onException(final DefaultNHttpServerConnection conn, final IOException ex) {
153         this.handler.exception(conn, ex);
154     }
155 
156     @Override
157     protected void onInputReady(final DefaultNHttpServerConnection conn) {
158         conn.consumeInput(this.handler);
159     }
160 
161     @Override
162     protected void onOutputReady(final DefaultNHttpServerConnection conn) {
163         conn.produceOutput(this.handler);
164     }
165 
166     @Override
167     protected void onTimeout(final DefaultNHttpServerConnection conn) {
168         try {
169             this.handler.timeout(conn);
170         } catch (final Exception ex) {
171             this.handler.exception(conn, ex);
172         }
173     }
174 
175 }