7777aaa2bae75cf1bb0ff1081b41fa7967a6016e
[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 @Deprecated
35 public class BaseResponse {
36
37     private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class);
38
39     private final int responseCode;
40
41     BaseResponse(Response response) {
42         this.responseCode = response != null ? response.getStatusLine().getStatusCode() : 0;
43     }
44
45     int getResponseCode() {
46         return this.responseCode;
47     }
48
49     public boolean isResponseSucceeded() {
50         return this.responseCode < 300 && this.responseCode >= 200;
51     }
52
53     JSONObject getJson(Response response) {
54         try {
55             String sresponse = EntityUtils.toString(response.getEntity());
56             LOG.debug("parsing response={}", sresponse);
57             return new JSONObject(sresponse);
58         } catch (UnsupportedOperationException | IOException e) {
59             LOG.warn("error parsing es response: {}", e.getMessage());
60             return null;
61         }
62
63     }
64
65     JSONObject getJson(String json) {
66         return new JSONObject(json);
67     }
68
69     /**
70      * @param response
71      * @return
72      */
73     List<String> getLines(Response response) {
74         return this.getLines(response, true);
75     }
76
77     List<String> getLines(Response response, boolean ignoreEmpty) {
78         try {
79             String sresponse = EntityUtils.toString(response.getEntity());
80             LOG.debug("parsing response={}", sresponse);
81             String[] hlp = sresponse.split("\n");
82             List<String> lines = new ArrayList<String>();
83             for (String h : hlp) {
84                 if (ignoreEmpty && h.trim().length() == 0) {
85                     continue;
86                 }
87                 lines.add(h);
88             }
89             return lines;
90         } catch (UnsupportedOperationException | IOException e) {
91             LOG.warn("error parsing es response: {}", e.getMessage());
92             return null;
93         }
94
95     }
96
97
98 }