/
DefaultHttpClientFactory.java
87 lines (76 loc) · 3.01 KB
/
DefaultHttpClientFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.atlassian.httpclient.apache.httpcomponents;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.httpclient.api.HttpClient;
import com.atlassian.httpclient.api.factory.HttpClientFactory;
import com.atlassian.httpclient.api.factory.HttpClientOptions;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.executor.ThreadLocalContextManager;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.beans.factory.DisposableBean;
import javax.annotation.Nonnull;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import static com.google.common.base.Preconditions.checkNotNull;
public final class DefaultHttpClientFactory implements HttpClientFactory, DisposableBean
{
private final EventPublisher eventPublisher;
private final ApplicationProperties applicationProperties;
private final ThreadLocalContextManager threadLocalContextManager;
private final Set<ApacheAsyncHttpClient> httpClients = new CopyOnWriteArraySet<ApacheAsyncHttpClient>();
public DefaultHttpClientFactory(EventPublisher eventPublisher, ApplicationProperties applicationProperties, ThreadLocalContextManager threadLocalContextManager)
{
this.eventPublisher = checkNotNull(eventPublisher);
this.applicationProperties = checkNotNull(applicationProperties);
this.threadLocalContextManager = checkNotNull(threadLocalContextManager);
}
@Override
public HttpClient create(HttpClientOptions options)
{
return doCreate(options, threadLocalContextManager);
}
@Override
public HttpClient create(HttpClientOptions options, ThreadLocalContextManager threadLocalContextManager)
{
return doCreate(options, threadLocalContextManager);
}
@Override
public void dispose(@Nonnull final HttpClient httpClient) throws Exception
{
if (httpClient instanceof ApacheAsyncHttpClient)
{
final ApacheAsyncHttpClient client = (ApacheAsyncHttpClient) httpClient;
if (httpClients.remove(client))
{
client.destroy();
}
else
{
throw new IllegalStateException("Client is already disposed");
}
}
else
{
throw new IllegalArgumentException("Given client is not disposable");
}
}
private HttpClient doCreate(HttpClientOptions options, ThreadLocalContextManager threadLocalContextManager)
{
checkNotNull(options);
final ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(eventPublisher, applicationProperties, threadLocalContextManager, options);
httpClients.add(httpClient);
return httpClient;
}
@Override
public void destroy() throws Exception
{
for (ApacheAsyncHttpClient httpClient : httpClients)
{
httpClient.destroy();
}
}
@VisibleForTesting
Iterable<ApacheAsyncHttpClient> getHttpClients()
{
return httpClients;
}
}