| 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.client; |
| 29 |
|
| 30 |
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; |
| 31 |
import org.apache.http.nio.conn.NHttpClientConnectionManager; |
| 32 |
import org.apache.http.nio.reactor.ConnectingIOReactor; |
| 33 |
import org.apache.http.util.Args; |
| 34 |
|
| 35 |
/** |
| 36 |
* Factory methods for {@link org.apache.http.impl.nio.client.CloseableHttpAsyncClient} and |
| 37 |
* {@link org.apache.http.impl.nio.client.CloseableHttpPipeliningClient} instances. |
| 38 |
* |
| 39 |
* @since 4.0 |
| 40 |
*/ |
| 41 |
public class HttpAsyncClients { |
| 42 |
|
| 43 |
private HttpAsyncClients() { |
| 44 |
super(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Creates builder object for construction of custom |
| 49 |
* {@link CloseableHttpAsyncClient} instances. |
| 50 |
*/ |
| 51 |
public static HttpAsyncClientBuilder custom() { |
| 52 |
return HttpAsyncClientBuilder.create(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates {@link CloseableHttpAsyncClient} instance with default |
| 57 |
* configuration. |
| 58 |
*/ |
| 59 |
public static CloseableHttpAsyncClient createDefault() { |
| 60 |
return HttpAsyncClientBuilder.create().build(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Creates {@link CloseableHttpAsyncClient} instance with default |
| 65 |
* configuration based on ssytem properties. |
| 66 |
*/ |
| 67 |
public static CloseableHttpAsyncClient createSystem() { |
| 68 |
return HttpAsyncClientBuilder.create() |
| 69 |
.useSystemProperties() |
| 70 |
.build(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Creates {@link CloseableHttpAsyncClient} instance that supports esential HTTP protocol |
| 75 |
* aspects only. This client does not support HTTP state management, authentication |
| 76 |
* and automatic redirects. |
| 77 |
*/ |
| 78 |
public static CloseableHttpAsyncClient createMinimal() { |
| 79 |
return MinimalHttpAsyncClientBuilder.create() |
| 80 |
.disableCookieManagement() |
| 81 |
.build(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Creates {@link CloseableHttpAsyncClient} instance that supports esential HTTP protocol |
| 86 |
* aspects only. This client does not support HTTP state management, authentication |
| 87 |
* and automatic redirects. |
| 88 |
*/ |
| 89 |
public static CloseableHttpAsyncClient createMinimal(final ConnectingIOReactor ioReactor) { |
| 90 |
Args.notNull(ioReactor, "I/O reactor"); |
| 91 |
return createMinimal(new PoolingNHttpClientConnectionManager(ioReactor), false); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Creates {@link CloseableHttpAsyncClient} instance that supports esential HTTP protocol |
| 96 |
* aspects only. This client does not support HTTP state management, authentication |
| 97 |
* and automatic redirects. |
| 98 |
*/ |
| 99 |
public static CloseableHttpAsyncClient createMinimal(final NHttpClientConnectionManager connManager) { |
| 100 |
return createMinimal(connManager, false); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Creates {@link CloseableHttpAsyncClient} instance that supports esential HTTP protocol |
| 105 |
* aspects only. This client does not support HTTP state management, authentication |
| 106 |
* and automatic redirects. |
| 107 |
* <p> |
| 108 |
* Please note that clients with a shared connection manager make no attempts to control |
| 109 |
* its life cycle and dealocation of resources. It is a responibility of the caller to |
| 110 |
* ensure that the shared connection manager is properly started and shut down when no |
| 111 |
* longer needed. |
| 112 |
* |
| 113 |
* @since 4.1 |
| 114 |
*/ |
| 115 |
public static CloseableHttpAsyncClient createMinimal( |
| 116 |
final NHttpClientConnectionManager connManager, final boolean shared) { |
| 117 |
Args.notNull(connManager, "Connection manager"); |
| 118 |
return MinimalHttpAsyncClientBuilder.create() |
| 119 |
.setConnectionManager(connManager) |
| 120 |
.setConnectionManagerShared(shared) |
| 121 |
.disableCookieManagement() |
| 122 |
.build(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Creates {@link CloseableHttpPipeliningClient} instance that supports pipelined request |
| 127 |
* execution. This client does not support authentication and automatic redirects. |
| 128 |
* |
| 129 |
* @since 4.1 |
| 130 |
*/ |
| 131 |
public static CloseableHttpPipeliningClient createPipelining() { |
| 132 |
return MinimalHttpAsyncClientBuilder.create().build(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Creates {@link CloseableHttpPipeliningClient} instance that supports pipelined request |
| 137 |
* execution. This client does not support authentication and automatic redirects. |
| 138 |
* |
| 139 |
* @since 4.1 |
| 140 |
*/ |
| 141 |
public static CloseableHttpPipeliningClient createPipelining(final ConnectingIOReactor ioReactor) { |
| 142 |
return createPipelining(new PoolingNHttpClientConnectionManager(ioReactor), false); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Creates {@link CloseableHttpPipeliningClient} instance that supports pipelined request |
| 147 |
* execution. This client does not support authentication and automatic redirects. |
| 148 |
* |
| 149 |
* @since 4.1 |
| 150 |
*/ |
| 151 |
public static CloseableHttpPipeliningClient createPipelining(final NHttpClientConnectionManager connManager) { |
| 152 |
return createPipelining(connManager, false); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Creates {@link CloseableHttpPipeliningClient} instance that supports pipelined request |
| 157 |
* execution. This client does not support authentication and automatic redirects. |
| 158 |
* <p> |
| 159 |
* Please note that clients with a shared connection manager make no attempts to control |
| 160 |
* its life cycle and dealocation of resources. It is a responibility of the caller to |
| 161 |
* ensure that the shared connection manager is properly started and shut down when no |
| 162 |
* longer needed. |
| 163 |
* |
| 164 |
* @since 4.1 |
| 165 |
*/ |
| 166 |
public static CloseableHttpPipeliningClient createPipelining( |
| 167 |
final NHttpClientConnectionManager connManager, final boolean shared) { |
| 168 |
Args.notNull(connManager, "Connection manager"); |
| 169 |
return MinimalHttpAsyncClientBuilder.create() |
| 170 |
.setConnectionManager(connManager) |
| 171 |
.setConnectionManagerShared(shared) |
| 172 |
.build(); |
| 173 |
} |
| 174 |
|
| 175 |
} |