use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.
the class Replicator method findAll.
/**
* Finds all documents in the replicator database.
*/
public List<ReplicatorDocument> findAll() {
InputStream instream = null;
try {
final URI uri = new DatabaseURIHelper(dbURI).path("_all_docs").query("include_docs", "true").build();
final Reader reader = new InputStreamReader(instream = client.get(uri), "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 = client.getGson().fromJson(elem, ReplicatorDocument.class);
list.add(rd);
}
}
return list;
} catch (UnsupportedEncodingException e) {
// to support UTF-8.
throw new RuntimeException(e);
} finally {
close(instream);
}
}
use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.
the class DatabaseURIHelperTest method _localDocumentURI.
@TestTemplate
public void _localDocumentURI(String path) throws Exception {
final String expected = uriBase + "/db_name/_local/mylocaldoc";
DatabaseURIHelper helper = helper(path + "/db_name");
URI localDoc = helper.documentUri("_local/mylocaldoc");
Assertions.assertEquals(expected, localDoc.toString());
}
use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.
the class CouchDatabaseBase method remove.
/**
* Removes a document from the database given both a document <code>_id</code> and
* <code>_rev</code> values.
*
* @param id The document _id field.
* @param rev The document _rev field.
* @return {@link Response}
* @throws NoDocumentException If the document is not found in the database.
*/
public Response remove(String id, String rev) {
assertNotEmpty(id, "id");
assertNotEmpty(rev, "rev");
final URI uri = new DatabaseURIHelper(dbUri).documentUri(id, rev);
return couchDbClient.delete(uri);
}
use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.
the class CouchDatabaseBase method saveAttachment.
/**
* 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}
* @throws DocumentConflictException
*/
public Response saveAttachment(InputStream in, String name, String contentType, String docId, String docRev) {
assertNotEmpty(in, "in");
assertNotEmpty(name, "name");
assertNotEmpty(contentType, "ContentType");
if (docId == null) {
docId = generateUUID();
// A new doc is being created; there should be no revision specified.
assertNull(docRev, "docRev");
} else {
// The id has been specified, ensure it is not empty
assertNotEmpty(docId, "docId");
if (docRev != null) {
// Existing doc with the specified ID, ensure rev is not empty
assertNotEmpty(docRev, "docRev");
}
}
final URI uri = new DatabaseURIHelper(dbUri).attachmentUri(docId, docRev, name);
return couchDbClient.put(uri, in, contentType);
}
use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.
the class CouchDatabaseBase method removeAttachment.
/**
* Removes an attachment from a document given both a document <code>_id</code> and
* <code>_rev</code> and <code>attachmentName</code> values.
*
* @param id The document _id field.
* @param rev The document _rev field.
* @param attachmentName The document attachment field.
* @return {@link Response}
* @throws NoDocumentException If the document is not found in the database.
* @throws DocumentConflictException If a conflict is found during attachment removal.
*/
public Response removeAttachment(String id, String rev, String attachmentName) {
assertNotEmpty(id, "id");
assertNotEmpty(rev, "rev");
assertNotEmpty(attachmentName, "attachmentName");
final URI uri = new DatabaseURIHelper(dbUri).attachmentUri(id, rev, attachmentName);
return couchDbClient.delete(uri);
}
Aggregations