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 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;
39 * use ComparableHttpRequestRetryHandler.compare instead of default generated equals
41 private final ComparableHttpRequestRetryHandler retryHandler;
42 private final int numOfRetries;
44 static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
45 // TODO: retrieve from a pool if exist, otherwise create new
46 return new HttpClientConfigImmutable(httpClientConfig);
49 HttpClientConfigImmutable(HttpClientConfig httpClientConfig) {
50 timeouts = httpClientConfig.getTimeouts() != null ? new Timeouts(httpClientConfig.getTimeouts()) : null;
51 basicAuthorization = httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null;
52 clientCertificate = httpClientConfig.getClientCertificate() != null ? new ClientCertificate(httpClientConfig.getClientCertificate()) : null;
53 headers = httpClientConfig.getHeaders() != null ? Collections.unmodifiableMap(new HashMap<>(httpClientConfig.getHeaders())) : null;
54 retryHandler = httpClientConfig.getRetryHandler();
55 numOfRetries = httpClientConfig.getNumOfRetries();
58 Map<String, String> getHeaders() {
62 int getNumOfRetries() {
66 String getBasicAuthPassword() {
67 return basicAuthorization != null ? basicAuthorization.getPassword() : null;
70 String getBasicAuthUserName() {
71 return basicAuthorization != null ? basicAuthorization.getUserName() : null;
74 String getClientCertKeyStore() {
75 return clientCertificate != null ? clientCertificate.getKeyStore() : null;
78 String getClientCertKeyPassword() {
79 return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
82 ClientCertificate getClientCertificate() {
83 return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
86 int getReadTimeoutMs() {
87 return timeouts.getReadTimeoutMs();
90 int getConnectTimeoutMs() {
91 return timeouts.getConnectTimeoutMs();
94 int getConnectPoolTimeoutMs() {
95 return timeouts.getConnectPoolTimeoutMs();
98 ComparableHttpRequestRetryHandler getRetryHandler() {
103 public int hashCode() {
104 final int prime = 31;
106 result = prime * result + ((basicAuthorization == null) ? 0 : basicAuthorization.hashCode());
107 result = prime * result + ((clientCertificate == null) ? 0 : clientCertificate.hashCode());
108 result = prime * result + ((headers == null) ? 0 : headers.hashCode());
109 result = prime * result + ((retryHandler == null) ? 0 : retryHandler.hashCode());
110 result = prime * result + ((timeouts == null) ? 0 : timeouts.hashCode());
115 public boolean equals(Object obj) {
122 if (getClass() != obj.getClass()) {
125 HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
126 if (basicAuthorization == null) {
127 if (other.basicAuthorization != null) {
130 } else if (!basicAuthorization.equals(other.basicAuthorization)) {
133 if (clientCertificate == null) {
134 if (other.clientCertificate != null) {
137 } else if (!clientCertificate.equals(other.clientCertificate)) {
140 if (headers == null) {
141 if (other.headers != null) {
144 } else if (!headers.equals(other.headers)) {
147 if (retryHandler == null) {
148 if (other.retryHandler != null) {
151 } else if (!retryHandler.compare(other.retryHandler)) {
154 if (timeouts == null) {
155 if (other.timeouts != null) {
158 } else if (!timeouts.equals(other.timeouts)) {
165 public String toString() {
166 StringBuilder builder = new StringBuilder();
167 builder.append("HttpClientConfigImmutable [basicAuthorization=");
168 builder.append(basicAuthorization);
169 builder.append(", clientCertificate=");
170 builder.append(clientCertificate);
171 builder.append(", retryHandler=");
172 builder.append(retryHandler);
173 builder.append(", timeouts=");
174 builder.append(timeouts);
175 builder.append(", headers=");
176 builder.append(headers);
177 builder.append(", numOfRetries=");
178 builder.append(numOfRetries);
180 return builder.toString();