Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / prevalidation / ValidationServiceRestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.prevalidation;
24
25 import org.onap.aai.restclient.TwoWaySSLRestClient;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
30 import org.springframework.util.MultiValueMap;
31
32 import java.util.Collections;
33 import java.util.Map;
34 import java.util.UUID;
35
36 public class ValidationServiceRestClient extends TwoWaySSLRestClient {
37
38     @Value("${validation.service.base.url}")
39     private String baseUrl;
40
41     @Value("${validation.service.ssl.key-store}")
42     private String keystorePath;
43
44     @Value("${validation.service.ssl.trust-store}")
45     private String truststorePath;
46
47     @Value("${validation.service.ssl.key-store-password}")
48     private String keystorePassword;
49
50     @Value("${validation.service.ssl.trust-store-password}")
51     private String truststorePassword;
52
53     @Value("${validation.service.timeout-in-milliseconds}")
54     private Integer timeout;
55
56     @Override
57     public String getBaseUrl() {
58         return baseUrl;
59     }
60
61     @Override
62     protected String getKeystorePath() {
63         return keystorePath;
64     }
65
66     @Override
67     protected String getTruststorePath() {
68         return truststorePath;
69     }
70
71     @Override
72     protected char[] getKeystorePassword() {
73         return keystorePassword.toCharArray();
74     }
75
76     @Override
77     protected char[] getTruststorePassword() {
78         return truststorePassword.toCharArray();
79     }
80
81     protected HttpComponentsClientHttpRequestFactory getHttpRequestFactory() throws Exception {
82         HttpComponentsClientHttpRequestFactory requestFactory = super.getHttpRequestFactory();
83         requestFactory.setConnectionRequestTimeout(timeout);
84         requestFactory.setReadTimeout(timeout);
85         requestFactory.setConnectTimeout(timeout);
86         return requestFactory;
87     }
88
89     @Override
90     public MultiValueMap<String, String> getHeaders(Map<String, String> headers) {
91         HttpHeaders httpHeaders = new HttpHeaders();
92
93         String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
94         String defaultContentType =
95                 headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
96
97         if (headers.isEmpty()) {
98             httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept)));
99             httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType));
100         }
101
102         httpHeaders.add("X-FromAppId", appName);
103         httpHeaders.add("X-TransactionId", UUID.randomUUID().toString());
104         headers.forEach(httpHeaders::add);
105         return httpHeaders;
106     }
107
108 }