2015-04-13 08:28:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Cloudius Systems
|
|
|
|
*/
|
2015-12-17 08:26:19 +01:00
|
|
|
package com.scylladb.jmx.api;
|
2015-04-13 08:28:47 +02:00
|
|
|
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.net.InetAddress;
|
2015-05-26 11:31:38 +02:00
|
|
|
import java.net.UnknownHostException;
|
2015-05-19 16:50:40 +02:00
|
|
|
import java.util.ArrayList;
|
2015-04-13 08:28:47 +02:00
|
|
|
import java.util.HashMap;
|
2015-06-17 14:31:18 +02:00
|
|
|
import java.util.HashSet;
|
2015-04-13 08:28:47 +02:00
|
|
|
import java.util.Iterator;
|
2016-12-28 11:45:33 +01:00
|
|
|
import java.util.LinkedHashMap;
|
2015-04-13 08:28:47 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2015-10-28 10:25:37 +01:00
|
|
|
import java.util.Map.Entry;
|
2015-06-17 14:31:18 +02:00
|
|
|
import java.util.Set;
|
2016-10-11 14:10:19 +02:00
|
|
|
import java.util.function.BiFunction;
|
|
|
|
import java.util.logging.Logger;
|
2015-05-25 15:40:37 +02:00
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
import javax.json.Json;
|
|
|
|
import javax.json.JsonArray;
|
|
|
|
import javax.json.JsonObject;
|
|
|
|
import javax.json.JsonReader;
|
|
|
|
import javax.json.JsonReaderFactory;
|
|
|
|
import javax.json.JsonString;
|
|
|
|
import javax.management.openmbean.TabularData;
|
2015-05-26 11:31:38 +02:00
|
|
|
import javax.management.openmbean.TabularDataSupport;
|
2016-03-11 13:00:28 +01:00
|
|
|
import javax.ws.rs.ProcessingException;
|
2015-10-28 10:25:37 +01:00
|
|
|
import javax.ws.rs.client.Client;
|
|
|
|
import javax.ws.rs.client.ClientBuilder;
|
|
|
|
import javax.ws.rs.client.Entity;
|
|
|
|
import javax.ws.rs.client.Invocation;
|
|
|
|
import javax.ws.rs.client.WebTarget;
|
2015-10-06 11:42:16 +02:00
|
|
|
import javax.ws.rs.core.MediaType;
|
2015-05-25 15:40:37 +02:00
|
|
|
import javax.ws.rs.core.MultivaluedMap;
|
2015-10-28 10:25:37 +01:00
|
|
|
import javax.ws.rs.core.Response;
|
2015-12-17 08:26:19 +01:00
|
|
|
|
2015-10-28 10:25:37 +01:00
|
|
|
import org.glassfish.jersey.client.ClientConfig;
|
2015-12-17 08:26:19 +01:00
|
|
|
|
2019-07-24 16:28:38 +02:00
|
|
|
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
|
2015-12-17 08:26:19 +01:00
|
|
|
import com.scylladb.jmx.utils.SnapshotDetailsTabularData;
|
2015-04-13 08:28:47 +02:00
|
|
|
|
|
|
|
public class APIClient {
|
2016-10-11 14:10:19 +02:00
|
|
|
private Map<String, CacheEntry> cache = new HashMap<String, CacheEntry>();
|
2019-11-19 16:28:09 +01:00
|
|
|
private final APIConfig config;
|
|
|
|
private final ClientConfig clientConfig;
|
|
|
|
private final Client client;
|
|
|
|
private JsonReaderFactory factory = Json.createReaderFactory(null);
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(APIClient.class.getName());
|
|
|
|
|
|
|
|
public APIClient(APIConfig config) {
|
|
|
|
this.config = config;
|
|
|
|
this.clientConfig = new ClientConfig();
|
|
|
|
clientConfig.register(new JacksonJaxbJsonProvider());
|
|
|
|
this.client = ClientBuilder.newClient(clientConfig);
|
|
|
|
}
|
2016-10-11 14:10:19 +02:00
|
|
|
|
|
|
|
private String getCacheKey(String key, MultivaluedMap<String, String> param, long duration) {
|
2015-10-06 11:42:16 +02:00
|
|
|
if (duration <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (param != null) {
|
|
|
|
StringBuilder sb = new StringBuilder(key);
|
|
|
|
sb.append("?");
|
|
|
|
for (String k : param.keySet()) {
|
|
|
|
sb.append(k).append('=').append(param.get(k)).append('&');
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
2015-10-21 10:33:00 +02:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
private String getStringFromCache(String key, long duration) {
|
2015-10-21 10:33:00 +02:00
|
|
|
if (key == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
CacheEntry value = cache.get(key);
|
2016-10-11 14:10:19 +02:00
|
|
|
return (value != null && value.valid(duration)) ? value.stringValue() : null;
|
2015-10-21 10:33:00 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
private JsonObject getJsonObjectFromCache(String key, long duration) {
|
2016-05-17 08:35:42 +02:00
|
|
|
if (key == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
CacheEntry value = cache.get(key);
|
2016-10-11 14:10:19 +02:00
|
|
|
return (value != null && value.valid(duration)) ? value.jsonObject() : null;
|
2016-05-17 08:35:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
private String getBaseUrl() {
|
|
|
|
return config.getBaseUrl();
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 10:25:37 +01:00
|
|
|
public Invocation.Builder get(String path, MultivaluedMap<String, String> queryParams) {
|
|
|
|
WebTarget webTarget = client.target(getBaseUrl()).path(path);
|
|
|
|
if (queryParams != null) {
|
2016-10-11 14:10:19 +02:00
|
|
|
for (Entry<String, List<String>> qp : queryParams.entrySet()) {
|
2015-10-28 10:25:37 +01:00
|
|
|
for (String e : qp.getValue()) {
|
|
|
|
webTarget = webTarget.queryParam(qp.getKey(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return webTarget.request(MediaType.APPLICATION_JSON);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 10:25:37 +01:00
|
|
|
public Invocation.Builder get(String path) {
|
|
|
|
return get(path, null);
|
2015-05-25 15:40:37 +02:00
|
|
|
}
|
|
|
|
|
2015-10-28 10:25:37 +01:00
|
|
|
public Response post(String path, MultivaluedMap<String, String> queryParams) {
|
2016-08-17 10:53:09 +02:00
|
|
|
return post(path, queryParams, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Response post(String path, MultivaluedMap<String, String> queryParams, Object object, String type) {
|
2016-08-19 08:52:05 +02:00
|
|
|
try {
|
2016-08-17 10:53:09 +02:00
|
|
|
Response response = get(path, queryParams).post(Entity.entity(object, type));
|
2016-10-11 14:10:19 +02:00
|
|
|
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
|
|
|
|
throw getException("Scylla API server HTTP POST to URL '" + path + "' failed",
|
|
|
|
response.readEntity(String.class));
|
2016-08-19 08:52:05 +02:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
} catch (ProcessingException e) {
|
|
|
|
throw new IllegalStateException("Unable to connect to Scylla API server: " + e.getMessage());
|
2015-05-25 15:40:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 10:53:09 +02:00
|
|
|
public Response post(String path, MultivaluedMap<String, String> queryParams, Object object) {
|
|
|
|
return post(path, queryParams, object, MediaType.TEXT_PLAIN);
|
|
|
|
}
|
2016-10-11 14:10:19 +02:00
|
|
|
|
2015-05-26 11:31:38 +02:00
|
|
|
public void post(String path) {
|
|
|
|
post(path, null);
|
|
|
|
}
|
|
|
|
|
2016-03-15 09:58:20 +01:00
|
|
|
public IllegalStateException getException(String msg, String json) {
|
|
|
|
JsonReader reader = factory.createReader(new StringReader(json));
|
2015-10-28 10:25:37 +01:00
|
|
|
JsonObject res = reader.readObject();
|
2016-03-15 09:58:20 +01:00
|
|
|
return new IllegalStateException(msg + ": " + res.getString("message"));
|
2015-10-28 10:25:37 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 19:44:24 +02:00
|
|
|
public String postGetVal(String path, MultivaluedMap<String, String> queryParams) {
|
2015-10-28 10:25:37 +01:00
|
|
|
return post(path, queryParams).readEntity(String.class);
|
2015-08-27 19:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int postInt(String path, MultivaluedMap<String, String> queryParams) {
|
|
|
|
return Integer.parseInt(postGetVal(path, queryParams));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int postInt(String path) {
|
|
|
|
return postInt(path, null);
|
|
|
|
}
|
|
|
|
|
2015-05-26 11:31:38 +02:00
|
|
|
public void delete(String path, MultivaluedMap<String, String> queryParams) {
|
|
|
|
if (queryParams != null) {
|
|
|
|
get(path, queryParams).delete();
|
|
|
|
return;
|
|
|
|
}
|
2019-06-18 15:53:12 +02:00
|
|
|
Response response = get(path).delete();
|
|
|
|
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
|
|
|
|
throw getException("Scylla API server HTTP delete to URL '" + path + "' failed",
|
|
|
|
response.readEntity(String.class));
|
|
|
|
}
|
2015-05-26 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(String path) {
|
|
|
|
delete(path, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public String getRawValue(String string, MultivaluedMap<String, String> queryParams, long duration) {
|
2016-03-11 13:00:28 +01:00
|
|
|
try {
|
|
|
|
if (string.equals("")) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
String key = getCacheKey(string, queryParams, duration);
|
|
|
|
String res = getStringFromCache(key, duration);
|
|
|
|
if (res != null) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
Response response = get(string, queryParams).get(Response.class);
|
2015-10-06 11:42:16 +02:00
|
|
|
|
2016-03-11 13:00:28 +01:00
|
|
|
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
|
|
|
|
// TBD
|
|
|
|
// We are currently not caching errors,
|
|
|
|
// it should be reconsider.
|
2016-10-11 14:10:19 +02:00
|
|
|
throw getException("Scylla API server HTTP GET to URL '" + string + "' failed",
|
|
|
|
response.readEntity(String.class));
|
2016-03-11 13:00:28 +01:00
|
|
|
}
|
|
|
|
res = response.readEntity(String.class);
|
|
|
|
if (duration > 0) {
|
|
|
|
cache.put(key, new CacheEntry(res));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
} catch (ProcessingException e) {
|
|
|
|
throw new IllegalStateException("Unable to connect to Scylla API server: " + e.getMessage());
|
2015-10-06 11:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public String getRawValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-10-06 11:42:16 +02:00
|
|
|
return getRawValue(string, queryParams, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRawValue(String string, long duration) {
|
|
|
|
return getRawValue(string, null, duration);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-09-10 21:43:30 +02:00
|
|
|
public String getRawValue(String string) {
|
2015-10-06 11:42:16 +02:00
|
|
|
return getRawValue(string, null, 0);
|
2015-09-10 21:43:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getStringValue(String string, MultivaluedMap<String, String> queryParams) {
|
2016-10-11 14:10:19 +02:00
|
|
|
return getRawValue(string, queryParams).replaceAll("^\"|\"$", "");
|
2015-09-10 21:43:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 11:42:16 +02:00
|
|
|
public String getStringValue(String string, MultivaluedMap<String, String> queryParams, long duration) {
|
2016-10-11 14:10:19 +02:00
|
|
|
return getRawValue(string, queryParams, duration).replaceAll("^\"|\"$", "");
|
2015-10-06 11:42:16 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 11:31:38 +02:00
|
|
|
public String getStringValue(String string) {
|
|
|
|
return getStringValue(string, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public JsonReader getReader(String string, MultivaluedMap<String, String> queryParams) {
|
|
|
|
return factory.createReader(new StringReader(getRawValue(string, queryParams)));
|
2015-05-26 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public JsonReader getReader(String string) {
|
2015-05-26 11:31:38 +02:00
|
|
|
return getReader(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String[] getStringArrValue(String string) {
|
2015-05-26 11:31:38 +02:00
|
|
|
List<String> val = getListStrValue(string);
|
|
|
|
return val.toArray(new String[val.size()]);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public int getIntValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-09-10 21:43:30 +02:00
|
|
|
return Integer.parseInt(getRawValue(string, queryParams));
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getIntValue(String string) {
|
2015-05-26 11:31:38 +02:00
|
|
|
return getIntValue(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public static <T> BiFunction<APIClient, String, T> getReader(Class<T> type) {
|
|
|
|
if (type == String.class) {
|
|
|
|
return (c, s) -> type.cast(c.getRawValue(s));
|
|
|
|
} else if (type == Integer.class) {
|
|
|
|
return (c, s) -> type.cast(c.getIntValue(s));
|
|
|
|
} else if (type == Double.class) {
|
|
|
|
return (c, s) -> type.cast(c.getDoubleValue(s));
|
|
|
|
} else if (type == Long.class) {
|
|
|
|
return (c, s) -> type.cast(c.getLongValue(s));
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException(type.getName());
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public boolean getBooleanValue(String string) {
|
2015-09-10 21:43:30 +02:00
|
|
|
return Boolean.parseBoolean(getRawValue(string));
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public double getDoubleValue(String string) {
|
2015-09-10 21:43:30 +02:00
|
|
|
return Double.parseDouble(getRawValue(string));
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public List<String> getListStrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-05-26 11:31:38 +02:00
|
|
|
JsonReader reader = getReader(string, queryParams);
|
2015-05-19 16:50:40 +02:00
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
List<String> res = new ArrayList<String>(arr.size());
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
res.add(arr.getString(i));
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return res;
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 11:31:38 +02:00
|
|
|
public List<String> getListStrValue(String string) {
|
|
|
|
return getListStrValue(string, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> listStrFromJArr(JsonArray arr) {
|
|
|
|
List<String> res = new ArrayList<String>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
res.add(arr.getString(i));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-07-02 08:18:34 +02:00
|
|
|
public static Map<String, String> mapStrFromJArr(JsonArray arr) {
|
|
|
|
Map<String, String> res = new HashMap<String, String>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
if (obj.containsKey("key") && obj.containsKey("value")) {
|
|
|
|
res.put(obj.getString("key"), obj.getString("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-26 11:31:38 +02:00
|
|
|
public static String join(String[] arr, String joiner) {
|
|
|
|
String res = "";
|
|
|
|
if (arr != null) {
|
|
|
|
for (String name : arr) {
|
|
|
|
if (name != null && !name.equals("")) {
|
|
|
|
if (!res.equals("")) {
|
|
|
|
res = res + ",";
|
|
|
|
}
|
|
|
|
res = res + name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String join(String[] arr) {
|
|
|
|
return join(arr, ",");
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public static String mapToString(Map<String, String> mp, String pairJoin, String joiner) {
|
2015-06-21 08:41:43 +02:00
|
|
|
String res = "";
|
|
|
|
if (mp != null) {
|
|
|
|
for (String name : mp.keySet()) {
|
|
|
|
if (!res.equals("")) {
|
|
|
|
res = res + joiner;
|
|
|
|
}
|
|
|
|
res = res + name + pairJoin + mp.get(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String mapToString(Map<String, String> mp) {
|
|
|
|
return mapToString(mp, "=", ",");
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public static boolean set_query_param(MultivaluedMap<String, String> queryParams, String key, String value) {
|
|
|
|
if (queryParams != null && key != null && value != null && !value.equals("")) {
|
2015-05-26 11:31:38 +02:00
|
|
|
queryParams.add(key, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public static boolean set_bool_query_param(MultivaluedMap<String, String> queryParams, String key, boolean value) {
|
2015-05-26 11:31:38 +02:00
|
|
|
if (queryParams != null && key != null && value) {
|
|
|
|
queryParams.add(key, "true");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-17 14:31:18 +02:00
|
|
|
public Map<String, List<String>> getMapStringListStrValue(String string,
|
|
|
|
MultivaluedMap<String, String> queryParams) {
|
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Map<String, List<String>> map = new HashMap<String, List<String>>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
if (obj.containsKey("key") && obj.containsKey("value")) {
|
2016-10-11 14:10:19 +02:00
|
|
|
map.put(obj.getString("key"), listStrFromJArr(obj.getJsonArray("value")));
|
2015-06-17 14:31:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, List<String>> getMapStringListStrValue(String string) {
|
|
|
|
return getMapStringListStrValue(string, null);
|
|
|
|
}
|
|
|
|
|
2015-05-26 11:31:38 +02:00
|
|
|
public Map<List<String>, List<String>> getMapListStrValue(String string,
|
|
|
|
MultivaluedMap<String, String> queryParams) {
|
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Map<List<String>, List<String>> map = new HashMap<List<String>, List<String>>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
if (obj.containsKey("key") && obj.containsKey("value")) {
|
2016-10-11 14:10:19 +02:00
|
|
|
map.put(listStrFromJArr(obj.getJsonArray("key")), listStrFromJArr(obj.getJsonArray("value")));
|
2015-05-26 11:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public Map<List<String>, List<String>> getMapListStrValue(String string) {
|
2015-05-26 11:31:38 +02:00
|
|
|
return getMapListStrValue(string, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Set<String> getSetStringValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-06-17 14:31:18 +02:00
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Set<String> res = new HashSet<String>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
res.add(arr.getString(i));
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<String> getSetStringValue(String string) {
|
|
|
|
return getSetStringValue(string, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, String> getMapStrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-05-26 11:31:38 +02:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
2016-12-28 11:45:33 +01:00
|
|
|
Map<String, String> map = new LinkedHashMap<String, String>();
|
2015-05-26 11:31:38 +02:00
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
if (obj.containsKey("key") && obj.containsKey("value")) {
|
|
|
|
map.put(obj.getString("key"), obj.getString("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, String> getMapStrValue(String string) {
|
2015-05-26 11:31:38 +02:00
|
|
|
return getMapStrValue(string, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, String> getReverseMapStrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2016-03-19 17:37:13 +01:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
if (obj.containsKey("key") && obj.containsKey("value")) {
|
|
|
|
map.put(obj.getString("value"), obj.getString("key"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, String> getReverseMapStrValue(String string) {
|
2016-03-19 17:37:13 +01:00
|
|
|
return getReverseMapStrValue(string, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public List<InetAddress> getListInetAddressValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-05-26 11:31:38 +02:00
|
|
|
List<String> vals = getListStrValue(string, queryParams);
|
|
|
|
List<InetAddress> res = new ArrayList<InetAddress>();
|
|
|
|
for (String val : vals) {
|
|
|
|
try {
|
|
|
|
res.add(InetAddress.getByName(val));
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<InetAddress> getListInetAddressValue(String string) {
|
2015-05-26 11:31:38 +02:00
|
|
|
return getListInetAddressValue(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, TabularData> getMapStringTabularDataValue(String string) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-12-09 13:25:53 +01:00
|
|
|
private TabularDataSupport getSnapshotData(String key, JsonArray arr) {
|
2016-10-11 14:10:19 +02:00
|
|
|
TabularDataSupport data = new TabularDataSupport(SnapshotDetailsTabularData.TABULAR_TYPE);
|
2015-05-26 11:31:38 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
2015-12-09 13:25:53 +01:00
|
|
|
if (obj.containsKey("ks") && obj.containsKey("cf")) {
|
2017-02-22 11:40:19 +01:00
|
|
|
SnapshotDetailsTabularData.from(key, obj.getString("ks"), obj.getString("cf"), obj.getJsonNumber("total").longValue(),
|
|
|
|
obj.getJsonNumber("live").longValue(), data);
|
2015-05-26 11:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, TabularData> getMapStringSnapshotTabularDataValue(String string,
|
|
|
|
MultivaluedMap<String, String> queryParams) {
|
2015-05-26 11:31:38 +02:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Map<String, TabularData> map = new HashMap<>();
|
|
|
|
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
if (obj.containsKey("key") && obj.containsKey("value")) {
|
|
|
|
String key = obj.getString("key");
|
|
|
|
map.put(key, getSnapshotData(key, obj.getJsonArray("value")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public long getLongValue(String string) {
|
2015-09-10 21:43:30 +02:00
|
|
|
return Long.parseLong(getRawValue(string));
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:32:16 +02:00
|
|
|
public Map<InetAddress, Float> getMapInetAddressFloatValue(String string,
|
|
|
|
MultivaluedMap<String, String> queryParams) {
|
|
|
|
Map<InetAddress, Float> res = new HashMap<InetAddress, Float>();
|
|
|
|
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
JsonObject obj = null;
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
try {
|
|
|
|
obj = arr.getJsonObject(i);
|
2016-10-11 14:10:19 +02:00
|
|
|
res.put(InetAddress.getByName(obj.getString("key")), Float.parseFloat(obj.getString("value")));
|
2015-08-24 09:32:16 +02:00
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
logger.warning("Bad formatted address " + obj.getString("key"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:32:16 +02:00
|
|
|
public Map<InetAddress, Float> getMapInetAddressFloatValue(String string) {
|
|
|
|
return getMapInetAddressFloatValue(string, null);
|
|
|
|
}
|
2015-11-03 09:24:23 +01:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, Long> getMapStringLongValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-11-03 09:24:23 +01:00
|
|
|
Map<String, Long> res = new HashMap<String, Long>();
|
|
|
|
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
JsonObject obj = null;
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
obj = arr.getJsonObject(i);
|
|
|
|
res.put(obj.getString("key"), obj.getJsonNumber("value").longValue());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public Map<String, Long> getMapStringLongValue(String string) {
|
2015-11-03 09:24:23 +01:00
|
|
|
return getMapStringLongValue(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public long[] getLongArrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-06-18 10:52:17 +02:00
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
long[] res = new long[arr.size()];
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
res[i] = arr.getJsonNumber(i).longValue();
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public long[] getLongArrValue(String string) {
|
2015-06-18 10:52:17 +02:00
|
|
|
return getLongArrValue(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, Integer> getMapStringIntegerValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-11-03 09:24:23 +01:00
|
|
|
Map<String, Integer> res = new HashMap<String, Integer>();
|
|
|
|
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
JsonObject obj = null;
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
obj = arr.getJsonObject(i);
|
|
|
|
res.put(obj.getString("key"), obj.getInt("value"));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public Map<String, Integer> getMapStringIntegerValue(String string) {
|
2015-11-03 09:24:23 +01:00
|
|
|
return getMapStringIntegerValue(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public int[] getIntArrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-06-21 08:41:43 +02:00
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
int[] res = new int[arr.size()];
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
res[i] = arr.getInt(i);
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:28:47 +02:00
|
|
|
public int[] getIntArrValue(String string) {
|
2015-06-21 08:41:43 +02:00
|
|
|
return getIntArrValue(string, null);
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, Long> getListMapStringLongValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-04-13 08:28:47 +02:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-06-02 09:27:42 +02:00
|
|
|
JsonReader reader = getReader(string, queryParams);
|
2015-04-13 08:28:47 +02:00
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Map<String, Long> map = new HashMap<String, Long>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
Iterator<String> it = obj.keySet().iterator();
|
|
|
|
String key = "";
|
|
|
|
long val = -1;
|
|
|
|
while (it.hasNext()) {
|
2015-05-25 15:40:37 +02:00
|
|
|
String k = it.next();
|
2015-04-13 08:28:47 +02:00
|
|
|
if (obj.get(k) instanceof JsonString) {
|
|
|
|
key = obj.getString(k);
|
|
|
|
} else {
|
2015-12-29 16:41:20 +01:00
|
|
|
val = obj.getJsonNumber(k).longValue();
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (val > 0 && !key.equals("")) {
|
|
|
|
map.put(key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
|
|
|
}
|
2015-06-18 10:52:17 +02:00
|
|
|
|
2015-06-02 09:27:42 +02:00
|
|
|
public Map<String, Long> getListMapStringLongValue(String string) {
|
|
|
|
return getListMapStringLongValue(string, null);
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public JsonArray getJsonArray(String string, MultivaluedMap<String, String> queryParams) {
|
2015-06-02 09:27:42 +02:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray res = reader.readArray();
|
|
|
|
reader.close();
|
|
|
|
return res;
|
|
|
|
}
|
2015-06-18 10:52:17 +02:00
|
|
|
|
2015-06-02 09:27:42 +02:00
|
|
|
public JsonArray getJsonArray(String string) {
|
|
|
|
return getJsonArray(string, null);
|
|
|
|
}
|
2015-07-02 08:18:34 +02:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public List<Map<String, String>> getListMapStrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-07-02 08:18:34 +02:00
|
|
|
JsonArray arr = getJsonArray(string, queryParams);
|
|
|
|
List<Map<String, String>> res = new ArrayList<Map<String, String>>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
res.add(mapStrFromJArr(arr.getJsonArray(i)));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Map<String, String>> getListMapStrValue(String string) {
|
|
|
|
return getListMapStrValue(string, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TabularData getCQLResult(String string) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-14 16:47:54 +02:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public JsonObject getJsonObj(String string, MultivaluedMap<String, String> queryParams, long duration) {
|
2015-07-14 16:47:54 +02:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-17 08:35:42 +02:00
|
|
|
String key = getCacheKey(string, queryParams, duration);
|
|
|
|
JsonObject res = getJsonObjectFromCache(key, duration);
|
|
|
|
if (res != null) {
|
|
|
|
return res;
|
|
|
|
}
|
2015-07-14 16:47:54 +02:00
|
|
|
JsonReader reader = getReader(string, queryParams);
|
2016-05-17 08:35:42 +02:00
|
|
|
res = reader.readObject();
|
2015-07-14 16:47:54 +02:00
|
|
|
reader.close();
|
2016-05-17 08:35:42 +02:00
|
|
|
if (duration > 0) {
|
|
|
|
cache.put(key, new CacheEntry(res));
|
|
|
|
}
|
2015-07-14 16:47:54 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public JsonObject getJsonObj(String string, MultivaluedMap<String, String> queryParams) {
|
|
|
|
return getJsonObj(string, queryParams, 0);
|
2015-07-14 16:47:54 +02:00
|
|
|
}
|
2015-08-05 14:29:09 +02:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public long[] getEstimatedHistogramAsLongArrValue(String string, MultivaluedMap<String, String> queryParams) {
|
2015-08-05 14:29:09 +02:00
|
|
|
JsonObject obj = getJsonObj(string, queryParams);
|
|
|
|
JsonArray arr = obj.getJsonArray("buckets");
|
2015-11-19 10:49:31 +01:00
|
|
|
if (arr == null) {
|
|
|
|
return new long[0];
|
|
|
|
}
|
2015-08-05 14:29:09 +02:00
|
|
|
long res[] = new long[arr.size()];
|
2016-10-11 14:10:19 +02:00
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
2015-08-05 14:29:09 +02:00
|
|
|
res[i] = arr.getJsonNumber(i).longValue();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long[] getEstimatedHistogramAsLongArrValue(String string) {
|
|
|
|
return getEstimatedHistogramAsLongArrValue(string, null);
|
|
|
|
}
|
2015-12-29 16:42:18 +01:00
|
|
|
|
2016-10-11 14:10:19 +02:00
|
|
|
public Map<String, Double> getMapStringDouble(String string, MultivaluedMap<String, String> queryParams) {
|
2015-12-29 16:42:18 +01:00
|
|
|
if (string.equals("")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
JsonReader reader = getReader(string, queryParams);
|
|
|
|
JsonArray arr = reader.readArray();
|
|
|
|
Map<String, Double> map = new HashMap<String, Double>();
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
JsonObject obj = arr.getJsonObject(i);
|
|
|
|
Iterator<String> it = obj.keySet().iterator();
|
|
|
|
String key = "";
|
|
|
|
double val = -1;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
String k = it.next();
|
|
|
|
if (obj.get(k) instanceof JsonString) {
|
|
|
|
key = obj.getString(k);
|
|
|
|
} else {
|
|
|
|
val = obj.getJsonNumber(k).doubleValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!key.equals("")) {
|
|
|
|
map.put(key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return map;
|
|
|
|
}
|
2016-10-11 14:10:19 +02:00
|
|
|
|
2015-12-29 16:42:18 +01:00
|
|
|
public Map<String, Double> getMapStringDouble(String string) {
|
|
|
|
return getMapStringDouble(string, null);
|
|
|
|
}
|
2015-04-13 08:28:47 +02:00
|
|
|
}
|