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.NHttpClientHandler;
38 import org.apache.http.nio.NHttpClientIOTarget;
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) client-side HTTP connections.
50 *
51 * @since 4.0
52 *
53 * @deprecated (4.2) use {@link DefaultHttpClientIODispatch}
54 */
55 @Deprecated
56 @Immutable // provided injected dependencies are immutable
57 public class DefaultClientIOEventDispatch extends AbstractIODispatch<NHttpClientIOTarget> {
58
59 protected final NHttpClientHandler handler;
60 protected final ByteBufferAllocator allocator;
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 client protocol handler.
68 * @param params HTTP parameters.
69 */
70 public DefaultClientIOEventDispatch(
71 final NHttpClientHandler handler,
72 final HttpParams params) {
73 super();
74 Args.notNull(handler, "HTTP client 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 DefaultHttpResponseFactory} to be used
96 * by HTTP connections for creating {@link HttpResponse} objects.
97 * <p>
98 * This method can be overridden in a super class in order to provide
99 * a different implementation of the {@link HttpResponseFactory} interface.
100 *
101 * @return HTTP response factory.
102 */
103 protected HttpResponseFactory createHttpResponseFactory() {
104 return DefaultHttpResponseFactory.INSTANCE;
105 }
106
107 /**
108 * Creates an instance of {@link DefaultNHttpClientConnection} 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 NHttpClientIOTarget} interface.
113 *
114 * @param session the underlying I/O session.
115 *
116 * @return newly created HTTP connection.
117 */
118 @Override
119 protected NHttpClientIOTarget createConnection(final IOSession session) {
120 return new DefaultNHttpClientConnection(
121 session,
122 createHttpResponseFactory(),
123 this.allocator,
124 this.params);
125 }
126
127 @Override
128 protected void onConnected(final NHttpClientIOTarget conn) {
129 final int timeout = HttpConnectionParams.getSoTimeout(this.params);
130 conn.setSocketTimeout(timeout);
131
132 final Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
133 this.handler.connected(conn, attachment);
134 }
135
136 @Override
137 protected void onClosed(final NHttpClientIOTarget conn) {
138 this.handler.closed(conn);
139 }
140
141 @Override
142 protected void onException(final NHttpClientIOTarget conn, final IOException ex) {
143 this.handler.exception(conn, ex);
144 }
145
146 @Override
147 protected void onInputReady(final NHttpClientIOTarget conn) {
148 conn.consumeInput(this.handler);
149 }
150
151 @Override
152 protected void onOutputReady(final NHttpClientIOTarget conn) {
153 conn.produceOutput(this.handler);
154 }
155
156 @Override
157 protected void onTimeout(final NHttpClientIOTarget conn) {
158 this.handler.timeout(conn);
159 }
160
161 }