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;
}
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);
}
}
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);
}
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));
}
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);
}
}
Aggregations