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  package org.apache.http.impl.nio.client;
28  
29  import java.util.List;
30  import java.util.concurrent.Future;
31  import java.util.concurrent.ThreadFactory;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.http.ConnectionReuseStrategy;
36  import org.apache.http.HttpHost;
37  import org.apache.http.auth.AuthSchemeProvider;
38  import org.apache.http.auth.AuthState;
39  import org.apache.http.client.CookieStore;
40  import org.apache.http.client.CredentialsProvider;
41  import org.apache.http.client.config.RequestConfig;
42  import org.apache.http.client.protocol.HttpClientContext;
43  import org.apache.http.concurrent.BasicFuture;
44  import org.apache.http.concurrent.FutureCallback;
45  import org.apache.http.config.Lookup;
46  import org.apache.http.conn.ConnectionKeepAliveStrategy;
47  import org.apache.http.cookie.CookieSpecProvider;
48  import org.apache.http.nio.NHttpClientEventHandler;
49  import org.apache.http.nio.conn.NHttpClientConnectionManager;
50  import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
51  import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
52  import org.apache.http.protocol.BasicHttpContext;
53  import org.apache.http.protocol.HttpContext;
54  
55  class InternalHttpAsyncClient extends CloseableHttpAsyncClientBase {
56  
57      private final Log log = LogFactory.getLog(getClass());
58  
59      private final NHttpClientConnectionManager connmgr;
60      private final ConnectionReuseStrategy connReuseStrategy;
61      private final ConnectionKeepAliveStrategy keepaliveStrategy;
62      private final InternalClientExec exec;
63      private final Lookup<CookieSpecProvider> cookieSpecRegistry;
64      private final Lookup<AuthSchemeProvider> authSchemeRegistry;
65      private final CookieStore cookieStore;
66      private final CredentialsProvider credentialsProvider;
67      private final RequestConfig defaultConfig;
68  
69      public InternalHttpAsyncClient(
70              final NHttpClientConnectionManager connmgr,
71              final ConnectionReuseStrategy connReuseStrategy,
72              final ConnectionKeepAliveStrategy keepaliveStrategy,
73              final ThreadFactory threadFactory,
74              final NHttpClientEventHandler handler,
75              final InternalClientExec exec,
76              final Lookup<CookieSpecProvider> cookieSpecRegistry,
77              final Lookup<AuthSchemeProvider> authSchemeRegistry,
78              final CookieStore cookieStore,
79              final CredentialsProvider credentialsProvider,
80              final RequestConfig defaultConfig) {
81          super(connmgr, threadFactory, handler);
82          this.connmgr = connmgr;
83          this.connReuseStrategy = connReuseStrategy;
84          this.keepaliveStrategy = keepaliveStrategy;
85          this.exec = exec;
86          this.cookieSpecRegistry = cookieSpecRegistry;
87          this.authSchemeRegistry = authSchemeRegistry;
88          this.cookieStore = cookieStore;
89          this.credentialsProvider = credentialsProvider;
90          this.defaultConfig = defaultConfig;
91      }
92  
93      private void setupContext(final HttpClientContext context) {
94          if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) {
95              context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState());
96          }
97          if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) {
98              context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthState());
99          }
100         if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) {
101             context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
102         }
103         if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) {
104             context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
105         }
106         if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {
107             context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
108         }
109         if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) {
110             context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
111         }
112         if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
113             context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);
114         }
115     }
116 
117     @Override
118     public <T> Future<T> execute(
119             final HttpAsyncRequestProducer requestProducer,
120             final HttpAsyncResponseConsumer<T> responseConsumer,
121             final HttpContext context,
122             final FutureCallback<T> callback) {
123         ensureRunning();
124         final BasicFuture<T> future = new BasicFuture<T>(callback);
125         final HttpClientContext localcontext = HttpClientContext.adapt(
126             context != null ? context : new BasicHttpContext());
127         setupContext(localcontext);
128 
129         @SuppressWarnings("resource")
130         final DefaultClientExchangeHandlerImpl<T> handler = new DefaultClientExchangeHandlerImpl<T>(
131             this.log,
132             requestProducer,
133             responseConsumer,
134             localcontext,
135             future,
136             this.connmgr,
137             this.connReuseStrategy,
138             this.keepaliveStrategy,
139             this.exec);
140         try {
141             handler.start();
142         } catch (final Exception ex) {
143             handler.failed(ex);
144         }
145         return new FutureWrapper<T>(future, handler);
146     }
147 
148     @Override
149     public <T> Future<List<T>> execute(
150             final HttpHost target,
151             final List<? extends HttpAsyncRequestProducer> requestProducers,
152             final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
153             final HttpContext context,
154             final FutureCallback<List<T>> callback) {
155         throw new UnsupportedOperationException("Pipelining not supported");
156     }
157 
158 }