a3a1b16e1bdf9eb9153c16d81ac54dfecc2a1467
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.database.responses;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.http.util.EntityUtils;
29 import org.elasticsearch.client.Response;
30 import org.json.JSONObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class BaseResponse {
35         private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class);
36
37         private final int responseCode;
38
39         BaseResponse(Response response) {
40                 this.responseCode = response != null ? response.getStatusLine().getStatusCode() : 0;
41         }
42
43         int getResponseCode() {
44                 return this.responseCode;
45         }
46
47         public boolean isResponseSucceeded() {
48                 return this.responseCode < 300;
49         }
50
51         JSONObject getJson(Response response) {
52                 if(response==null) {
53                         LOG.warn("unable to parse response. response is null.");
54                         return null;
55                 }
56                 try {
57                         String sresponse = EntityUtils.toString(response.getEntity());
58                         LOG.debug("parsing response={}", sresponse);
59                         return new JSONObject(sresponse);
60                 } catch (UnsupportedOperationException | IOException e) {
61                         LOG.warn("error parsing es response: {}", e.getMessage());
62                         return null;
63                 }
64
65         }
66
67         JSONObject getJson(String json) {
68                 return new JSONObject(json);
69         }
70
71         /**
72          * @param response
73          * @return
74          */
75         List<String> getLines(Response response){
76                 return this.getLines(response,true);
77         }
78         List<String> getLines(Response response,boolean ignoreEmpty) {
79                 try {
80                         String sresponse = EntityUtils.toString(response.getEntity());
81                         LOG.debug("parsing response={}", sresponse);
82                         String[] hlp = sresponse.split("\n");
83                         List<String> lines=new ArrayList<String>();
84                         for(String h:hlp) {
85                                 if(ignoreEmpty && h.trim().length()==0) {
86                                         continue;
87                                 }
88                                 lines.add(h);
89                         }
90                         return lines;
91                 } catch (UnsupportedOperationException | IOException e) {
92                         LOG.warn("error parsing es response: {}", e.getMessage());
93                         return null;
94                 }
95
96         }
97 }