56ddd744f568b31a759b4b444fbcebbb6caf29c1
[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 com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27
28 import java.util.Collections;
29 import java.util.Map;
30 import java.util.UUID;
31
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.MediaType;
35 import org.springframework.stereotype.Component;
36 import org.springframework.util.MultiValueMap;
37
38 @Component(value = "schema-service-rest-client")
39 public class SchemaServiceRestClient extends TwoWaySSLRestClient {
40     private static EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceRestClient.class);
41
42     @Value("${schema.service.base.url}")
43     private String baseUrl;
44
45     @Value("${schema.service.ssl.key-store}")
46     private String keystorePath;
47
48     @Value("${schema.service.ssl.trust-store}")
49     private String truststorePath;
50
51     @Value("${schema.service.ssl.key-store-password}")
52     private String keystorePassword;
53
54     @Value("${schema.service.ssl.trust-store-password}")
55     private String truststorePassword;
56
57     @Override
58     public String getBaseUrl() {
59         return baseUrl;
60     }
61
62     @Override
63     protected String getKeystorePath() {
64         return keystorePath;
65     }
66
67     @Override
68     protected String getTruststorePath() {
69         return truststorePath;
70     }
71
72     @Override
73     protected char[] getKeystorePassword() {
74         return keystorePassword.toCharArray();
75     }
76
77     @Override
78     protected char[] getTruststorePassword() {
79         return truststorePassword.toCharArray();
80     }
81
82     @Override
83     public MultiValueMap<String, String> getHeaders(Map<String, String> headers) {
84         HttpHeaders httpHeaders = new HttpHeaders();
85
86         String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
87         String defaultContentType =
88                 headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
89
90         if (headers.isEmpty()) {
91             httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept)));
92             httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType));
93         }
94
95         httpHeaders.add("X-FromAppId", appName);
96         httpHeaders.add("X-TransactionId", UUID.randomUUID().toString());
97         headers.forEach(httpHeaders::add);
98         return httpHeaders;
99     }
100
101 }