Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / commons / EWInterfaceUtils.java
1 /**
2  * Copyright (c) 2019 Vodafone Group
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.nbi.commons;
18
19 import org.onap.nbi.exceptions.BackendFunctionalException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.http.*;
25 import org.springframework.stereotype.Service;
26 import org.springframework.web.client.ResourceAccessException;
27 import org.springframework.web.client.RestTemplate;
28
29 @Service
30 public class EWInterfaceUtils {
31
32     public static final String RESPONSE_STATUS = "response status : ";
33     public static final String RETURNS = " returns ";
34     public static final String ERROR_ON_CALLING = "error on calling ";
35     private static final Logger LOGGER = LoggerFactory.getLogger(EWInterfaceUtils.class);
36     @Autowired
37     private RestTemplate restTemplate;
38     @Value("${so.host}")
39     private String soHostname;
40
41     @Value("${so.api.id}")
42     private String soApiId;
43
44     @Value("${so.header.authorization}")
45     private String soHeaderAuthorization;
46
47     private static final String HEADER_AUTHORIZATION = "Authorization";
48     private static final String X_FROM_APP_ID = "X-FromAppId";
49
50     public ResponseEntity<Object> callPostRequestTarget(Object obj, String targetUrl) {
51
52         try {
53             ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.POST,
54                     new HttpEntity<>(obj, buildRequestHeader()), Object.class);
55
56             logResponseGet(targetUrl, response);
57             if (null == response) {
58                 return null;
59             } else {
60                 return response;
61             }
62
63         } catch (BackendFunctionalException | ResourceAccessException e) {
64             LOGGER.error(ERROR_ON_CALLING + " ," + e.getMessage());
65             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
66         }
67     }
68
69     public ResponseEntity<Object> callGetRequestTarget(String targetUrl) {
70         try {
71             ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.GET,
72                     new HttpEntity<>(buildRequestHeader()), Object.class);
73             LOGGER.info("response status : {}", targetUrl);
74             logResponseGet(targetUrl, response);
75             if (null == response) {
76                 return null;
77             } else {
78                 return response;
79             }
80
81         } catch (BackendFunctionalException | ResourceAccessException e) {
82             LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
83             return null;
84         }
85     }
86
87     public ResponseEntity<Object> callDeleteRequestTarget(String targetUrl) {
88         try {
89             ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.DELETE,
90                     new HttpEntity<>(buildRequestHeader()), Object.class);
91             LOGGER.info("response status ewhost : {}", targetUrl);
92
93             if (null == response) {
94                 return null;
95             } else {
96                 return response;
97             }
98
99         } catch (BackendFunctionalException | ResourceAccessException e) {
100             LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
101             return null;
102         }
103     }
104
105     private void logResponseGet(String url, ResponseEntity<Object> response) {
106         if (response != null) {
107             if (LOGGER.isDebugEnabled()) {
108                 LOGGER.debug("response body : {}", response.getBody().toString());
109             }
110             if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
111                 LOGGER.warn("HTTP call EWInterface on {} returns {} , {}", url, response.getStatusCodeValue(),
112                         response.getBody().toString());
113             }
114         } else {
115             LOGGER.info("no response calling url {}", url);
116         }
117     }
118
119     private HttpHeaders buildRequestHeader() {
120         HttpHeaders httpHeaders = new HttpHeaders();
121         httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
122         httpHeaders.add(X_FROM_APP_ID, soApiId);
123         httpHeaders.add("Accept", "application/json");
124         httpHeaders.add("Content-Type", "application/json");
125         return httpHeaders;
126     }
127 }