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;
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;
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";
50 public ResponseEntity<Object> callPostRequestTarget(Object obj, String targetUrl) {
53 ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.POST,
54 new HttpEntity<>(obj, buildRequestHeader()), Object.class);
56 logResponseGet(targetUrl, response);
57 if (null == response) {
63 } catch (BackendFunctionalException | ResourceAccessException e) {
64 LOGGER.error(ERROR_ON_CALLING + " ," + e.getMessage());
65 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
69 public ResponseEntity<Object> callGetRequestTarget(String targetUrl) {
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) {
81 } catch (BackendFunctionalException | ResourceAccessException e) {
82 LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
87 public ResponseEntity<Object> callDeleteRequestTarget(String targetUrl) {
89 ResponseEntity<Object> response = restTemplate.exchange(targetUrl, HttpMethod.DELETE,
90 new HttpEntity<>(buildRequestHeader()), Object.class);
91 LOGGER.info("response status ewhost : {}", targetUrl);
93 if (null == response) {
99 } catch (BackendFunctionalException | ResourceAccessException e) {
100 LOGGER.error(ERROR_ON_CALLING + targetUrl + " ," + e);
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());
110 if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
111 LOGGER.warn("HTTP call EWInterface on {} returns {} , {}", url, response.getStatusCodeValue(),
112 response.getBody().toString());
115 LOGGER.info("no response calling url {}", url);
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");