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