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.hc.core5.http.impl.bootstrap;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.util.concurrent.Future;
33  
34  import org.apache.hc.core5.annotation.Internal;
35  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
36  import org.apache.hc.core5.concurrent.FutureCallback;
37  import org.apache.hc.core5.function.Callback;
38  import org.apache.hc.core5.function.Decorator;
39  import org.apache.hc.core5.function.Resolver;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.HttpRequest;
42  import org.apache.hc.core5.http.ProtocolException;
43  import org.apache.hc.core5.http.impl.DefaultAddressResolver;
44  import org.apache.hc.core5.io.CloseMode;
45  import org.apache.hc.core5.net.URIAuthority;
46  import org.apache.hc.core5.reactor.ConnectionInitiator;
47  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
48  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
49  import org.apache.hc.core5.reactor.IOReactorConfig;
50  import org.apache.hc.core5.reactor.IOReactorService;
51  import org.apache.hc.core5.reactor.IOReactorStatus;
52  import org.apache.hc.core5.reactor.IOSession;
53  import org.apache.hc.core5.reactor.IOSessionListener;
54  import org.apache.hc.core5.util.Args;
55  import org.apache.hc.core5.util.TimeValue;
56  import org.apache.hc.core5.util.Timeout;
57  
58  /**
59   * Protocol agnostic client side I/O session initiator.
60   *
61   * @since 5.0
62   */
63  public class AsyncRequester extends AbstractConnectionInitiatorBase implements IOReactorService {
64  
65      private final DefaultConnectingIOReactor ioReactor;
66      private final Resolver<HttpHost, InetSocketAddress> addressResolver;
67  
68      @Internal
69      public AsyncRequester(
70              final IOEventHandlerFactory eventHandlerFactory,
71              final IOReactorConfig ioReactorConfig,
72              final Decorator<IOSession> ioSessionDecorator,
73              final Callback<Exception> exceptionCallback,
74              final IOSessionListener sessionListener,
75              final Callback<IOSession> sessionShutdownCallback,
76              final Resolver<HttpHost, InetSocketAddress> addressResolver) {
77          this.ioReactor = new DefaultConnectingIOReactor(
78                  eventHandlerFactory,
79                  ioReactorConfig,
80                  new DefaultThreadFactory("requester-dispatch", true),
81                  ioSessionDecorator,
82                  exceptionCallback,
83                  sessionListener,
84                  sessionShutdownCallback);
85          this.addressResolver = addressResolver != null ? addressResolver : DefaultAddressResolver.INSTANCE;
86      }
87  
88      @Override
89      ConnectionInitiator getIOReactor() {
90          return ioReactor;
91      }
92  
93      public Future<IOSession> requestSession(
94              final HttpHost host,
95              final Timeout timeout,
96              final Object attachment,
97              final FutureCallback<IOSession> callback) {
98          Args.notNull(host, "Host");
99          Args.notNull(timeout, "Timeout");
100         return connect(host, addressResolver.resolve(host), null, timeout, attachment, callback);
101     }
102 
103     @Override
104     public void start() {
105         ioReactor.start();
106     }
107 
108     @Override
109     public IOReactorStatus getStatus() {
110         return ioReactor.getStatus();
111     }
112 
113     @Override
114     public void initiateShutdown() {
115         ioReactor.initiateShutdown();
116     }
117 
118     @Override
119     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
120         ioReactor.awaitShutdown(waitTime);
121     }
122 
123     @Override
124     public void close(final CloseMode closeMode) {
125         ioReactor.close(closeMode);
126     }
127 
128     @Override
129     public void close() throws IOException {
130         ioReactor.close();
131     }
132 
133     @Internal
134     protected static HttpHost defaultTarget(final HttpRequest request) throws ProtocolException {
135         final String scheme = request.getScheme();
136         final URIAuthority authority = request.getAuthority();
137         if (authority == null) {
138             throw new ProtocolException("Request authority not specified");
139         }
140         return new HttpHost(scheme, authority);
141     }
142 
143 }