List of usage examples for com.google.gson JsonElement getAsJsonObject
public JsonObject getAsJsonObject()
From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java
License:Apache License
/** * Gathers Bucket Replication stats from /pools/default/buckets/<bucket_name>/stats * //www .j a va 2 s.c om * Response contains all other stats but this only filters any stats that starts with 'replications', * e.g. replications/03cd3332434401c64594f47eeeabbb79/beer-sample/gamesim-sample/wtavg_meta_latency * * @param buckets * @param httpClient * @return Map<String, Map<String, Double>> */ public Map<String, Map<String, Double>> gatherOtherBucketMetrics(Set<String> buckets, SimpleHttpClient httpClient) { Map<String, Map<String, Double>> otherBucketMetrics = new HashMap<String, Map<String, Double>>(); if (buckets != null) { for (String bucket : buckets) { JsonElement bucketResponse = getResponse(httpClient, String.format(BUCKET_STATS_URI, bucket)); if (bucketResponse != null && bucketResponse.isJsonObject()) { JsonObject bucketStats = bucketResponse.getAsJsonObject(); JsonObject op = bucketStats.getAsJsonObject("op"); JsonObject samples = op.getAsJsonObject("samples"); populateOtherBucketMetrics(bucket, samples.entrySet().iterator(), otherBucketMetrics); } } } return otherBucketMetrics; }
From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java
License:Apache License
/** * Populates the node metrics hashmap/*from w w w . j a v a2 s . co m*/ * * @param nodes * @return */ private Map<String, Map<String, Double>> populateNodeMetrics(JsonArray nodes) { Map<String, Map<String, Double>> nodeMetrics = new HashMap<String, Map<String, Double>>(); for (JsonElement node : nodes) { JsonObject nodeObject = node.getAsJsonObject(); Map<String, Double> metrics = new HashMap<String, Double>(); nodeMetrics.put(nodeObject.get("hostname").getAsString(), metrics); JsonObject interestingStats = nodeObject.getAsJsonObject("interestingStats"); Iterator<Entry<String, JsonElement>> iterator = interestingStats.entrySet().iterator(); populateMetricsMapHelper(iterator, metrics, ""); JsonObject systemStats = nodeObject.getAsJsonObject("systemStats"); iterator = systemStats.entrySet().iterator(); populateMetricsMapHelper(iterator, metrics, ""); iterator = nodeObject.entrySet().iterator(); populateMetricsMapHelper(iterator, metrics, ""); } return nodeMetrics; }
From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java
License:Apache License
/** * Populates the bucket metrics hashmap//from ww w. j a v a2s .c o m * * @param buckets * @return */ private Map<String, Map<String, Double>> populateBucketMetrics(JsonArray buckets) { Map<String, Map<String, Double>> bucketMetrics = new HashMap<String, Map<String, Double>>(); for (JsonElement bucket : buckets) { JsonObject bucketObject = bucket.getAsJsonObject(); Map<String, Double> metrics = new HashMap<String, Double>(); bucketMetrics.put(bucketObject.get("name").getAsString(), metrics); JsonObject interestingStats = bucketObject.getAsJsonObject("quota"); Iterator<Entry<String, JsonElement>> iterator = interestingStats.entrySet().iterator(); populateMetricsMapHelper(iterator, metrics, ""); JsonObject systemStats = bucketObject.getAsJsonObject("basicStats"); iterator = systemStats.entrySet().iterator(); populateMetricsMapHelper(iterator, metrics, ""); } return bucketMetrics; }
From source file:com.arangodb.BaseArangoDriver.java
License:Apache License
/** * Checks the Http response for database or server errors * @param res the response of the database * @return The Http status code//from www . j a v a 2 s.c om * @throws ArangoException if any error happened */ private int checkServerErrors(HttpResponseEntity res) throws ArangoException { int statusCode = res.getStatusCode(); if (statusCode >= 400) { // always throws ArangoException DefaultEntity defaultEntity = new DefaultEntity(); if (res.getText() != null && !res.getText().equalsIgnoreCase("") && statusCode != 500) { JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(res.getText()); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement errorMessage = jsonObject.get("errorMessage"); defaultEntity.setErrorMessage(errorMessage.getAsString()); JsonElement errorNumber = jsonObject.get("errorNum"); defaultEntity.setErrorNumber(errorNumber.getAsInt()); } else { String statusPhrase = ""; switch (statusCode) { case 400: statusPhrase = "Bad Request"; break; case 401: statusPhrase = "Unauthorized"; break; case 403: statusPhrase = "Forbidden"; break; case 404: statusPhrase = "Not Found"; break; case 405: statusPhrase = "Method Not Allowed"; break; case 406: statusPhrase = "Not Acceptable"; break; case 407: statusPhrase = "Proxy Authentication Required"; break; case 408: statusPhrase = "Request Time-out"; break; case 409: statusPhrase = "Conflict"; break; case 500: statusPhrase = "Internal Server Error"; break; default: statusPhrase = "unknown error"; break; } defaultEntity.setErrorMessage(statusPhrase); if (statusCode == 500) { defaultEntity.setErrorMessage(statusPhrase + ": " + res.getText()); } } defaultEntity.setCode(statusCode); defaultEntity.setStatusCode(statusCode); defaultEntity.setError(true); ArangoException arangoException = new ArangoException(defaultEntity); arangoException.setCode(statusCode); throw arangoException; } return statusCode; }
From source file:com.arangodb.BaseArangoDriver.java
License:Apache License
/** * Gets the raw JSON string with results, from the Http response * @param res the response of the database * @return A valid JSON string with the results * @throws ArangoException/*from ww w . j a v a 2s. c o m*/ */ protected String getJSONResponseText(HttpResponseEntity res) throws ArangoException { if (res == null) { return null; } checkServerErrors(res); // no errors, return results as a JSON string JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(res.getText()); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement result = jsonObject.get("result"); return result.toString(); }
From source file:com.arangodb.entity.EntityDeserializers.java
License:Apache License
private static JsonObject getFirstResultAsJsonObject(JsonObject obj) { if (obj.has("result")) { if (obj.get("result").isJsonArray()) { JsonArray result = obj.getAsJsonArray("result"); if (result.size() > 0) { JsonElement jsonElement = result.get(0); if (jsonElement.isJsonObject()) { return jsonElement.getAsJsonObject(); }/*from w w w . j a v a 2 s.co m*/ } } else if (obj.get("result").isJsonObject()) { return obj.getAsJsonObject("result"); } } return null; }
From source file:com.arangodb.entity.EntityFactory.java
License:Apache License
public static <T> String toJsonString(T obj, boolean includeNullValue) { if (obj != null && obj.getClass().equals(BaseDocument.class)) { String tmp = includeNullValue ? gsonNull.toJson(obj) : gson.toJson(obj); JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(tmp); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonObject result = jsonObject.getAsJsonObject("properties"); JsonElement keyObject = jsonObject.get("_key"); if (keyObject != null && keyObject.getClass() != JsonNull.class) { result.add("_key", jsonObject.get("_key")); }//from w ww . ja v a 2s . c om JsonElement handleObject = jsonObject.get("_id"); if (handleObject != null && handleObject.getClass() != JsonNull.class) { result.add("_id", jsonObject.get("_id")); } // JsonElement revisionValue = jsonObject.get("documentRevision"); // result.add("_rev", revisionValue); return result.toString(); } return includeNullValue ? gsonNull.toJson(obj) : gson.toJson(obj); }
From source file:com.arangodb.impl.InternalDocumentDriverImpl.java
License:Apache License
private <T> DocumentEntity<T> _createDocument(String database, String collectionName, String documentKey, T value, Boolean createCollection, Boolean waitForSync, boolean raw) throws ArangoException { validateCollectionName(collectionName); String body;/*w w w . j a va 2 s. co m*/ if (raw) { body = value.toString(); } else if (documentKey != null) { JsonElement elem = EntityFactory.toJsonElement(value, false); if (elem.isJsonObject()) { elem.getAsJsonObject().addProperty(BaseDocument.KEY, documentKey); } body = EntityFactory.toJsonString(elem); } else { body = EntityFactory.toJsonString(value); } HttpResponseEntity res = httpManager.doPost(createEndpointUrl(database, "/_api/document"), new MapBuilder().put("collection", collectionName).put("createCollection", createCollection) .put("waitForSync", waitForSync).get(), body); @SuppressWarnings("unchecked") DocumentEntity<T> result = createEntity(res, DocumentEntity.class); annotationHandler.updateDocumentAttributes(value, result.getDocumentRevision(), result.getDocumentHandle(), result.getDocumentKey()); result.setEntity(value); return result; }
From source file:com.arangodb.impl.InternalGraphDriverImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from ww w . j a v a 2 s . c o m*/ public <T> VertexEntity<T> createVertex(String database, String graphName, String collectionName, String key, T vertex, Boolean waitForSync) throws ArangoException { JsonObject obj; if (vertex == null) { obj = new JsonObject(); } else { JsonElement elem = EntityFactory.toJsonElement(vertex, false); if (elem.isJsonObject()) { obj = elem.getAsJsonObject(); } else { throw new IllegalArgumentException("vertex need object type(not support array, primitive, etc..)."); } } if (key != null) { obj.addProperty("_key", key); } validateCollectionName(graphName); HttpResponseEntity res = httpManager.doPost( createEndpointUrl(database, "/_api/gharial", StringUtils.encodeUrl(graphName), "vertex", StringUtils.encodeUrl(collectionName)), new MapBuilder().put("waitForSync", waitForSync).get(), EntityFactory.toJsonString(obj)); if (!res.isJsonResponse()) { throw new ArangoException("unknown error"); } VertexEntity<T> result = createEntity(res, VertexEntity.class, vertex.getClass()); if (vertex != null) { result.setEntity(vertex); annotationHandler.updateDocumentAttributes(result.getEntity(), result.getDocumentRevision(), result.getDocumentHandle(), result.getDocumentKey()); } return result; }
From source file:com.arangodb.impl.InternalGraphDriverImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w w w .j av a2s. c om public <T> EdgeEntity<T> createEdge(String database, String graphName, String edgeCollectionName, String key, String fromHandle, String toHandle, T value, Boolean waitForSync) throws ArangoException { JsonObject obj; if (value == null) { obj = new JsonObject(); } else { JsonElement elem = EntityFactory.toJsonElement(value, false); if (elem.isJsonObject()) { obj = elem.getAsJsonObject(); } else { throw new IllegalArgumentException("value need object type(not support array, primitive, etc..)."); } } if (key != null) { obj.addProperty("_key", key); } obj.addProperty("_from", fromHandle); obj.addProperty("_to", toHandle); validateCollectionName(graphName); HttpResponseEntity res = httpManager.doPost( createEndpointUrl(database, "/_api/gharial", StringUtils.encodeUrl(graphName), "/edge", StringUtils.encodeUrl(edgeCollectionName)), new MapBuilder().put("waitForSync", waitForSync).get(), EntityFactory.toJsonString(obj)); EdgeEntity<T> entity = createEntity(res, EdgeEntity.class, value == null ? null : value.getClass()); if (value != null) { entity.setEntity(value); annotationHandler.updateEdgeAttributes(value, entity.getDocumentRevision(), entity.getDocumentHandle(), entity.getDocumentKey(), fromHandle, toHandle); } entity.setFromVertexHandle(fromHandle); entity.setToVertexHandle(toHandle); return entity; }