Catalog alignment
[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 public 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     private boolean enableMetricLogging;
39     /*
40      * use ComparableHttpRequestRetryHandler.compare instead of default generated equals 
41      */
42     private final ComparableHttpRequestRetryHandler retryHandler;
43     private final int numOfRetries;
44     
45     static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
46         // TODO: retrieve from a pool if exist, otherwise create new
47         return new HttpClientConfigImmutable(httpClientConfig); 
48     }
49
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();
58     }
59
60     public boolean isEnableMetricLogging() {
61         return enableMetricLogging;
62     }
63
64     Map<String, String> getHeaders() {
65         return headers;
66     }
67
68     public int getNumOfRetries() {
69         return numOfRetries;
70     }
71     
72     String getBasicAuthPassword() {
73         return basicAuthorization != null ? basicAuthorization.getPassword() : null;
74     }
75
76     String getBasicAuthUserName() {
77         return basicAuthorization != null ? basicAuthorization.getUserName() : null;
78     }
79
80     String getClientCertKeyStore() {
81         return clientCertificate != null ? clientCertificate.getKeyStore() : null;
82     }
83
84     String getClientCertKeyPassword() {
85         return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
86     }
87
88     ClientCertificate getClientCertificate() {
89         return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
90     }
91     
92     public int getReadTimeoutMs() {
93         return timeouts.getReadTimeoutMs();
94     }
95
96     public int getConnectTimeoutMs() {
97         return timeouts.getConnectTimeoutMs();
98     }
99
100     public int getConnectPoolTimeoutMs() {
101         return timeouts.getConnectPoolTimeoutMs();
102     }
103
104     public ComparableHttpRequestRetryHandler getRetryHandler() {
105         return retryHandler;
106     }
107
108     @Override
109     public int hashCode() {
110         final int prime = 31;
111         int result = 1;
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());
117         return result;
118     }
119
120     @Override
121     public boolean equals(Object obj) {
122         if (this == obj)
123             return true;
124         if (obj == null)
125             return false;
126         if (getClass() != obj.getClass())
127             return false;
128         HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
129         if (basicAuthorization == null) {
130             if (other.basicAuthorization != null)
131                 return false;
132         }
133         else if (!basicAuthorization.equals(other.basicAuthorization))
134             return false;
135         if (clientCertificate == null) {
136             if (other.clientCertificate != null)
137                 return false;
138         }
139         else if (!clientCertificate.equals(other.clientCertificate))
140             return false;
141         if (headers == null) {
142             if (other.headers != null)
143                 return false;
144         }
145         else if (!headers.equals(other.headers))
146             return false;
147         if (retryHandler == null) {
148             if (other.retryHandler != null)
149                 return false;
150         }
151         else if (!retryHandler.compare(other.retryHandler))
152             return false;
153         if (timeouts == null) {
154             if (other.timeouts != null)
155                 return false;
156         }
157         else if (!timeouts.equals(other.timeouts))
158             return false;
159         return true;
160     }
161
162     @Override
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);
177         builder.append("]");
178         return builder.toString();
179     }
180 }