--- /dev/null
+/*\r
+ * ============LICENSE_START========================================================================\r
+ * ONAP : tr-069-adapter\r
+ * =================================================================================================\r
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
+ * =================================================================================================\r
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
+ * may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ===============LICENSE_END=======================================================================\r
+ */\r
+\r
+package org.commscope.tr069adapter.vesagent.http;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.InputStreamReader;\r
+import java.io.OutputStreamWriter;\r
+import java.net.HttpURLConnection;\r
+import java.net.URL;\r
+import java.nio.charset.StandardCharsets;\r
+\r
+import org.commscope.tr069adapter.mapper.model.VESNotificationResponse;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.http.HttpStatus;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class HttpRequestSender {\r
+\r
+ private HttpRequestSender() {}\r
+\r
+ private static final String CONTENT = "Content-Type";\r
+ private static final String ACCEPT = "Accept";\r
+ private static final String JSON = "application/json";\r
+ private static final Logger logger = LoggerFactory.getLogger(HttpRequestSender.class);\r
+\r
+ public VESNotificationResponse postRequest(String requestUrl, String requestBody) {\r
+ logger.debug("Event lister URL : {}", requestUrl);\r
+ logger.debug("Event Data : {}", requestBody);\r
+ String response = "";\r
+ int responseCode = 0;\r
+ HttpURLConnection connection = null;\r
+ BufferedReader br = null;\r
+ try {\r
+ URL url = new URL(requestUrl);\r
+ connection = (HttpURLConnection) url.openConnection();\r
+ connection.setDoOutput(true);\r
+ connection.setDoInput(true);\r
+ connection.setRequestProperty(CONTENT, JSON);\r
+ connection.setRequestProperty(ACCEPT, JSON);\r
+ connection.setRequestMethod("POST");\r
+ OutputStreamWriter writer =\r
+ new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);\r
+ writer.write(requestBody);\r
+ writer.close();\r
+ br = new BufferedReader(new InputStreamReader(connection.getInputStream()));\r
+ String temp = null;\r
+ responseCode = connection.getResponseCode();\r
+ logger.debug("Response received from ves collector : {}", responseCode);\r
+ while ((temp = br.readLine()) != null) {\r
+ response = response.concat(temp);\r
+ }\r
+\r
+ if (response == null || response.length() <= 0) {\r
+ response = String.valueOf(responseCode);\r
+ }\r
+\r
+ } catch (Exception e) {\r
+ logger.error("Exception occurred while posting the message {}", e.getMessage());\r
+ response = "posting message failed";\r
+ return new VESNotificationResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), response);\r
+ } finally {\r
+ try {\r
+ if (br != null)\r
+ br.close();\r
+ if (connection != null)\r
+ connection.disconnect();\r
+ } catch (Exception exToIgnore) {\r
+ logger.debug("Exception occurred while closing the connection {}", exToIgnore.toString());\r
+ }\r
+ }\r
+ return new VESNotificationResponse(HttpStatus.valueOf(responseCode).value(), response);\r
+ }\r
+}\r