JSON objects now displaying as string
[so.git] / so-monitoring / so-monitoring-handler / src / main / java / org / onap / so / montoring / rest / service / HttpRestServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.montoring.rest.service;
21
22 import org.onap.so.montoring.exception.InvalidRestRequestException;
23 import org.onap.so.montoring.exception.RestProcessingException;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpMethod;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.web.client.HttpClientErrorException;
29 import org.springframework.web.client.RestClientException;
30 import org.springframework.web.client.RestTemplate;
31
32 import com.google.common.base.Optional;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * @author waqas.ikram@ericsson.com
39  */
40 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
43     private final RestTemplate restTemplate;
44
45     public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
46         this.restTemplate = restTemplate;
47     }
48
49     @Override
50     public <T> Optional<T> getHttpResponse(final String url, final Class<T> clazz) {
51         LOGGER.trace("Will invoke HTTP GET using URL: " + url);
52         try {
53             final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, null, clazz);
54             if (!response.getStatusCode().equals(HttpStatus.OK)) {
55                 final String message = "Unable to invoke HTTP GET using URL: " + url + 
56                     ", Response Code: " + response.getStatusCode();
57                 LOGGER.error(message);
58                 return Optional.absent();
59             }
60
61             if (response.hasBody()) {
62                 return Optional.of(response.getBody());
63             }
64         } catch (final HttpClientErrorException httpClientErrorException) {
65             final String message = "Unable to invoke HTTP GET using url: " + url + ", Response: " +
66                     httpClientErrorException.getRawStatusCode();
67             LOGGER.error(message, httpClientErrorException);
68             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
69             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
70                 throw new InvalidRestRequestException("No result found for given url: " + url);
71             }
72             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
73
74         } catch (final RestClientException restClientException) {
75             LOGGER.error("Unable to invoke HTTP GET using url: " + url, restClientException);
76             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + 
77                                               url, restClientException);
78         }
79
80         return Optional.absent();
81     }
82
83     @Override
84     public <T> Optional<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
85         try {
86             final HttpEntity<?> request = new HttpEntity<>(object);
87             final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, request, clazz);
88             if (!response.getStatusCode().equals(HttpStatus.OK)) {
89                 final String message = "Unable to invoke HTTP GET using URL: " + url + 
90                     ", Response Code: " + response.getStatusCode();
91                 LOGGER.error(message);
92                 return Optional.absent();
93             }
94
95             if (response.hasBody()) {
96                 return Optional.of(response.getBody());
97             }
98
99         } catch (final HttpClientErrorException httpClientErrorException) {
100             final String message = "Unable to invoke HTTP POST using url: " + url + 
101                 ", Response: " + httpClientErrorException.getRawStatusCode();
102             LOGGER.error(message, httpClientErrorException);
103             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
104             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
105                 throw new InvalidRestRequestException("No result found for given url: " + url);
106             }
107             throw new RestProcessingException("Unable to invoke HTTP POST using URL: " + url);
108
109         } catch (final RestClientException restClientException) {
110             LOGGER.error("Unable to invoke HTTP POST using url: " + url, restClientException);
111             throw new RestProcessingException("Unable to invoke HTTP POST using URL: " 
112                                               + url, restClientException);
113         }
114
115         return Optional.absent();
116     }
117
118
119 }