ed09d34cd664366ad335fac626abfbf8fb3cdea6
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.net.HttpURLConnection;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import org.json.JSONObject;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 public class HtDatabaseWebAPIClient {
32
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;
38
39     public HtDatabaseWebAPIClient() {
40         this.host = "http://localhost";
41         this.port = 9200;
42     }
43
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;
48
49         if (body != null) {
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");
60                 // send request
61                 // Send the message to destination
62                 if (!method.equals("GET")) {
63                     try (OutputStream output = http.getOutputStream()) {
64                         output.write(body.toString().getBytes(CHARSET));
65                     }
66                 }
67                 responseCode = http.getResponseCode();
68                 // Receive answer
69                 if (responseCode >= 200 && responseCode < 300) {
70                     response = http.getInputStream();
71                 } else {
72                     response = http.getErrorStream();
73                     if (response == null) {
74                         http.getInputStream();
75                     }
76                 }
77             }
78             byte[] buffer = new byte[BUFSIZE];
79             int len = 0;
80             if (response != null) {
81                 while (true) {
82                     len = response.read(buffer, 0, BUFSIZE);
83                     if (len <= 0) {
84                         break;
85                     }
86                     sresponse += new String(buffer, 0, len, CHARSET);
87                 }
88                 response.close();
89             } else {
90                 LOG.debug("response is null");
91             }
92         }
93         LOG.debug("ResponseCode: " + responseCode);
94         LOG.trace("Response: " + sresponse);
95
96         return sresponse;
97     }
98
99         public void insertEntry(String index, String type, JSONObject data) throws IOException {
100                 this.sendRequest(String.format("/%s/%s/", index,type), "POST", data);
101         }
102 }