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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.database.responses;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
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;
31 public class BaseResponse {
32 private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class);
34 private final int responseCode;
36 BaseResponse(Response response) {
37 this.responseCode = response != null ? response.getStatusLine().getStatusCode() : 0;
40 int getResponseCode() {
41 return this.responseCode;
44 public boolean isResponseSucceeded() {
45 return this.responseCode < 300;
48 JSONObject getJson(Response response) {
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());
60 JSONObject getJson(String json) {
61 return new JSONObject(json);
68 List<String> getLines(Response response){
69 return this.getLines(response,true);
71 List<String> getLines(Response response,boolean ignoreEmpty) {
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>();
78 if(ignoreEmpty && h.trim().length()==0) {
84 } catch (UnsupportedOperationException | IOException e) {
85 LOG.warn("error parsing es response: {}", e.getMessage());