49447f6605b5ab7b27ff8e0490b32e61de49225a
[ccsdk/features.git] / sdnr / wt / common / src / main / java / org / onap / ccsdk / features / sdnr / wt / common / database / responses / BaseResponse.java
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.common.database.responses;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Scanner;
23
24 import org.elasticsearch.client.Response;
25 import org.json.JSONObject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class BaseResponse {
30         private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class);
31         private int responseCode;
32
33         public int getResponseCode() {
34                 return this.responseCode;
35         }
36         public boolean isResponseSucceeded() {
37                 return this.responseCode<300;
38         }
39         public BaseResponse() {
40         }
41         public BaseResponse(Response response) {
42                 this.responseCode = response!=null?response.getStatusLine().getStatusCode():0;
43         }
44         protected JSONObject getJson(Response response) {
45                 Scanner s;
46                 JSONObject o=null;
47                 try {
48                         //input stream used because Scanner ignored whitespaces
49                         InputStream is = response.getEntity().getContent();
50                         long len;
51                         int BUFSIZE=1024;
52                         byte[] buffer = new byte[BUFSIZE];
53                         StringBuffer sresponse = new StringBuffer();
54                          while (true) {
55                  len = is.read(buffer, 0, BUFSIZE);
56                  if (len <= 0) {
57                      break;
58                  }
59
60                  sresponse.append(new String(buffer));
61              }
62
63                         LOG.debug("parsing response={}",sresponse);
64                         o = new JSONObject(sresponse.toString());
65                 } catch (UnsupportedOperationException | IOException e) {
66                         LOG.warn("error parsing es response: {}",e.getMessage());
67                 }
68
69                 return o;
70         }
71         protected JSONObject getJson(String json) {
72                 return new JSONObject(json);
73         }
74
75 }