24bf1fca4c5f02a046711ba5116b0f1b23308d9e
[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 import org.onap.nbi.exceptions.BackendFunctionalException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.beans.factory.annotation.Value;
23 import org.springframework.http.*;
24 import org.springframework.stereotype.Service;
25 import org.springframework.web.client.ResourceAccessException;
26 import org.springframework.web.client.RestTemplate;
27
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
51     public ResponseEntity<Object> callPostRequestTarget(Object obj, String targetUrl) {
52
53         try {
54             ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.POST,
55                     new HttpEntity<>(obj, buildRequestHeader()), Object.class);
56
57             logResponseGet(targetUrl, response);
58             if (null == response) {
59                 return null;
60             } else {
61                 return response;
62             }
63
64         } catch (BackendFunctionalException | ResourceAccessException e) {
65             LOGGER.error(ERROR_ON_CALLING  + " ," + e.getMessage());
66             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
67         }
68     }
69
70
71     public ResponseEntity<Object> callGetRequestTarget(String targetUrl) {
72         try {
73             ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.GET,
74                     new HttpEntity<>(buildRequestHeader()), Object.class);
75             LOGGER.info("response status : {}", targetUrl);
76             logResponseGet(targetUrl, response);
77             if (null == response) {
78                 return null;
79             } else {
80                 return response;
81             }
82
83         } catch (BackendFunctionalException | ResourceAccessException e) {
84             LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
85             return null;
86         }
87     }
88
89     public ResponseEntity<Object> callDeleteRequestTarget(String targetUrl) {
90         try {
91             ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.DELETE,
92                     new HttpEntity<>(buildRequestHeader()), Object.class);
93             LOGGER.info("response status ewhost : {}", targetUrl);
94
95             if (null == response) {
96                 return null;
97             } else {
98                 return response;
99             }
100
101         } catch (BackendFunctionalException | ResourceAccessException e) {
102             LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
103             return null;
104         }
105     }
106     private void logResponseGet(String url, ResponseEntity<Object> response) {
107         if (response != null) {
108             if (LOGGER.isDebugEnabled()) {
109                 LOGGER.debug("response body : {}", response.getBody().toString());
110             }
111             if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals( HttpStatus.OK)) {
112                 LOGGER.warn("HTTP call EWInterface on {} returns {} , {}", url, response.getStatusCodeValue(),
113                         response.getBody().toString());
114             }
115         } else {
116             LOGGER.info("no response calling url {}", url);
117         }
118     }
119
120     private HttpHeaders buildRequestHeader() {
121         HttpHeaders httpHeaders = new HttpHeaders();
122         httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
123         httpHeaders.add(X_FROM_APP_ID, soApiId);
124         httpHeaders.add("Accept", "application/json");
125         httpHeaders.add("Content-Type", "application/json");
126         return httpHeaders;
127     }
128 }