1 package org.openecomp.sdc.common.http.client.api;
3 import java.util.Collections;
4 import java.util.HashMap;
7 import org.openecomp.sdc.common.http.config.BasicAuthorization;
8 import org.openecomp.sdc.common.http.config.ClientCertificate;
9 import org.openecomp.sdc.common.http.config.HttpClientConfig;
10 import org.openecomp.sdc.common.http.config.Timeouts;
12 final class HttpClientConfigImmutable {
14 private final Map<String, String> headers;
15 private final BasicAuthorization basicAuthorization;
16 private final ClientCertificate clientCertificate;
17 private final Timeouts timeouts;
19 * use ComparableHttpRequestRetryHandler.compare instead of default generated equals
21 private final ComparableHttpRequestRetryHandler retryHandler;
22 private final int numOfRetries;
24 static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
25 // TODO: retrive from a pool if exist, otherwise create new
26 return new HttpClientConfigImmutable(httpClientConfig);
29 HttpClientConfigImmutable(HttpClientConfig httpClientConfig) {
30 timeouts = httpClientConfig.getTimeouts() != null ? new Timeouts(httpClientConfig.getTimeouts()) : null;
31 basicAuthorization = httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null;
32 clientCertificate = httpClientConfig.getClientCertificate() != null ? new ClientCertificate(httpClientConfig.getClientCertificate()) : null;
33 headers = httpClientConfig.getHeaders() != null ? Collections.unmodifiableMap(new HashMap<>(httpClientConfig.getHeaders())) : null;
34 retryHandler = httpClientConfig.getRetryHandler();
35 numOfRetries = httpClientConfig.getNumOfRetries();
38 Map<String, String> getHeaders() {
42 int getNumOfRetries() {
46 String getBasicAuthPassword() {
47 return basicAuthorization != null ? basicAuthorization.getPassword() : null;
50 String getBasicAuthUserName() {
51 return basicAuthorization != null ? basicAuthorization.getUserName() : null;
54 String getClientCertKeyStore() {
55 return clientCertificate != null ? clientCertificate.getKeyStore() : null;
58 String getClientCertKeyPassword() {
59 return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
62 ClientCertificate getClientCertificate() {
63 return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
66 int getReadTimeoutMs() {
67 return timeouts.getReadTimeoutMs();
70 int getConnectTimeoutMs() {
71 return timeouts.getConnectTimeoutMs();
74 int getConnectPoolTimeoutMs() {
75 return timeouts.getConnectPoolTimeoutMs();
78 ComparableHttpRequestRetryHandler getRetryHandler() {
83 public int hashCode() {
86 result = prime * result + ((basicAuthorization == null) ? 0 : basicAuthorization.hashCode());
87 result = prime * result + ((clientCertificate == null) ? 0 : clientCertificate.hashCode());
88 result = prime * result + ((headers == null) ? 0 : headers.hashCode());
89 result = prime * result + ((retryHandler == null) ? 0 : retryHandler.hashCode());
90 result = prime * result + ((timeouts == null) ? 0 : timeouts.hashCode());
95 public boolean equals(Object obj) {
100 if (getClass() != obj.getClass())
102 HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
103 if (basicAuthorization == null) {
104 if (other.basicAuthorization != null)
107 else if (!basicAuthorization.equals(other.basicAuthorization))
109 if (clientCertificate == null) {
110 if (other.clientCertificate != null)
113 else if (!clientCertificate.equals(other.clientCertificate))
115 if (headers == null) {
116 if (other.headers != null)
119 else if (!headers.equals(other.headers))
121 if (retryHandler == null) {
122 if (other.retryHandler != null)
125 else if (!retryHandler.compare(other.retryHandler))
127 if (timeouts == null) {
128 if (other.timeouts != null)
131 else if (!timeouts.equals(other.timeouts))
137 public String toString() {
138 StringBuilder builder = new StringBuilder();
139 builder.append("HttpClientConfigImmutable [basicAuthorization=");
140 builder.append(basicAuthorization);
141 builder.append(", clientCertificate=");
142 builder.append(clientCertificate);
143 builder.append(", retryHandler=");
144 builder.append(retryHandler);
145 builder.append(", timeouts=");
146 builder.append(timeouts);
147 builder.append(", headers=");
148 builder.append(headers);
149 builder.append(", numOfRetries=");
150 builder.append(numOfRetries);
152 return builder.toString();