1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.net.HttpURLConnection;
25 import java.net.URLConnection;
26 import org.json.JSONObject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class HtDatabaseWebAPIClient {
33 private static Logger LOG = LoggerFactory.getLogger(HtDatabaseWebAPIClient.class);
34 private static final int BUFSIZE = 1024;
35 private static final String CHARSET = "UTF-8";
36 private final String host;
37 private final int port;
39 public HtDatabaseWebAPIClient() {
40 this.host = "http://localhost";
44 public String sendRequest(String uri, String method, JSONObject body) throws IOException {
45 LOG.debug("try to send request with uri=" + uri + " as method=" + method);
46 String sresponse = "";
47 int responseCode = -1;
50 LOG.trace("body: {}", body);
51 InputStream response = null;
52 String surl = String.format("%s:%d%s", this.host, this.port, uri);
53 URL url = new URL(surl);
54 URLConnection urlConnection = url.openConnection();
55 if (urlConnection instanceof HttpURLConnection) {
56 HttpURLConnection http = (HttpURLConnection) urlConnection;
57 http.setRequestMethod(method);
58 http.setDoOutput(true);
59 http.setRequestProperty("Content-Type", "application/json");
61 // Send the message to destination
62 if (!method.equals("GET")) {
63 try (OutputStream output = http.getOutputStream()) {
64 output.write(body.toString().getBytes(CHARSET));
67 responseCode = http.getResponseCode();
69 if (responseCode >= 200 && responseCode < 300) {
70 response = http.getInputStream();
72 response = http.getErrorStream();
73 if (response == null) {
74 http.getInputStream();
78 byte[] buffer = new byte[BUFSIZE];
80 if (response != null) {
82 len = response.read(buffer, 0, BUFSIZE);
86 sresponse += new String(buffer, 0, len, CHARSET);
90 LOG.debug("response is null");
93 LOG.debug("ResponseCode: " + responseCode);
94 LOG.trace("Response: " + sresponse);
99 public void insertEntry(String index, String type, JSONObject data) throws IOException {
100 this.sendRequest(String.format("/%s/%s/", index,type), "POST", data);