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 org.apache.http.HttpRequestFactory;
33  import org.apache.http.annotation.Immutable;
34  import org.apache.http.impl.DefaultHttpRequestFactory;
35  import org.apache.http.impl.nio.reactor.AbstractIODispatch;
36  import org.apache.http.nio.NHttpServerIOTarget;
37  import org.apache.http.nio.NHttpServiceHandler;
38  import org.apache.http.nio.reactor.IOSession;
39  import org.apache.http.nio.util.ByteBufferAllocator;
40  import org.apache.http.nio.util.HeapByteBufferAllocator;
41  import org.apache.http.params.HttpConnectionParams;
42  import org.apache.http.params.HttpParams;
43  import org.apache.http.util.Args;
44  
45  /**
46   * Default implementation of {@link org.apache.http.nio.reactor.IOEventDispatch}
47   * interface for plain (unencrypted) server-side HTTP connections.
48   *
49   * @since 4.0
50   *
51   * @deprecated (4.2) use {@link DefaultHttpServerIODispatch}
52   */
53  @Deprecated
54  @Immutable // provided injected dependencies are immutable
55  public class DefaultServerIOEventDispatch extends AbstractIODispatch<NHttpServerIOTarget> {
56  
57      protected final ByteBufferAllocator allocator;
58      protected final NHttpServiceHandler handler;
59      protected final HttpParams params;
60  
61      /**
62       * Creates a new instance of this class to be used for dispatching I/O event
63       * notifications to the given protocol handler.
64       *
65       * @param handler the server protocol handler.
66       * @param params HTTP parameters.
67       */
68       public DefaultServerIOEventDispatch(
69              final NHttpServiceHandler handler,
70              final HttpParams params) {
71          super();
72          Args.notNull(handler, "HTTP service handler");
73          Args.notNull(params, "HTTP parameters");
74          this.allocator = createByteBufferAllocator();
75          this.handler = handler;
76          this.params = params;
77      }
78  
79     /**
80      * Creates an instance of {@link HeapByteBufferAllocator} to be used
81      * by HTTP connections for allocating {@link java.nio.ByteBuffer} objects.
82      * <p>
83      * This method can be overridden in a super class in order to provide
84      * a different implementation of the {@link ByteBufferAllocator} interface.
85      *
86      * @return byte buffer allocator.
87      */
88      protected ByteBufferAllocator createByteBufferAllocator() {
89          return HeapByteBufferAllocator.INSTANCE;
90      }
91  
92      /**
93       * Creates an instance of {@link DefaultHttpRequestFactory} to be used
94       * by HTTP connections for creating {@link org.apache.http.HttpRequest}
95       * objects.
96       * <p>
97       * This method can be overridden in a super class in order to provide
98       * a different implementation of the {@link HttpRequestFactory} interface.
99       *
100      * @return HTTP request factory.
101      */
102     protected HttpRequestFactory createHttpRequestFactory() {
103         return DefaultHttpRequestFactory.INSTANCE;
104     }
105 
106     /**
107      * Creates an instance of {@link DefaultNHttpServerConnection} based on the
108      * given {@link IOSession}.
109      * <p>
110      * This method can be overridden in a super class in order to provide
111      * a different implementation of the {@link NHttpServerIOTarget} interface.
112      *
113      * @param session the underlying I/O session.
114      *
115      * @return newly created HTTP connection.
116      */
117     @Override
118     protected NHttpServerIOTarget createConnection(final IOSession session) {
119         return new DefaultNHttpServerConnection(
120                 session,
121                 createHttpRequestFactory(),
122                 this.allocator,
123                 this.params);
124     }
125 
126     @Override
127     protected void onConnected(final NHttpServerIOTarget conn) {
128         final int timeout = HttpConnectionParams.getSoTimeout(this.params);
129         conn.setSocketTimeout(timeout);
130         this.handler.connected(conn);
131     }
132 
133     @Override
134     protected void onClosed(final NHttpServerIOTarget conn) {
135         this.handler.closed(conn);
136     }
137 
138     @Override
139     protected void onException(final NHttpServerIOTarget conn, final IOException ex) {
140         this.handler.exception(conn, ex);
141     }
142 
143     @Override
144     protected void onInputReady(final NHttpServerIOTarget conn) {
145         conn.consumeInput(this.handler);
146     }
147 
148     @Override
149     protected void onOutputReady(final NHttpServerIOTarget conn) {
150         conn.produceOutput(this.handler);
151     }
152 
153     @Override
154     protected void onTimeout(final NHttpServerIOTarget conn) {
155         this.handler.timeout(conn);
156     }
157 
158 }