Added oparent to sdc main
[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         if (obj == null)
119             return false;
120         if (getClass() != obj.getClass())
121             return false;
122         HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
123         if (basicAuthorization == null) {
124             if (other.basicAuthorization != null)
125                 return false;
126         }
127         else if (!basicAuthorization.equals(other.basicAuthorization))
128             return false;
129         if (clientCertificate == null) {
130             if (other.clientCertificate != null)
131                 return false;
132         }
133         else if (!clientCertificate.equals(other.clientCertificate))
134             return false;
135         if (headers == null) {
136             if (other.headers != null)
137                 return false;
138         }
139         else if (!headers.equals(other.headers))
140             return false;
141         if (retryHandler == null) {
142             if (other.retryHandler != null)
143                 return false;
144         }
145         else if (!retryHandler.compare(other.retryHandler))
146             return false;
147         if (timeouts == null) {
148             if (other.timeouts != null)
149                 return false;
150         }
151         else if (!timeouts.equals(other.timeouts))
152             return false;
153         return true;
154     }
155
156     @Override
157     public String toString() {
158         StringBuilder builder = new StringBuilder();
159         builder.append("HttpClientConfigImmutable [basicAuthorization=");
160         builder.append(basicAuthorization);
161         builder.append(", clientCertificate=");
162         builder.append(clientCertificate);
163         builder.append(", retryHandler=");
164         builder.append(retryHandler);
165         builder.append(", timeouts=");
166         builder.append(timeouts);
167         builder.append(", headers=");
168         builder.append(headers);
169         builder.append(", numOfRetries=");
170         builder.append(numOfRetries);
171         builder.append("]");
172         return builder.toString();
173     }
174 }