/** * Closes the response input stream. * * @param response The {@link HttpResponse} */ public static void close(HttpResponse response) { try { close(response.getEntity().getContent()); } catch (Exception e) {} }
/** * 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); } }
/** * Queries a view. * @param <T> Object type T * @param classOfT The class of type T * @return The result of the view query as a {@code List<T> } */ public <T> List<T> query(Class<T> classOfT) { InputStream instream = null; try { Reader reader = new InputStreamReader(instream = queryForStream(), Charsets.UTF_8); JsonArray jsonArray = new JsonParser().parse(reader) .getAsJsonObject().getAsJsonArray("rows"); List<T> list = new ArrayList<T>(); for (JsonElement jsonElem : jsonArray) { JsonElement elem = jsonElem.getAsJsonObject(); if(Boolean.TRUE.equals(this.includeDocs)) { elem = jsonElem.getAsJsonObject().get("doc"); } T t = this.gson.fromJson(elem, classOfT); list.add(t); } return list; } finally { close(instream); } }
/** * Performs a HTTP DELETE request. * @return {@link Response} */ Response delete(URI uri) { HttpResponse response = null; try { HttpDelete delete = new HttpDelete(uri); response = executeRequest(delete); return getResponse(response); } finally { close(response); } }
private void terminate() { httpGet.abort(); CouchDbUtil.close(getReader()); } }
/** * Performs a HTTP PUT request, saves an attachment. * @return {@link Response} */ Response put(URI uri, InputStream instream, String contentType) { HttpResponse response = null; try { final HttpPut httpPut = new HttpPut(uri); final InputStreamEntity entity = new InputStreamEntity(instream, -1); entity.setContentType(contentType); httpPut.setEntity(entity); response = executeRequest(httpPut); return getResponse(response); } finally { close(response); } }
/** * Performs a HTTP GET request. * @return An object of type T */ <T> T get(URI uri, Class<T> classType) { InputStream in = null; try { in = get(uri); return getGson().fromJson(new InputStreamReader(in, "UTF-8"), classType); } catch (UnsupportedEncodingException e) { throw new CouchDbException(e); } finally { close(in); } }
/** * @return DB Server version. */ public String serverVersion() { InputStream instream = null; try { instream = dbc.get(buildUri(dbc.getBaseUri()).build()); Reader reader = new InputStreamReader(instream, Charsets.UTF_8); return getAsString(new JsonParser().parse(reader).getAsJsonObject(), "version"); } finally { close(instream); } }
/** * Finds all documents in the replicator database. * @return a list of {@link ReplicatorDocument} */ public List<ReplicatorDocument> findAll() { InputStream instream = null; try { final URI uri = buildUri(dbURI).path("_all_docs").query("include_docs", "true").build(); final Reader reader = new InputStreamReader(instream = dbc.get(uri), Charsets.UTF_8); final JsonArray jsonArray = new JsonParser().parse(reader) .getAsJsonObject().getAsJsonArray("rows"); final List<ReplicatorDocument> list = new ArrayList<ReplicatorDocument>(); for (JsonElement jsonElem : jsonArray) { JsonElement elem = jsonElem.getAsJsonObject().get("doc"); if(!getAsString(elem.getAsJsonObject(), "_id").startsWith("_design")) { // skip design docs ReplicatorDocument rd = dbc.getGson().fromJson(elem, ReplicatorDocument.class); list.add(rd); } } return list; } finally { close(instream); } }
/** * Requests CouchDB creates a new database; if one doesn't exist. * @param dbName The Database name */ public void createDB(String dbName) { assertNotEmpty(dbName, "dbName"); InputStream getresp = null; HttpResponse putresp = null; final URI uri = buildUri(dbc.getBaseUri()).path(dbName).build(); try { getresp = dbc.get(uri); } catch (NoDocumentException e) { // db doesn't exist final HttpPut put = new HttpPut(uri); putresp = dbc.executeRequest(put); log.info(String.format("Created Database: '%s'", dbName)); } finally { close(getresp); close(putresp); } }
/** * @return All Server databases. */ public List<String> getAllDbs() { InputStream instream = null; try { Type typeOfList = new TypeToken<List<String>>() {}.getType(); instream = dbc.get(buildUri(dbc.getBaseUri()).path("_all_dbs").build()); Reader reader = new InputStreamReader(instream, Charsets.UTF_8); return dbc.getGson().fromJson(reader, typeOfList); } finally { close(instream); } }
/** * Triggers a replication request. * @return {@link ReplicationResult} */ public ReplicationResult trigger() { assertNotEmpty(source, "Source"); assertNotEmpty(target, "Target"); HttpResponse response = null; try { JsonObject json = createJson(); if(log.isDebugEnabled()) { log.debug(json); } final URI uri = buildUri(dbc.getBaseUri()).path("_replicate").build(); response = dbc.post(uri, json.toString()); final InputStreamReader reader = new InputStreamReader(getStream(response), Charsets.UTF_8); return dbc.getGson().fromJson(reader, ReplicationResult.class); } finally { close(response); } }
/** * Requests the database commits any recent changes to disk. */ public void ensureFullCommit() { HttpResponse response = null; try { response = dbc.post(buildUri(dbc.getDBUri()).path("_ensure_full_commit").build(), ""); } finally { close(response); } }
/** * Triggers a database <i>compact</i> request. */ public void compact() { HttpResponse response = null; try { response = dbc.post(buildUri(dbc.getDBUri()).path("_compact").build(), ""); } finally { close(response); } }
/** * Saves a document with <tt>batch=ok</tt> query param. * @param object The object to save. */ public void batch(Object object) { assertNotEmpty(object, "object"); HttpResponse response = null; try { URI uri = buildUri(getDBUri()).query("batch", "ok").build(); response = post(uri, getGson().toJson(object)); } finally { close(response); } }
/** * Performs bulk documents create and update request. * @param objects The {@link List} of documents objects. * @param newEdits If false, prevents the database from assigning documents new revision IDs. * @return {@code List<Response>} Containing the resulted entries. */ public List<Response> bulk(List<?> objects, boolean newEdits) { assertNotEmpty(objects, "objects"); HttpResponse response = null; try { final String newEditsVal = newEdits ? "\"new_edits\": true, " : "\"new_edits\": false, "; final String json = String.format("{%s%s%s}", newEditsVal, "\"docs\": ", getGson().toJson(objects)); final URI uri = buildUri(getDBUri()).path("_bulk_docs").build(); response = post(uri, json); return getResponseList(response); } finally { close(response); } }
/** * Saves an object in the database using HTTP <tt>POST</tt> request. * <p>The database will be responsible for generating the document id. * @param object The object to save * @return {@link Response} */ public Response post(Object object) { assertNotEmpty(object, "object"); HttpResponse response = null; try { URI uri = buildUri(getDBUri()).build(); response = post(uri, getGson().toJson(object)); return getResponse(response); } finally { close(response); } }
/** * Checks if a document exist in the database. * @param id The document _id field. * @return true If the document is found, false otherwise. */ public boolean contains(String id) { assertNotEmpty(id, "id"); HttpResponse response = null; try { response = head(buildUri(getDBUri()).pathEncoded(id).build()); } catch (NoDocumentException e) { return false; } finally { close(response); } return true; }
/** * Performs a HTTP PUT request, saves or updates a document. * @return {@link Response} */ Response put(URI uri, Object object, boolean newEntity) { assertNotEmpty(object, "object"); HttpResponse response = null; try { final JsonObject json = getGson().toJsonTree(object).getAsJsonObject(); String id = getAsString(json, "_id"); String rev = getAsString(json, "_rev"); if(newEntity) { // save assertNull(rev, "rev"); id = (id == null) ? generateUUID() : id; } else { // update assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); } final HttpPut put = new HttpPut(buildUri(uri).pathEncoded(id).build()); setEntity(put, json.toString()); response = executeRequest(put); return getResponse(response); } finally { close(response); } }