2 * Copyright (c) 2019 Vodafone Group
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
30 public class EWInterfaceUtils {
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);
37 private RestTemplate restTemplate;
39 private String soHostname;
41 @Value("${so.api.id}")
42 private String soApiId;
44 @Value("${so.header.authorization}")
45 private String soHeaderAuthorization;
47 private static final String HEADER_AUTHORIZATION = "Authorization";
48 private static final String X_FROM_APP_ID = "X-FromAppId";
51 public ResponseEntity<Object> callPostRequestTarget(Object obj, String targetUrl) {
54 ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.POST,
55 new HttpEntity<>(obj, buildRequestHeader()), Object.class);
57 logResponseGet(targetUrl, response);
58 if (null == response) {
64 } catch (BackendFunctionalException | ResourceAccessException e) {
65 LOGGER.error(ERROR_ON_CALLING + " ," + e.getMessage());
66 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
71 public ResponseEntity<Object> callGetRequestTarget(String targetUrl) {
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) {
83 } catch (BackendFunctionalException | ResourceAccessException e) {
84 LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
89 public ResponseEntity<Object> callDeleteRequestTarget(String targetUrl) {
91 ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.DELETE,
92 new HttpEntity<>(buildRequestHeader()), Object.class);
93 LOGGER.info("response status ewhost : {}", targetUrl);
95 if (null == response) {
101 } catch (BackendFunctionalException | ResourceAccessException e) {
102 LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
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());
111 if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals( HttpStatus.OK)) {
112 LOGGER.warn("HTTP call EWInterface on {} returns {} , {}", url, response.getStatusCodeValue(),
113 response.getBody().toString());
116 LOGGER.info("no response calling url {}", url);
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");