2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.http.client.api;
23 import org.openecomp.sdc.common.http.config.BasicAuthorization;
24 import org.openecomp.sdc.common.http.config.ClientCertificate;
25 import org.openecomp.sdc.common.http.config.HttpClientConfig;
26 import org.openecomp.sdc.common.http.config.Timeouts;
28 import java.util.Collections;
29 import java.util.HashMap;
32 public final class HttpClientConfigImmutable {
34 private final Map<String, String> headers;
35 private final BasicAuthorization basicAuthorization;
36 private final ClientCertificate clientCertificate;
37 private final Timeouts timeouts;
38 private boolean enableMetricLogging;
40 * use ComparableHttpRequestRetryHandler.compare instead of default generated equals
42 private final ComparableHttpRequestRetryHandler retryHandler;
43 private final int numOfRetries;
45 static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
46 // TODO: retrieve from a pool if exist, otherwise create new
47 return new HttpClientConfigImmutable(httpClientConfig);
50 public HttpClientConfigImmutable(HttpClientConfig httpClientConfig) {
51 timeouts = httpClientConfig.getTimeouts() != null ? new Timeouts(httpClientConfig.getTimeouts()) : null;
52 basicAuthorization = httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null;
53 clientCertificate = httpClientConfig.getClientCertificate() != null ? new ClientCertificate(httpClientConfig.getClientCertificate()) : null;
54 headers = httpClientConfig.getHeaders() != null ? Collections.unmodifiableMap(new HashMap<>(httpClientConfig.getHeaders())) : null;
55 retryHandler = httpClientConfig.getRetryHandler();
56 numOfRetries = httpClientConfig.getNumOfRetries();
57 enableMetricLogging = httpClientConfig.isEnableMetricLogging();
60 public boolean isEnableMetricLogging() {
61 return enableMetricLogging;
64 Map<String, String> getHeaders() {
68 public int getNumOfRetries() {
72 String getBasicAuthPassword() {
73 return basicAuthorization != null ? basicAuthorization.getPassword() : null;
76 String getBasicAuthUserName() {
77 return basicAuthorization != null ? basicAuthorization.getUserName() : null;
80 String getClientCertKeyStore() {
81 return clientCertificate != null ? clientCertificate.getKeyStore() : null;
84 String getClientCertKeyPassword() {
85 return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
88 ClientCertificate getClientCertificate() {
89 return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
92 public int getReadTimeoutMs() {
93 return timeouts.getReadTimeoutMs();
96 public int getConnectTimeoutMs() {
97 return timeouts.getConnectTimeoutMs();
100 public int getConnectPoolTimeoutMs() {
101 return timeouts.getConnectPoolTimeoutMs();
104 public ComparableHttpRequestRetryHandler getRetryHandler() {
109 public int hashCode() {
110 final int prime = 31;
112 result = prime * result + ((basicAuthorization == null) ? 0 : basicAuthorization.hashCode());
113 result = prime * result + ((clientCertificate == null) ? 0 : clientCertificate.hashCode());
114 result = prime * result + ((headers == null) ? 0 : headers.hashCode());
115 result = prime * result + ((retryHandler == null) ? 0 : retryHandler.hashCode());
116 result = prime * result + ((timeouts == null) ? 0 : timeouts.hashCode());
121 public boolean equals(Object obj) {
126 if (getClass() != obj.getClass())
128 HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
129 if (basicAuthorization == null) {
130 if (other.basicAuthorization != null)
133 else if (!basicAuthorization.equals(other.basicAuthorization))
135 if (clientCertificate == null) {
136 if (other.clientCertificate != null)
139 else if (!clientCertificate.equals(other.clientCertificate))
141 if (headers == null) {
142 if (other.headers != null)
145 else if (!headers.equals(other.headers))
147 if (retryHandler == null) {
148 if (other.retryHandler != null)
151 else if (!retryHandler.compare(other.retryHandler))
153 if (timeouts == null) {
154 if (other.timeouts != null)
157 else if (!timeouts.equals(other.timeouts))
163 public String toString() {
164 StringBuilder builder = new StringBuilder();
165 builder.append("HttpClientConfigImmutable [basicAuthorization=");
166 builder.append(basicAuthorization);
167 builder.append(", clientCertificate=");
168 builder.append(clientCertificate);
169 builder.append(", retryHandler=");
170 builder.append(retryHandler);
171 builder.append(", timeouts=");
172 builder.append(timeouts);
173 builder.append(", headers=");
174 builder.append(headers);
175 builder.append(", numOfRetries=");
176 builder.append(numOfRetries);
178 return builder.toString();