use of com.ibm.cloud.cloudant.v1.Cloudant in project knative-eventing-java-app by IBM.
the class SBApplicationConfig method cloudant.
/**
* Enables Spring to automatically create <code>Cloudant</code> instances
* configured to connect to the bound Cloudant client.
*
* @return A fully-configured <code>Cloudant</code> instance.
*/
@ConditionalOnMissingBean
@Bean
public Cloudant cloudant() {
// Create the authenticator.
Authenticator authenticator = new BasicAuthenticator.Builder().username(this.cloudantUsername).password(this.cloudantPassword).build();
// Create the client
Cloudant client = new Cloudant("", authenticator);
client.setServiceUrl(cloudantUrl);
return client;
}
use of com.ibm.cloud.cloudant.v1.Cloudant in project knative-eventing-java-app by IBM.
the class CloudEventStoreCloudant method addEvent.
@Override
public void addEvent(CloudEvent<?, ?> event) throws Exception {
// Convert event into document object
Document document = new Document();
// https://github.com/cloudant/java-cloudant/blob/master/MIGRATION.md
document.setProperties(this.gson.fromJson(this.gson.toJson(event), Map.class));
// Post document and get response
PostDocumentOptions postDocumentOptions = new PostDocumentOptions.Builder().db(this.dbName).document(document).build();
DocumentResult response = this.client.postDocument(postDocumentOptions).execute().getResult();
// Check for errors
String error = response.getError();
if (error != null) {
logger.error("Error adding event to Cloudant: " + error);
throw new Exception(error);
}
}
use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.
the class PutCloudantSecurityConfigurationOptionsTest method testPutCloudantSecurityConfigurationOptions.
@Test
public void testPutCloudantSecurityConfigurationOptions() throws Throwable {
SecurityObject securityObjectModel = new SecurityObject.Builder().names(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).roles(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
assertEquals(securityObjectModel.names(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
assertEquals(securityObjectModel.roles(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
PutCloudantSecurityConfigurationOptions putCloudantSecurityConfigurationOptionsModel = new PutCloudantSecurityConfigurationOptions.Builder().db("testString").cloudant(new java.util.HashMap<String, List<String>>() {
{
put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
}
}).admins(securityObjectModel).members(securityObjectModel).couchdbAuthOnly(true).build();
assertEquals(putCloudantSecurityConfigurationOptionsModel.db(), "testString");
assertEquals(putCloudantSecurityConfigurationOptionsModel.cloudant(), new java.util.HashMap<String, List<String>>() {
{
put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
}
});
assertEquals(putCloudantSecurityConfigurationOptionsModel.admins(), securityObjectModel);
assertEquals(putCloudantSecurityConfigurationOptionsModel.members(), securityObjectModel);
assertEquals(putCloudantSecurityConfigurationOptionsModel.couchdbAuthOnly(), Boolean.valueOf(true));
}
use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.
the class CreateDbAndDoc method main.
public static void main(String[] args) {
// 1. Create a client with `CLOUDANT` default service name ============
Cloudant client = Cloudant.newInstance();
// 2. Create a database ===============================================
// Create a database object with "orders" id
String exampleDbName = "orders";
PutDatabaseOptions putDbOptions = new PutDatabaseOptions.Builder().db(exampleDbName).build();
// Try to create database if it doesn't exist
try {
Ok putDatabaseResult = client.putDatabase(putDbOptions).execute().getResult();
if (putDatabaseResult.isOk()) {
System.out.println("\"" + exampleDbName + "\" database created.");
}
} catch (ServiceResponseException sre) {
if (sre.getStatusCode() == 412)
System.out.println("Cannot create \"" + exampleDbName + "\" database, it already exists.");
}
// 3. Create a document ===============================================
// Create a document object with "example" id
String exampleDocId = "example";
Document exampleDocument = new Document();
// Setting id for the document is optional when "postDocument" method is used for CREATE.
// When id is not provided the server will generate one for your document.
exampleDocument.setId(exampleDocId);
// Add "name" and "joined" fields to the document
exampleDocument.put("name", "Bob Smith");
exampleDocument.put("joined", "2019-01-24T10:42:59.000Z");
// Save the document in the database with "postDocument" method
PostDocumentOptions createDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(exampleDocument).build();
DocumentResult createDocumentResponse = client.postDocument(createDocumentOptions).execute().getResult();
// ====================================================================
// Note: saving the document can also be done with the "putDocument"
// method. In this case `docId` is required for a CREATE operation:
/*
PutDocumentOptions createDocumentOptions =
new PutDocumentOptions.Builder()
.db(exampleDbName)
.docId(exampleDocId)
.document(exampleDocument)
.build();
DocumentResult createDocumentResponse = client
.putDocument(createDocumentOptions)
.execute()
.getResult();
*/
// ====================================================================
// Keeping track of the revision number of the document object
// is necessary for further UPDATE/DELETE operations:
exampleDocument.setRev(createDocumentResponse.getRev());
System.out.println("You have created the document:\n" + exampleDocument);
}
use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.
the class DeleteDoc method main.
public static void main(String[] args) {
// 1. Create a client with `CLOUDANT` default service name ============
Cloudant client = Cloudant.newInstance();
// 2. Delete the document =============================================
// Set the options to get the document out of the database if it exists
String exampleDbName = "orders";
String exampleDocId = "example";
GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId(exampleDocId).build();
try {
// Try to get the document if it previously existed in the database
Document document = client.getDocument(documentInfoOptions).execute().getResult();
// Delete the document from the database
DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(exampleDbName).docId(// docId is required for DELETE
exampleDocId).rev(// rev is required for DELETE
document.getRev()).build();
DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
if (deleteDocumentResponse.isOk()) {
System.out.println("You have deleted the document.");
}
} catch (NotFoundException nfe) {
System.out.println("Cannot delete document because " + "either \"orders\" database or the \"example\" " + "document was not found.");
}
}
Aggregations