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