adae4ec55d7bd9fb58a60dcbfa29dd242e8d4c37
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceinventory / BaseClient.java
1 /**
2  *     Copyright (c) 2018 Orange
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 package org.onap.nbi.apis.serviceinventory;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.http.HttpEntity;
22 import org.springframework.http.HttpHeaders;
23 import org.springframework.http.HttpMethod;
24 import org.springframework.http.HttpStatus;
25 import org.springframework.http.ResponseEntity;
26 import org.springframework.http.converter.StringHttpMessageConverter;
27 import org.springframework.web.client.RestTemplate;
28
29 public abstract class BaseClient {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(BaseClient.class);
32
33
34     @Autowired
35     private RestTemplate restTemplate;
36
37
38
39     protected ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders) {
40
41
42         if(LOGGER.isDebugEnabled()){
43             LOGGER.debug("log request : "+callURL+ " "+httpHeaders);
44         }
45
46         ResponseEntity<Object> response = null;
47             response = restTemplate.exchange(callURL, HttpMethod.GET,
48                     new HttpEntity<>("parameters", httpHeaders), Object.class);
49
50         if(LOGGER.isDebugEnabled()){
51             LOGGER.debug("response body : {}",response.getBody().toString());
52         }
53         LOGGER.info("response status : {}", response.getStatusCodeValue());
54         if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
55             LOGGER.warn("HTTP call on {} returns {}, {}", callURL , response.getStatusCodeValue() ,response.getBody().toString());
56         }
57         return response;
58     }
59
60
61     protected ResponseEntity<String> callApiGetHealthCheck(String callURL, HttpHeaders httpHeaders) {
62
63
64         if(LOGGER.isDebugEnabled()){
65             LOGGER.debug("log request : "+callURL+ " "+httpHeaders);
66         }
67
68         ResponseEntity<String> response = null;
69         response = restTemplate.exchange(callURL, HttpMethod.GET,
70             new HttpEntity<>("parameters", httpHeaders), String.class);
71
72         if(LOGGER.isDebugEnabled()){
73             LOGGER.debug("response body : {}",response.getBody().toString());
74         }
75         LOGGER.info("response status : {}", response.getStatusCodeValue());
76         if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
77             LOGGER.warn("HTTP call on {} returns {}, {}", callURL , response.getStatusCodeValue() ,response.getBody().toString());
78         }
79         return response;
80     }
81
82 }