2 * ============LICENSE_START========================================================================
\r
3 * ONAP : tr-069-adapter
\r
4 * =================================================================================================
\r
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
\r
6 * =================================================================================================
\r
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
\r
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
\r
9 * may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
\r
14 * either express or implied. See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
16 * ===============LICENSE_END=======================================================================
\r
19 package org.commscope.tr069adapter.vesagent.http;
\r
21 import java.io.BufferedReader;
\r
22 import java.io.InputStreamReader;
\r
23 import java.io.OutputStreamWriter;
\r
24 import java.net.HttpURLConnection;
\r
25 import java.net.URL;
\r
26 import java.nio.charset.StandardCharsets;
\r
28 import org.commscope.tr069adapter.mapper.model.VESNotificationResponse;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
31 import org.springframework.http.HttpStatus;
\r
32 import org.springframework.stereotype.Component;
\r
35 public class HttpRequestSender {
\r
37 private HttpRequestSender() {}
\r
39 private static final String CONTENT = "Content-Type";
\r
40 private static final String ACCEPT = "Accept";
\r
41 private static final String JSON = "application/json";
\r
42 private static final Logger logger = LoggerFactory.getLogger(HttpRequestSender.class);
\r
44 public VESNotificationResponse postRequest(String requestUrl, String requestBody) {
\r
45 logger.debug("Event lister URL : {}", requestUrl);
\r
46 logger.debug("Event Data : {}", requestBody);
\r
47 String response = "";
\r
48 int responseCode = 0;
\r
49 HttpURLConnection connection = null;
\r
50 BufferedReader br = null;
\r
52 URL url = new URL(requestUrl);
\r
53 connection = (HttpURLConnection) url.openConnection();
\r
54 connection.setDoOutput(true);
\r
55 connection.setDoInput(true);
\r
56 connection.setRequestProperty(CONTENT, JSON);
\r
57 connection.setRequestProperty(ACCEPT, JSON);
\r
58 connection.setRequestMethod("POST");
\r
59 OutputStreamWriter writer =
\r
60 new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
\r
61 writer.write(requestBody);
\r
63 br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
\r
65 responseCode = connection.getResponseCode();
\r
66 logger.debug("Response received from ves collector : {}", responseCode);
\r
67 while ((temp = br.readLine()) != null) {
\r
68 response = response.concat(temp);
\r
71 if (response == null || response.length() <= 0) {
\r
72 response = String.valueOf(responseCode);
\r
75 } catch (Exception e) {
\r
76 logger.error("Exception occurred while posting the message {}", e.getMessage());
\r
77 response = "posting message failed";
\r
78 return new VESNotificationResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), response);
\r
83 if (connection != null)
\r
84 connection.disconnect();
\r
85 } catch (Exception exToIgnore) {
\r
86 logger.debug("Exception occurred while closing the connection {}", exToIgnore.toString());
\r
89 return new VESNotificationResponse(HttpStatus.valueOf(responseCode).value(), response);
\r