01219f0f01eb1d6f9b9dc8cdc51e26ef9e1302b2
[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.common.database.responses;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.apache.http.util.EntityUtils;
26 import org.elasticsearch.client.Response;
27 import org.json.JSONObject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class BaseResponse {
32         private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class);
33
34         private final int responseCode;
35
36         BaseResponse(Response response) {
37                 this.responseCode = response != null ? response.getStatusLine().getStatusCode() : 0;
38         }
39
40         int getResponseCode() {
41                 return this.responseCode;
42         }
43
44         public boolean isResponseSucceeded() {
45                 return this.responseCode < 300;
46         }
47
48         JSONObject getJson(Response response) {
49                 try {
50                         String sresponse = EntityUtils.toString(response.getEntity());
51                         LOG.debug("parsing response={}", sresponse);
52                         return new JSONObject(sresponse);
53                 } catch (UnsupportedOperationException | IOException e) {
54                         LOG.warn("error parsing es response: {}", e.getMessage());
55                         return null;
56                 }
57
58         }
59
60         JSONObject getJson(String json) {
61                 return new JSONObject(json);
62         }
63
64         /**
65          * @param response
66          * @return
67          */
68         List<String> getLines(Response response){
69                 return this.getLines(response,true);
70         }
71         List<String> getLines(Response response,boolean ignoreEmpty) {
72                 try {
73                         String sresponse = EntityUtils.toString(response.getEntity());
74                         LOG.debug("parsing response={}", sresponse);
75                         String[] hlp = sresponse.split("\n");
76                         List<String> lines=new ArrayList<String>();
77                         for(String h:hlp) {
78                                 if(ignoreEmpty && h.trim().length()==0) {
79                                         continue;
80                                 }
81                                 lines.add(h);
82                         }
83                         return lines;
84                 } catch (UnsupportedOperationException | IOException e) {
85                         LOG.warn("error parsing es response: {}", e.getMessage());
86                         return null;
87                 }
88
89         }
90 }