Search in sources :

Example 6 with DatabaseURIHelper

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URI(java.net.URI) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) JsonParser(com.google.gson.JsonParser)

Example 7 with DatabaseURIHelper

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());
}
Also used : DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) URI(java.net.URI) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 8 with DatabaseURIHelper

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);
}
Also used : DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) URI(java.net.URI)

Example 9 with DatabaseURIHelper

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);
}
Also used : DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) URI(java.net.URI)

Example 10 with DatabaseURIHelper

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);
}
Also used : DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) URI(java.net.URI)

Aggregations

DatabaseURIHelper (com.cloudant.client.internal.DatabaseURIHelper)22 URI (java.net.URI)20 InputStream (java.io.InputStream)11 CouchDbUtil.getAsString (com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getAsString)3 HttpConnection (com.cloudant.http.HttpConnection)3 JsonObject (com.google.gson.JsonObject)3 InputStreamReader (java.io.InputStreamReader)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 CouchDbUtil.getResponse (com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getResponse)2 Reader (java.io.Reader)2 QueryResult (com.cloudant.client.api.query.QueryResult)1 AllDocsRequestResponse (com.cloudant.client.internal.views.AllDocsRequestResponse)1 Response (com.cloudant.client.org.lightcouch.Response)1 CouchDbUtil.streamToString (com.cloudant.client.org.lightcouch.internal.CouchDbUtil.streamToString)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1