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.HttpResponse;
33 import org.apache.http.HttpResponseFactory;
34 import org.apache.http.annotation.Immutable;
35 import org.apache.http.impl.DefaultHttpResponseFactory;
36 import org.apache.http.impl.nio.reactor.AbstractIODispatch;
37 import org.apache.http.nio.NHttpClientIOTarget;
38 import org.apache.http.nio.NHttpClientHandler;
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) client-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 DefaultHttpClientIODispatch}
63 */
64 @Deprecated
65 @Immutable // provided injected dependencies are immutable
66 public class DefaultClientIOEventDispatch extends AbstractIODispatch<NHttpClientIOTarget> {
67
68 protected final NHttpClientHandler handler;
69 protected final ByteBufferAllocator allocator;
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 client protocol handler.
77 * @param params HTTP parameters.
78 */
79 public DefaultClientIOEventDispatch(
80 final NHttpClientHandler handler,
81 final HttpParams params) {
82 super();
83 if (handler == null) {
84 throw new IllegalArgumentException("HTTP client 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 DefaultHttpResponseFactory} to be used
109 * by HTTP connections for creating {@link HttpResponse} objects.
110 * <p>
111 * This method can be overridden in a super class in order to provide
112 * a different implementation of the {@link HttpResponseFactory} interface.
113 *
114 * @return HTTP response factory.
115 */
116 protected HttpResponseFactory createHttpResponseFactory() {
117 return new DefaultHttpResponseFactory();
118 }
119
120 /**
121 * Creates an instance of {@link DefaultNHttpClientConnection} 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 NHttpClientIOTarget} interface.
126 *
127 * @param session the underlying I/O session.
128 *
129 * @return newly created HTTP connection.
130 */
131 @Override
132 protected NHttpClientIOTarget createConnection(final IOSession session) {
133 return new DefaultNHttpClientConnection(
134 session,
135 createHttpResponseFactory(),
136 this.allocator,
137 this.params);
138 }
139
140 @Override
141 protected void onConnected(final NHttpClientIOTarget conn) {
142 int timeout = HttpConnectionParams.getSoTimeout(this.params);
143 conn.setSocketTimeout(timeout);
144
145 Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
146 this.handler.connected(conn, attachment);
147 }
148
149 @Override
150 protected void onClosed(final NHttpClientIOTarget conn) {
151 this.handler.closed(conn);
152 }
153
154 @Override
155 protected void onException(final NHttpClientIOTarget conn, IOException ex) {
156 this.handler.exception(conn, ex);
157 }
158
159 @Override
160 protected void onInputReady(final NHttpClientIOTarget conn) {
161 conn.consumeInput(this.handler);
162 }
163
164 @Override
165 protected void onOutputReady(final NHttpClientIOTarget conn) {
166 conn.produceOutput(this.handler);
167 }
168
169 @Override
170 protected void onTimeout(final NHttpClientIOTarget conn) {
171 this.handler.timeout(conn);
172 }
173
174 }