Search in sources :

Example 1 with DatabaseURIHelper

use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.

the class CouchDatabaseBase method contains.

/**
 * 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");
    InputStream response = null;
    try {
        response = couchDbClient.head(new DatabaseURIHelper(dbUri).documentUri(id));
    } catch (NoDocumentException e) {
        return false;
    } finally {
        close(response);
    }
    return true;
}
Also used : InputStream(java.io.InputStream) DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper)

Example 2 with DatabaseURIHelper

use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.

the class CouchDatabaseBase method bulk.

/**
 * Performs a Bulk Documents insert request.
 *
 * @param objects      The {@link List} of objects.
 * @param allOrNothing Indicates whether the request has <tt>all-or-nothing</tt> semantics.
 * @return {@code List<Response>} Containing the resulted entries.
 */
public List<Response> bulk(List<?> objects, boolean allOrNothing) {
    assertNotEmpty(objects, "objects");
    InputStream responseStream = null;
    HttpConnection connection;
    try {
        final JsonObject jsonObject = new JsonObject();
        if (allOrNothing) {
            jsonObject.addProperty("all_or_nothing", true);
        }
        final URI uri = new DatabaseURIHelper(dbUri).bulkDocsUri();
        jsonObject.add("docs", getGson().toJsonTree(objects));
        connection = Http.POST(uri, "application/json");
        if (jsonObject.toString().length() != 0) {
            connection.setRequestBody(jsonObject.toString());
        }
        couchDbClient.execute(connection);
        responseStream = connection.responseAsInputStream();
        List<Response> bulkResponses = getResponseList(responseStream, getGson(), DeserializationTypes.LC_RESPONSES);
        for (Response response : bulkResponses) {
            response.setStatusCode(connection.getConnection().getResponseCode());
            response.setReason(connection.getConnection().getResponseMessage());
        }
        return bulkResponses;
    } catch (IOException e) {
        throw new CouchDbException("Error retrieving response input stream.", e);
    } finally {
        close(responseStream);
    }
}
Also used : CouchDbUtil.getResponse(com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getResponse) HttpConnection(com.cloudant.http.HttpConnection) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) IOException(java.io.IOException) URI(java.net.URI)

Example 3 with DatabaseURIHelper

use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.

the class CouchDbClient method put.

/**
 * Performs a HTTP PUT request, saves or updates a document.
 *
 * @param object    Object for updating request
 * @param newEntity If true, saves a new document. Else, updates an existing one.
 * @return {@link Response}
 */
public Response put(URI uri, Object object, boolean newEntity, int writeQuorum) {
    assertNotEmpty(object, "object");
    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");
    }
    URI httpUri = null;
    if (writeQuorum > -1) {
        httpUri = new DatabaseURIHelper(uri).documentUri(id, "w", writeQuorum);
    } else {
        httpUri = new DatabaseURIHelper(uri).documentUri(id);
    }
    HttpConnection connection = Http.PUT(httpUri, "application/json");
    connection.setRequestBody(json.toString());
    return executeToResponse(connection);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) JsonObject(com.google.gson.JsonObject) DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) CouchDbUtil.getAsString(com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getAsString) URI(java.net.URI)

Example 4 with DatabaseURIHelper

use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.

the class CouchDbClient method createDB.

/**
 * Requests CouchDB creates a new database; if one doesn't exist.
 *
 * @param dbName The Database name
 */
public void createDB(String dbName) {
    assertNotEmpty(dbName, "dbName");
    final URI uri = new DatabaseURIHelper(getBaseUri(), dbName).getDatabaseUri();
    executeToResponse(Http.PUT(uri, "application/json"));
    log.info(String.format("Created Database: '%s'", dbName));
}
Also used : DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) URI(java.net.URI)

Example 5 with DatabaseURIHelper

use of com.cloudant.client.internal.DatabaseURIHelper in project java-cloudant by cloudant.

the class Replication method trigger.

/**
 * Triggers a replication request.
 */
public ReplicationResult trigger() {
    assertNotEmpty(source, "Source");
    assertNotEmpty(target, "Target");
    InputStream response = null;
    try {
        JsonObject json = createJson();
        if (log.isLoggable(Level.FINE)) {
            log.fine(json.toString());
        }
        final URI uri = new DatabaseURIHelper(client.getBaseUri()).path("_replicate").build();
        response = client.post(uri, json.toString());
        final InputStreamReader reader = new InputStreamReader(response, "UTF-8");
        return client.getGson().fromJson(reader, ReplicationResult.class);
    } catch (UnsupportedEncodingException e) {
        // to support UTF-8.
        throw new RuntimeException(e);
    } finally {
        close(response);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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