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