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 import org.apache.http.util.Args;
46
47 /**
48 * Default implementation of {@link IOEventDispatch} interface for plain
49 * (unencrypted) server-side HTTP connections.
50 *
51 * @since 4.0
52 *
53 * @deprecated (4.2) use {@link DefaultHttpServerIODispatch}
54 */
55 @Deprecated
56 @Immutable // provided injected dependencies are immutable
57 public class DefaultServerIOEventDispatch extends AbstractIODispatch<NHttpServerIOTarget> {
58
59 protected final ByteBufferAllocator allocator;
60 protected final NHttpServiceHandler handler;
61 protected final HttpParams params;
62
63 /**
64 * Creates a new instance of this class to be used for dispatching I/O event
65 * notifications to the given protocol handler.
66 *
67 * @param handler the server protocol handler.
68 * @param params HTTP parameters.
69 */
70 public DefaultServerIOEventDispatch(
71 final NHttpServiceHandler handler,
72 final HttpParams params) {
73 super();
74 Args.notNull(handler, "HTTP service handler");
75 Args.notNull(params, "HTTP parameters");
76 this.allocator = createByteBufferAllocator();
77 this.handler = handler;
78 this.params = params;
79 }
80
81 /**
82 * Creates an instance of {@link HeapByteBufferAllocator} to be used
83 * by HTTP connections for allocating {@link java.nio.ByteBuffer} objects.
84 * <p>
85 * This method can be overridden in a super class in order to provide
86 * a different implementation of the {@link ByteBufferAllocator} interface.
87 *
88 * @return byte buffer allocator.
89 */
90 protected ByteBufferAllocator createByteBufferAllocator() {
91 return HeapByteBufferAllocator.INSTANCE;
92 }
93
94 /**
95 * Creates an instance of {@link DefaultHttpRequestFactory} to be used
96 * by HTTP connections for creating {@link HttpRequest} 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 }