2 * ============LICENSE_START=======================================================
3 * ONAP : CCSDK.apps.sdnr.wt.apigateway
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=========================================================
21 package org.onap.ccsdk.features.sdnr.wt.apigateway.database;
23 import java.io.IOException;
24 import java.util.HashMap;
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPClient;
30 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class DatabaseHttpClient extends BaseHTTPClient {
36 private static Logger LOG = LoggerFactory.getLogger(DatabaseHttpClient.class);
37 private static final String URI = "/mwtn/mediator-server/_search";
38 private final Map<String, String> headers;
40 public DatabaseHttpClient(String base, boolean trustAllCerts) {
41 super(base, trustAllCerts);
42 this.headers = this.getHeaders();
45 private Map<String, String> getHeaders() {
46 Map<String, String> h = new HashMap<String, String>();
51 public Map<String, MediatorServerInfo> requestEntries() {
52 Map<String, MediatorServerInfo> entries = new HashMap<String, MediatorServerInfo>();
53 BaseHTTPResponse response = null;
55 response = this.sendRequest(URI, "GET", (String) null, this.headers);
56 } catch (IOException e) {
57 LOG.warn("problem reading db entries of mediator server: {}", e.getMessage());
59 if (response != null && response.code == BaseHTTPResponse.CODE200) {
61 JSONObject o = new JSONObject(response.body);
63 JSONObject hits = o.getJSONObject("hits");
64 if (hits.has("hits")) {
65 JSONArray hitsarray = hits.getJSONArray("hits");
66 if (hitsarray.length() > 0) {
67 for (int i = 0; i < hitsarray.length(); i++) {
68 JSONObject entry = hitsarray.getJSONObject(i);
69 entries.put(entry.getString("_id"),
70 new MediatorServerInfo(entry.getJSONObject("_source").getString("name"),
71 entry.getJSONObject("_source").getString("url")));
77 } catch (Exception e) {
78 LOG.warn("problem parsing response: {} | e={}", response, e.getMessage());