/** * Queries for scalar values. Internal use. */ private <V> V queryValue(Class<V> classOfV) { InputStream instream = null; try { Reader reader = new InputStreamReader(instream = queryForStream(), Charsets.UTF_8); JsonArray array = new JsonParser().parse(reader). getAsJsonObject().get("rows").getAsJsonArray(); if(array.size() != 1) { throw new NoDocumentException("Expecting a single result but was: " + array.size()); } return JsonToObject(gson, array.get(0), "value", classOfV); } finally { close(instream); } }
@Override public void deleteVisualization(String pipelineId) { try { CouchDbClient couchDbClient = couchDbClientSupplier.get(); List<RunningVisualization> currentVisualizations = getRunningVisualizations(); for (RunningVisualization viz : currentVisualizations) { if (viz.getPipelineId() != null) { if (viz.getPipelineId().equals(pipelineId)) couchDbClient.remove(viz); } } couchDbClient.shutdown(); } catch (NoDocumentException e) { e.printStackTrace(); } } }
/** * Validates a HTTP response; on error cases logs status and throws relevant exceptions. * @param response The HTTP response. */ void validate(HttpResponse response) throws IOException { final int code = response.getStatusLine().getStatusCode(); if(code == 200 || code == 201 || code == 202) { // success (ok | created | accepted) return; } String reason = response.getStatusLine().getReasonPhrase(); switch (code) { case HttpStatus.SC_NOT_FOUND: { throw new NoDocumentException(reason); } case HttpStatus.SC_CONFLICT: { throw new DocumentConflictException(reason); } default: { // other errors: 400 | 401 | 500 etc. throw new CouchDbException(reason += EntityUtils.toString(response.getEntity())); } } }