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