2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.database.responses;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
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;
34 public class BaseResponse {
36 private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class);
38 private final int responseCode;
40 BaseResponse(Response response) {
41 this.responseCode = response != null ? response.getStatusLine().getStatusCode() : 0;
44 int getResponseCode() {
45 return this.responseCode;
48 public boolean isResponseSucceeded() {
49 return this.responseCode < 300 && this.responseCode >= 200;
52 JSONObject getJson(Response response) {
54 String sresponse = EntityUtils.toString(response.getEntity());
55 LOG.debug("parsing response={}", sresponse);
56 return new JSONObject(sresponse);
57 } catch (UnsupportedOperationException | IOException e) {
58 LOG.warn("error parsing es response: {}", e.getMessage());
64 JSONObject getJson(String json) {
65 return new JSONObject(json);
72 List<String> getLines(Response response) {
73 return this.getLines(response, true);
76 List<String> getLines(Response response, boolean ignoreEmpty) {
78 String sresponse = EntityUtils.toString(response.getEntity());
79 LOG.debug("parsing response={}", sresponse);
80 String[] hlp = sresponse.split("\n");
81 List<String> lines = new ArrayList<String>();
82 for (String h : hlp) {
83 if (ignoreEmpty && h.trim().length() == 0) {
89 } catch (UnsupportedOperationException | IOException e) {
90 LOG.warn("error parsing es response: {}", e.getMessage());