Adding rest service for so monitoring
[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.slf4j.ext.XLogger;
25 import org.slf4j.ext.XLoggerFactory;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpMethod;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.web.client.HttpClientErrorException;
31 import org.springframework.web.client.RestClientException;
32 import org.springframework.web.client.RestTemplate;
33
34 import com.google.common.base.Optional;
35
36 /**
37  * @author waqas.ikram@ericsson.com
38  */
39 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
40
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(HttpRestServiceProviderImpl.class);
42
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                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: {}", url,
56                         response.getStatusCode());
57                 return Optional.absent();
58             }
59
60             if (response.hasBody()) {
61                 return Optional.of(response.getBody());
62             }
63         } catch (final HttpClientErrorException httpClientErrorException) {
64             LOGGER.error("Unable to invoke HTTP GET using url: {}, Response: {}", url,
65                     httpClientErrorException.getRawStatusCode(), httpClientErrorException);
66             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
67             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
68                 throw new InvalidRestRequestException("No result found for given url: " + url);
69             }
70             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
71
72         } catch (final RestClientException restClientException) {
73             LOGGER.error("Unable to invoke HTTP GET using url: {}", url, restClientException);
74             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url, restClientException);
75         }
76
77         return Optional.absent();
78     }
79
80     @Override
81     public <T> Optional<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
82         try {
83             final HttpEntity<?> request = new HttpEntity<>(object);
84             final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, request, clazz);
85             if (!response.getStatusCode().equals(HttpStatus.OK)) {
86                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: {}", url,
87                         response.getStatusCode());
88                 return Optional.absent();
89             }
90
91             if (response.hasBody()) {
92                 return Optional.of(response.getBody());
93             }
94
95         } catch (final HttpClientErrorException httpClientErrorException) {
96             LOGGER.error("Unable to invoke HTTP POST using url: {}, Response: {}", url,
97                     httpClientErrorException.getRawStatusCode(), httpClientErrorException);
98             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
99             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
100                 throw new InvalidRestRequestException("No result found for given url: " + url);
101             }
102             throw new RestProcessingException("Unable to invoke HTTP POST using URL: " + url);
103
104         } catch (final RestClientException restClientException) {
105             LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
106             throw new RestProcessingException("Unable to invoke HTTP POST using URL: " + url, restClientException);
107         }
108
109         return Optional.absent();
110     }
111
112
113 }