c46d912899c0775ac1ed389ed42f56decce89a23
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / utils / SonHandlerRestTemplate.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019 Wipro Limited.
6  *   ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *  
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *  
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *  
20  *******************************************************************************/
21
22 package org.onap.dcaegen2.services.sonhms.utils;
23
24 import java.util.Collections;
25
26 import org.slf4j.Logger;
27 import org.springframework.core.ParameterizedTypeReference;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpMethod;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.MediaType;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Component;
35 import org.springframework.web.client.RestTemplate;
36
37 @Component
38 public class SonHandlerRestTemplate {
39
40     private static final String AUTH = "Authorization";
41     private static final String EXCEPTION_MSG = "Exception caught during request {}";
42     private static final Logger log = org.slf4j.LoggerFactory.getLogger(SonHandlerRestTemplate.class);
43
44     private SonHandlerRestTemplate() {
45         
46     }
47     
48     /**
49      * Send Post Request.
50      */
51
52     public static <T> ResponseEntity<T> sendPostRequest(String requestUrl, String requestBody,
53             ParameterizedTypeReference<T> responseType) {
54         HttpHeaders headers = new HttpHeaders();
55         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
56         headers.setContentType(MediaType.APPLICATION_JSON);
57         HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
58         try {
59             RestTemplate restTemplate = BeanUtil.getBean(RestTemplate.class);
60             return restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, responseType);
61         } catch (Exception e) {
62             log.debug(EXCEPTION_MSG, e.getMessage());
63             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
64         }
65     }
66
67
68     /**
69      * Send Get Request.
70      */
71
72     public static <T> ResponseEntity<T> sendGetRequest(String requestUrl, ParameterizedTypeReference<T> responseType) {
73         HttpHeaders headers = new HttpHeaders();
74         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
75         HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
76         try {
77             RestTemplate restTemplate = BeanUtil.getBean(RestTemplate.class);
78             return restTemplate.exchange(requestUrl, HttpMethod.GET, requestEntity, responseType);
79         } catch (Exception e) {
80             log.debug(EXCEPTION_MSG, e.getMessage());
81             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
82         }
83     }
84
85     /**
86      * Send Get Request to SDNR.
87      */
88
89     public static <T> ResponseEntity<T> sendGetRequest(String requestUrl, String requestBody,
90             ParameterizedTypeReference<T> responseType) {
91         HttpHeaders headers = new HttpHeaders();
92         headers.setAccept(Collections.singletonList(MediaType.TEXT_PLAIN));
93         headers.setContentType(MediaType.APPLICATION_JSON);
94         headers.add(AUTH, "Basic SW5mcmFQb3J0YWxDbGllbnQ6cGFzc3dvcmQxJA==");
95         HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
96         try {
97             RestTemplate restTemplate = BeanUtil.getBean(RestTemplate.class);
98             return restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, responseType);
99         } catch (Exception e) {
100             log.debug(EXCEPTION_MSG, e.getMessage());
101             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
102         }
103     }
104
105     /**
106      * Send Post Request to oof.
107      */
108
109     public static <T> ResponseEntity<T> sendPostRequestToOof(String requestUrl, String requestBody,
110             ParameterizedTypeReference<T> responseType) {
111         HttpHeaders headers = new HttpHeaders();
112         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
113         headers.setContentType(MediaType.APPLICATION_JSON);
114         headers.add(AUTH, "Basic cGNpX3Rlc3Q6cGNpX3Rlc3Rwd2Q=");
115         HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
116         try {
117             RestTemplate restTemplate = BeanUtil.getBean(RestTemplate.class);
118             return restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, responseType);
119         } catch (Exception e) {
120             log.debug(EXCEPTION_MSG, e);
121             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
122         }
123     }
124
125 }