Update aai-common to be spring boot 2 compatible
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / prevalidation / ValidationServiceNoAuthClient.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 org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.onap.aai.restclient.NoAuthRestClient;
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.ClientHttpRequestFactory;
30 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
31 import org.springframework.util.MultiValueMap;
32
33 import java.util.Collections;
34 import java.util.Map;
35 import java.util.UUID;
36
37 public class ValidationServiceNoAuthClient extends NoAuthRestClient {
38
39     private static Logger logger = LoggerFactory.getLogger(ValidationServiceNoAuthClient.class);
40
41     @Value("${validation.service.base.url}")
42     private String baseUrl;
43
44     @Value("${validation.service.timeout-in-milliseconds}")
45     private Integer timeout;
46
47     @Override
48     protected HttpComponentsClientHttpRequestFactory getHttpRequestFactory() throws Exception {
49         HttpComponentsClientHttpRequestFactory requestFactory = super.getHttpRequestFactory();
50         requestFactory.setConnectionRequestTimeout(timeout);
51         requestFactory.setReadTimeout(timeout);
52         requestFactory.setConnectTimeout(timeout);
53         return requestFactory;
54     }
55
56     @Override
57     public String getBaseUrl() {
58         return baseUrl;
59     }
60
61     @Override
62     public MultiValueMap<String, String> getHeaders(Map<String, String> headers) {
63         HttpHeaders httpHeaders = new HttpHeaders();
64
65         String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString());
66         String defaultContentType =
67                 headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString());
68
69         if (headers.isEmpty()) {
70             httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept)));
71             httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType));
72         }
73
74         httpHeaders.add("X-FromAppId", appName);
75         httpHeaders.add("X-TransactionId", UUID.randomUUID().toString());
76         headers.forEach(httpHeaders::add);
77         return httpHeaders;
78     }
79
80 }