/** * Saves an attachment to an existing document given both a document id * and revision, or save to a new document given only the id, and rev as {@code null}. * <p>To retrieve an attachment, see {@link #find(String)}. * @param in The {@link InputStream} holding the binary data. * @param name The attachment name. * @param contentType The attachment "Content-Type". * @param docId The document id to save the attachment under, or {@code null} to save under a new document. * @param docRev The document revision to save the attachment under, or {@code null} when saving to a new document. * @return {@link Response} */ public Response saveAttachment(InputStream in, String name, String contentType, String docId, String docRev) { assertNotEmpty(in, "in"); assertNotEmpty(name, "name"); assertNotEmpty(contentType, "ContentType"); assertNotEmpty(docId, "docId"); final URI uri = buildUri(getDBUri()).pathEncoded(docId).path("/").path(name).query("rev", docRev).build(); return put(uri, in, contentType); }
public static URIBuilder buildUri(URI uri) { URIBuilder builder = URIBuilder.buildUri().scheme(uri.getScheme()). host(uri.getHost()).port(uri.getPort()).path(uri.getPath()); return builder; }
View(CouchDbClientBase dbc, String viewId) { assertNotEmpty(viewId, "View id"); this.dbc = dbc; this.gson = dbc.getGson(); String view = viewId; if(viewId.contains("/")) { String[] v = viewId.split("/"); view = String.format("_design/%s/_view/%s", v[0], v[1]); } this.uriBuilder = URIBuilder.buildUri(dbc.getDBUri()).path(view); }
/** * Requests CouchDB deletes a database. * @param dbName The database name * @param confirm A confirmation string with the value: <tt>delete database</tt> */ public void deleteDB(String dbName, String confirm) { assertNotEmpty(dbName, "dbName"); if(!"delete database".equals(confirm)) throw new IllegalArgumentException("Invalid confirm!"); dbc.delete(buildUri(dbc.getBaseUri()).path(dbName).build()); }
/** * Saves an attachment to a new document with a generated <tt>UUID</tt> as the document id. * <p>To retrieve an attachment, see {@link #find(String)}. * @param in The {@link InputStream} holding the binary data. * @param name The attachment name. * @param contentType The attachment "Content-Type". * @return {@link Response} */ public Response saveAttachment(InputStream in, String name, String contentType) { assertNotEmpty(in, "in"); assertNotEmpty(name, "name"); assertNotEmpty(contentType, "ContentType"); final URI uri = buildUri(getDBUri()).path(generateUUID()).path("/").path(name).build(); return put(uri, in, contentType); }
Changes(CouchDbClientBase dbc) { this.dbc = dbc; this.gson = dbc.getGson(); this.uriBuilder = URIBuilder.buildUri(dbc.getDBUri()).path("_changes"); }
/** * Invokes an Update Handler. * <pre> * Params params = new Params() * .addParam("field", "foo") * .addParam("value", "bar"); * String output = dbClient.invokeUpdateHandler("designDoc/update1", "docId", params); * </pre> * @param updateHandlerUri The Update Handler URI, in the format: <code>designDoc/update1</code> * @param docId The document id to update. * @param params The query parameters as {@link Params}. * @return The output of the request. */ public String invokeUpdateHandler(String updateHandlerUri, String docId, Params params) { assertNotEmpty(updateHandlerUri, "uri"); assertNotEmpty(docId, "docId"); final String[] v = updateHandlerUri.split("/"); final String path = String.format("_design/%s/_update/%s/", v[0], v[1]); final URI uri = buildUri(getDBUri()).path(path).path(docId).query(params).build(); final HttpResponse response = executeRequest(new HttpPut(uri)); return streamToString(getStream(response)); }
/** * @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); } }
CouchDbClientBase(CouchDbConfig config) { final CouchDbProperties props = config.getProperties(); this.httpClient = createHttpClient(props); this.gson = initGson(new GsonBuilder()); this.host = new HttpHost(props.getHost(), props.getPort(), props.getProtocol()); final String path = props.getPath() != null ? props.getPath() : ""; this.baseURI = buildUri().scheme(props.getProtocol()).host(props.getHost()).port(props.getPort()).path("/").path(path).build(); this.dbURI = buildUri(baseURI).path(props.getDbName()).path("/").build(); this.context = new CouchDbContext(this, props); this.design = new CouchDbDesign(this); }
/** * Gets a design document from the database. * @param id The document id * @return {@link DesignDocument} */ public DesignDocument getFromDb(String id) { assertNotEmpty(id, "id"); final URI uri = buildUri(dbc.getDBUri()).path(id).build(); return dbc.get(uri, DesignDocument.class); }
/** * 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); } }
/** * Finds a document and return the result as {@link InputStream}. * <p><b>Note</b>: The stream must be closed after use to release the connection. * @param id The document _id field. * @return The result as {@link InputStream} * @throws NoDocumentException If the document is not found in the database. * @see #find(String, String) */ public InputStream find(String id) { assertNotEmpty(id, "id"); return get(buildUri(getDBUri()).path(id).build()); }
/** * 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); } }
/** * 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); } }
/** * Gets a design document from the database. * @param id The document id * @param rev The document revision * @return {@link DesignDocument} */ public DesignDocument getFromDb(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(id, "rev"); final URI uri = buildUri(dbc.getDBUri()).path(id).query("rev", rev).build(); return dbc.get(uri, DesignDocument.class); }
/** * Finds a document given id and revision and returns the result as {@link InputStream}. * <p><b>Note</b>: The stream must be closed after use to release the connection. * @param id The document _id field. * @param rev The document _rev field. * @return The result as {@link InputStream} * @throws NoDocumentException If the document is not found in the database. */ public InputStream find(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); final URI uri = buildUri(getDBUri()).path(id).query("rev", rev).build(); return get(uri); }
/** * Finds a document in the replicator database. * @return {@link ReplicatorDocument} */ public ReplicatorDocument find() { assertNotEmpty(replicatorDoc.getId(), "Doc id"); final URI uri = buildUri(dbURI).path(replicatorDoc.getId()).query("rev", replicatorDoc.getRevision()).build(); return dbc.get(uri, ReplicatorDocument.class); }
/** * Removes a document from the replicator database. * @return {@link Response} */ public Response remove() { assertNotEmpty(replicatorDoc.getId(), "Doc id"); assertNotEmpty(replicatorDoc.getRevision(), "Doc rev"); final URI uri = buildUri(dbURI).path(replicatorDoc.getId()).query("rev", replicatorDoc.getRevision()).build(); return dbc.delete(uri); }