summaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutable.java
blob: 4eadd14ab5ec8ccbf6c7bbec6a86754112f4d481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package org.openecomp.sdc.common.http.client.api;

import org.openecomp.sdc.common.http.config.BasicAuthorization;
import org.openecomp.sdc.common.http.config.ClientCertificate;
import org.openecomp.sdc.common.http.config.HttpClientConfig;
import org.openecomp.sdc.common.http.config.Timeouts;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

final class HttpClientConfigImmutable {

    private final Map<String, String> headers;
    private final BasicAuthorization basicAuthorization;
    private final ClientCertificate clientCertificate;
    private final Timeouts timeouts;
    /*
     * use ComparableHttpRequestRetryHandler.compare instead of default generated equals 
     */
    private final ComparableHttpRequestRetryHandler retryHandler;
    private final int numOfRetries;
    
    static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) {
        // TODO: retrieve from a pool if exist, otherwise create new
        return new HttpClientConfigImmutable(httpClientConfig); 
    }

    HttpClientConfigImmutable(HttpClientConfig httpClientConfig) {
        timeouts = httpClientConfig.getTimeouts() != null ? new Timeouts(httpClientConfig.getTimeouts()) : null;
        basicAuthorization = httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null;
        clientCertificate = httpClientConfig.getClientCertificate() != null ? new ClientCertificate(httpClientConfig.getClientCertificate()) : null;
        headers = httpClientConfig.getHeaders() != null ? Collections.unmodifiableMap(new HashMap<>(httpClientConfig.getHeaders())) : null;
        retryHandler = httpClientConfig.getRetryHandler();
        numOfRetries = httpClientConfig.getNumOfRetries();
    }

    Map<String, String> getHeaders() {
        return headers;
    }

    int getNumOfRetries() {
        return numOfRetries;
    }
    
    String getBasicAuthPassword() {
        return basicAuthorization != null ? basicAuthorization.getPassword() : null;
    }

    String getBasicAuthUserName() {
        return basicAuthorization != null ? basicAuthorization.getUserName() : null;
    }

    String getClientCertKeyStore() {
        return clientCertificate != null ? clientCertificate.getKeyStore() : null;
    }

    String getClientCertKeyPassword() {
        return clientCertificate != null ? clientCertificate.getKeyStorePassword() : null;
    }

    ClientCertificate getClientCertificate() {
        return clientCertificate != null ? new ClientCertificate(clientCertificate) : null;
    }
    
    int getReadTimeoutMs() {
        return timeouts.getReadTimeoutMs();
    }

    int getConnectTimeoutMs() {
        return timeouts.getConnectTimeoutMs();
    }

    int getConnectPoolTimeoutMs() {
        return timeouts.getConnectPoolTimeoutMs();
    }

    ComparableHttpRequestRetryHandler getRetryHandler() {
        return retryHandler;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((basicAuthorization == null) ? 0 : basicAuthorization.hashCode());
        result = prime * result + ((clientCertificate == null) ? 0 : clientCertificate.hashCode());
        result = prime * result + ((headers == null) ? 0 : headers.hashCode());
        result = prime * result + ((retryHandler == null) ? 0 : retryHandler.hashCode());
        result = prime * result + ((timeouts == null) ? 0 : timeouts.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj;
        if (basicAuthorization == null) {
            if (other.basicAuthorization != null)
                return false;
        }
        else if (!basicAuthorization.equals(other.basicAuthorization))
            return false;
        if (clientCertificate == null) {
            if (other.clientCertificate != null)
                return false;
        }
        else if (!clientCertificate.equals(other.clientCertificate))
            return false;
        if (headers == null) {
            if (other.headers != null)
                return false;
        }
        else if (!headers.equals(other.headers))
            return false;
        if (retryHandler == null) {
            if (other.retryHandler != null)
                return false;
        }
        else if (!retryHandler.compare(other.retryHandler))
            return false;
        if (timeouts == null) {
            if (other.timeouts != null)
                return false;
        }
        else if (!timeouts.equals(other.timeouts))
            return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("HttpClientConfigImmutable [basicAuthorization=");
        builder.append(basicAuthorization);
        builder.append(", clientCertificate=");
        builder.append(clientCertificate);
        builder.append(", retryHandler=");
        builder.append(retryHandler);
        builder.append(", timeouts=");
        builder.append(timeouts);
        builder.append(", headers=");
        builder.append(headers);
        builder.append(", numOfRetries=");
        builder.append(numOfRetries);
        builder.append("]");
        return builder.toString();
    }
}