X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ves-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Fcommscope%2Ftr069adapter%2Fvesagent%2Fhttp%2FHttpRequestSender.java;fp=ves-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Fcommscope%2Ftr069adapter%2Fvesagent%2Fhttp%2FHttpRequestSender.java;h=fab8c78fd1744507241b25f7efca2b401a5ada66;hb=a58ada8fd244e69cf2ebe48a251fcdd4d48acec4;hp=0000000000000000000000000000000000000000;hpb=5096ffe11e0c38f2a1cc60ccba41d27c1333f22a;p=oam%2Ftr069-adapter.git diff --git a/ves-agent/src/main/java/org/commscope/tr069adapter/vesagent/http/HttpRequestSender.java b/ves-agent/src/main/java/org/commscope/tr069adapter/vesagent/http/HttpRequestSender.java new file mode 100644 index 0000000..fab8c78 --- /dev/null +++ b/ves-agent/src/main/java/org/commscope/tr069adapter/vesagent/http/HttpRequestSender.java @@ -0,0 +1,91 @@ +/* + * ============LICENSE_START======================================================================== + * ONAP : tr-069-adapter + * ================================================================================================= + * Copyright (C) 2020 CommScope Inc Intellectual Property. + * ================================================================================================= + * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You + * may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + * ===============LICENSE_END======================================================================= + */ + +package org.commscope.tr069adapter.vesagent.http; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +import org.commscope.tr069adapter.mapper.model.VESNotificationResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; + +@Component +public class HttpRequestSender { + + private HttpRequestSender() {} + + private static final String CONTENT = "Content-Type"; + private static final String ACCEPT = "Accept"; + private static final String JSON = "application/json"; + private static final Logger logger = LoggerFactory.getLogger(HttpRequestSender.class); + + public VESNotificationResponse postRequest(String requestUrl, String requestBody) { + logger.debug("Event lister URL : {}", requestUrl); + logger.debug("Event Data : {}", requestBody); + String response = ""; + int responseCode = 0; + HttpURLConnection connection = null; + BufferedReader br = null; + try { + URL url = new URL(requestUrl); + connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setRequestProperty(CONTENT, JSON); + connection.setRequestProperty(ACCEPT, JSON); + connection.setRequestMethod("POST"); + OutputStreamWriter writer = + new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); + writer.write(requestBody); + writer.close(); + br = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String temp = null; + responseCode = connection.getResponseCode(); + logger.debug("Response received from ves collector : {}", responseCode); + while ((temp = br.readLine()) != null) { + response = response.concat(temp); + } + + if (response == null || response.length() <= 0) { + response = String.valueOf(responseCode); + } + + } catch (Exception e) { + logger.error("Exception occurred while posting the message {}", e.getMessage()); + response = "posting message failed"; + return new VESNotificationResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), response); + } finally { + try { + if (br != null) + br.close(); + if (connection != null) + connection.disconnect(); + } catch (Exception exToIgnore) { + logger.debug("Exception occurred while closing the connection {}", exToIgnore.toString()); + } + } + return new VESNotificationResponse(HttpStatus.valueOf(responseCode).value(), response); + } +}