Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / prevalidation / ValidationServiceOneWayClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2018-19 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.onap.aai.prevalidation;
22
23 import java.util.Collections;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.onap.aai.restclient.OneWaySSLRestClient;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
32 import org.springframework.util.MultiValueMap;
33
34 public class ValidationServiceOneWayClient extends OneWaySSLRestClient {
35
36     @Value("${validation.service.base.url}")
37     private String baseUrl;
38
39     @Value("${validation.service.ssl.trust-store}")
40     private String truststorePath;
41
42     @Value("${validation.service.ssl.trust-store-password}")
43     private String truststorePassword;
44
45     @Value("${validation.service.timeout-in-milliseconds}")
46     private Integer timeout;
47
48     @Override
49     public String getBaseUrl() {
50         return baseUrl;
51     }
52
53     @Override
54     protected String getTruststorePath() {
55         return truststorePath;
56     }
57
58     @Override
59     protected char[] getTruststorePassword() {
60         return truststorePassword.toCharArray();
61     }
62
63     @Override
64     protected HttpComponentsClientHttpRequestFactory getHttpRequestFactory() throws Exception {
65         HttpComponentsClientHttpRequestFactory requestFactory = super.getHttpRequestFactory();
66         requestFactory.setConnectionRequestTimeout(timeout);
67         requestFactory.setReadTimeout(timeout);
68         requestFactory.setConnectTimeout(timeout);
69         return requestFactory;
70     }
71
72     @Override
73     public MultiValueMap<String, String> getHeaders(Map<String, String> headers) {
74         HttpHeaders httpHeaders = new HttpHeaders();
75
76         String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
77         String defaultContentType =
78                 headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
79
80         if (headers.isEmpty()) {
81             httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept)));
82             httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType));
83         }
84
85         httpHeaders.add("X-FromAppId", appName);
86         httpHeaders.add("X-TransactionId", UUID.randomUUID().toString());
87         httpHeaders.add("X-TransactionId", appName);
88         headers.forEach(httpHeaders::add);
89         return httpHeaders;
90     }
91
92 }