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