Enhancements for the aai-common library
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / restclient / SchemaServiceRestClient.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.restclient;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.MediaType;
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 SchemaServiceRestClient extends TwoWaySSLRestClient {
37     private static Logger logger = LoggerFactory.getLogger(SchemaServiceRestClient.class);
38
39     @Value("${schema.service.base.url}")
40     private String baseUrl;
41
42     @Value("${schema.service.ssl.key-store}")
43     private String keystorePath;
44
45     @Value("${schema.service.ssl.trust-store}")
46     private String truststorePath;
47
48     @Value("${schema.service.ssl.key-store-password}")
49     private String keystorePassword;
50
51     @Value("${schema.service.ssl.trust-store-password}")
52     private String truststorePassword;
53
54     @Override
55     public String getBaseUrl() {
56         return baseUrl;
57     }
58
59     @Override
60     protected String getKeystorePath() {
61         return keystorePath;
62     }
63
64     @Override
65     protected String getTruststorePath() {
66         return truststorePath;
67     }
68
69     @Override
70     protected char[] getKeystorePassword() {
71         return keystorePassword.toCharArray();
72     }
73
74     @Override
75     protected char[] getTruststorePassword() {
76         return truststorePassword.toCharArray();
77     }
78
79     @Override
80     public MultiValueMap<String, String> getHeaders(Map<String, String> headers) {
81         HttpHeaders httpHeaders = new HttpHeaders();
82
83         String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
84         String defaultContentType =
85                 headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
86
87         if (headers.isEmpty()) {
88             httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept)));
89             httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType));
90         }
91
92         httpHeaders.add("X-FromAppId", appName);
93         httpHeaders.add("X-TransactionId", UUID.randomUUID().toString());
94         headers.forEach(httpHeaders::add);
95         return httpHeaders;
96     }
97
98 }