Sync Integ to Master
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / client / api / HttpClientConfigImmutable.java
1 package org.openecomp.sdc.common.http.client.api;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.openecomp.sdc.common.http.config.BasicAuthorization;
8 import org.openecomp.sdc.common.http.config.ClientCertificate;
9 import org.openecomp.sdc.common.http.config.HttpClientConfig;
10 import org.openecomp.sdc.common.http.config.Timeouts;
11
12 final class HttpClientConfigImmutable {
13
14     private final Map<String, String> headers;
15     private final BasicAuthorization basicAuthorization;
16     private final ClientCertificate clientCertificate;
17     private final Timeouts timeouts;
18     /*
19      * use ComparableHttpRequestRetryHandler.compare instead of default generated equals 
20      */
21     private final ComparableHttpRequestRetryHandler retryHandler;
22     private final int numOfRetries;
23     
24     static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
25         // TODO: retrive from a pool if exist, otherwise create new
26         return new HttpClientConfigImmutable(httpClientConfig); 
27     }
28
29     HttpClientConfigImmutable(HttpClientConfig httpClientConfig) {
30         timeouts = httpClientConfig.getTimeouts() != null ? new Timeouts(httpClientConfig.getTimeouts()) : null;
31         basicAuthorization = httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null;
32         clientCertificate = httpClientConfig.getClientCertificate() != null ? new ClientCertificate(httpClientConfig.getClientCertificate()) : null;
33         headers = httpClientConfig.getHeaders() != null ? Collections.unmodifiableMap(new HashMap<>(httpClientConfig.getHeaders())) : null;
34         retryHandler = httpClientConfig.getRetryHandler();
35         numOfRetries = httpClientConfig.getNumOfRetries();
36     }
37
38     Map<String, String> getHeaders() {
39         return headers;
40     }
41
42     int getNumOfRetries() {
43         return numOfRetries;
44     }
45     
46     String getBasicAuthPassword() {
47         return basicAuthorization != null ? basicAuthorization.getPassword() : null;
48     }
49
50     String getBasicAuthUserName() {
51         return basicAuthorization != null ? basicAuthorization.getUserName() : null;
52     }
53
54     String getClientCertKeyStore() {
55         return clientCertificate != null ? clientCertificate.getKeyStore() : null;
56     }
57
58     String getClientCertKeyPassword() {
59         return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
60     }
61
62     ClientCertificate getClientCertificate() {
63         return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
64     }
65     
66     int getReadTimeoutMs() {
67         return timeouts.getReadTimeoutMs();
68     }
69
70     int getConnectTimeoutMs() {
71         return timeouts.getConnectTimeoutMs();
72     }
73
74     int getConnectPoolTimeoutMs() {
75         return timeouts.getConnectPoolTimeoutMs();
76     }
77
78     ComparableHttpRequestRetryHandler getRetryHandler() {
79         return retryHandler;
80     }
81
82     @Override
83     public int hashCode() {
84         final int prime = 31;
85         int result = 1;
86         result = prime * result + ((basicAuthorization == null) ? 0 : basicAuthorization.hashCode());
87         result = prime * result + ((clientCertificate == null) ? 0 : clientCertificate.hashCode());
88         result = prime * result + ((headers == null) ? 0 : headers.hashCode());
89         result = prime * result + ((retryHandler == null) ? 0 : retryHandler.hashCode());
90         result = prime * result + ((timeouts == null) ? 0 : timeouts.hashCode());
91         return result;
92     }
93
94     @Override
95     public boolean equals(Object obj) {
96         if (this == obj)
97             return true;
98         if (obj == null)
99             return false;
100         if (getClass() != obj.getClass())
101             return false;
102         HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
103         if (basicAuthorization == null) {
104             if (other.basicAuthorization != null)
105                 return false;
106         }
107         else if (!basicAuthorization.equals(other.basicAuthorization))
108             return false;
109         if (clientCertificate == null) {
110             if (other.clientCertificate != null)
111                 return false;
112         }
113         else if (!clientCertificate.equals(other.clientCertificate))
114             return false;
115         if (headers == null) {
116             if (other.headers != null)
117                 return false;
118         }
119         else if (!headers.equals(other.headers))
120             return false;
121         if (retryHandler == null) {
122             if (other.retryHandler != null)
123                 return false;
124         }
125         else if (!retryHandler.compare(other.retryHandler))
126             return false;
127         if (timeouts == null) {
128             if (other.timeouts != null)
129                 return false;
130         }
131         else if (!timeouts.equals(other.timeouts))
132             return false;
133         return true;
134     }
135
136     @Override
137     public String toString() {
138         StringBuilder builder = new StringBuilder();
139         builder.append("HttpClientConfigImmutable [basicAuthorization=");
140         builder.append(basicAuthorization);
141         builder.append(", clientCertificate=");
142         builder.append(clientCertificate);
143         builder.append(", retryHandler=");
144         builder.append(retryHandler);
145         builder.append(", timeouts=");
146         builder.append(timeouts);
147         builder.append(", headers=");
148         builder.append(headers);
149         builder.append(", numOfRetries=");
150         builder.append(numOfRetries);
151         builder.append("]");
152         return builder.toString();
153     }
154 }