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.impl.nio.reactor.AbstractIODispatch;
36  import org.apache.http.nio.NHttpConnectionFactory;
37  import org.apache.http.nio.NHttpServerEventHandler;
38  import org.apache.http.nio.reactor.IOEventDispatch;
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  
43  /**
44   * Default {@link IOEventDispatch} implementation that supports both plain (non-encrypted)
45   * and SSL encrypted server side HTTP connections.
46   *
47   * @since 4.2
48   */
49  @Immutable // provided injected dependencies are immutable
50  public class DefaultHttpServerIODispatch
51                      extends AbstractIODispatch<DefaultNHttpServerConnection> {
52  
53      private final NHttpServerEventHandler handler;
54      private final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
55  
56      public DefaultHttpServerIODispatch(
57              final NHttpServerEventHandler handler,
58              final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory) {
59          super();
60          if (handler == null) {
61              throw new IllegalArgumentException("HTTP client handler may not be null");
62          }
63          if (connFactory == null) {
64              throw new IllegalArgumentException("HTTP server connection factory is null");
65          }
66          this.handler = handler;
67          this.connFactory = connFactory;
68      }
69  
70      public DefaultHttpServerIODispatch(
71              final NHttpServerEventHandler handler,
72              final HttpParams params) {
73          this(handler, new DefaultNHttpServerConnectionFactory(params));
74      }
75  
76      public DefaultHttpServerIODispatch(
77              final NHttpServerEventHandler handler,
78              final SSLContext sslcontext,
79              final SSLSetupHandler sslHandler,
80              final HttpParams params) {
81          this(handler, new SSLNHttpServerConnectionFactory(sslcontext, sslHandler, params));
82      }
83  
84      public DefaultHttpServerIODispatch(
85              final NHttpServerEventHandler handler,
86              final SSLContext sslcontext,
87              final HttpParams params) {
88          this(handler, sslcontext, null, params);
89      }
90  
91      @Override
92      protected DefaultNHttpServerConnection createConnection(final IOSession session) {
93          return this.connFactory.createConnection(session);
94      }
95  
96      @Override
97      protected void onConnected(final DefaultNHttpServerConnection conn) {
98          try {
99              this.handler.connected(conn);
100         } catch (Exception ex) {
101             this.handler.exception(conn, ex);
102         }
103     }
104 
105     @Override
106     protected void onClosed(final DefaultNHttpServerConnection conn) {
107         this.handler.closed(conn);
108     }
109 
110     @Override
111     protected void onException(final DefaultNHttpServerConnection conn, IOException ex) {
112         this.handler.exception(conn, ex);
113     }
114 
115     @Override
116     protected void onInputReady(final DefaultNHttpServerConnection conn) {
117         conn.consumeInput(this.handler);
118     }
119 
120     @Override
121     protected void onOutputReady(final DefaultNHttpServerConnection conn) {
122         conn.produceOutput(this.handler);
123     }
124 
125     @Override
126     protected void onTimeout(final DefaultNHttpServerConnection conn) {
127         try {
128             this.handler.timeout(conn);
129         } catch (Exception ex) {
130             this.handler.exception(conn, ex);
131         }
132     }
133 
134 }