fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / client / api / HttpClientConfigImmutable.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.common.http.client.api;
22
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;
27
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 final class HttpClientConfigImmutable {
33
34     private final Map<String, String> headers;
35     private final BasicAuthorization basicAuthorization;
36     private final ClientCertificate clientCertificate;
37     private final Timeouts timeouts;
38     /*
39      * use ComparableHttpRequestRetryHandler.compare instead of default generated equals
40      */
41     private final ComparableHttpRequestRetryHandler retryHandler;
42     private final int numOfRetries;
43
44     static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
45         // TODO: retrieve from a pool if exist, otherwise create new
46         return new HttpClientConfigImmutable(httpClientConfig);
47     }
48
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();
56     }
57
58     Map<String, String> getHeaders() {
59         return headers;
60     }
61
62     int getNumOfRetries() {
63         return numOfRetries;
64     }
65
66     String getBasicAuthPassword() {
67         return basicAuthorization != null ? basicAuthorization.getPassword() : null;
68     }
69
70     String getBasicAuthUserName() {
71         return basicAuthorization != null ? basicAuthorization.getUserName() : null;
72     }
73
74     String getClientCertKeyStore() {
75         return clientCertificate != null ? clientCertificate.getKeyStore() : null;
76     }
77
78     String getClientCertKeyPassword() {
79         return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
80     }
81
82     ClientCertificate getClientCertificate() {
83         return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
84     }
85
86     int getReadTimeoutMs() {
87         return timeouts.getReadTimeoutMs();
88     }
89
90     int getConnectTimeoutMs() {
91         return timeouts.getConnectTimeoutMs();
92     }
93
94     int getConnectPoolTimeoutMs() {
95         return timeouts.getConnectPoolTimeoutMs();
96     }
97
98     ComparableHttpRequestRetryHandler getRetryHandler() {
99         return retryHandler;
100     }
101
102     @Override
103     public int hashCode() {
104         final int prime = 31;
105         int result = 1;
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());
111         return result;
112     }
113
114     @Override
115     public boolean equals(Object obj) {
116         if (this == obj) {
117             return true;
118         }
119         if (obj == null) {
120             return false;
121         }
122         if (getClass() != obj.getClass()) {
123             return false;
124         }
125         HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
126         if (basicAuthorization == null) {
127             if (other.basicAuthorization != null) {
128                 return false;
129             }
130         } else if (!basicAuthorization.equals(other.basicAuthorization)) {
131             return false;
132         }
133         if (clientCertificate == null) {
134             if (other.clientCertificate != null) {
135                 return false;
136             }
137         } else if (!clientCertificate.equals(other.clientCertificate)) {
138             return false;
139         }
140         if (headers == null) {
141             if (other.headers != null) {
142                 return false;
143             }
144         } else if (!headers.equals(other.headers)) {
145             return false;
146         }
147         if (retryHandler == null) {
148             if (other.retryHandler != null) {
149                 return false;
150             }
151         } else if (!retryHandler.compare(other.retryHandler)) {
152             return false;
153         }
154         if (timeouts == null) {
155             if (other.timeouts != null) {
156                 return false;
157             }
158         } else if (!timeouts.equals(other.timeouts)) {
159             return false;
160         }
161         return true;
162     }
163
164     @Override
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);
179         builder.append("]");
180         return builder.toString();
181     }
182 }