From 23f68480b6a8ca473bf68bfb5e854e5b5419140c Mon Sep 17 00:00:00 2001 From: revijay Date: Thu, 21 Nov 2024 14:28:50 +0530 Subject: [PATCH 01/22] Created cloudant-with-open-liberty.adoc --- ...2024-11-25-cloudant-with-open-liberty.adoc | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 posts/2024-11-25-cloudant-with-open-liberty.adoc diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc new file mode 100644 index 000000000..a54715df5 --- /dev/null +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -0,0 +1,170 @@ +--- +layout: post +title: "Access Cloudant client with Open Liberty using CDI" +categories: blog +author_picture: https://avatars3.githubusercontent.com/revijay +author_github: https://github.com/revijay +seo-title: Access Cloudant client with Open Liberty using CDI - OpenLiberty.io +seo-description: Using Cloudant with Open Liberty previously meant enabling the cloudant-1.0 feature and configuring several elements in server.xml. Now, with improvements in CDI and the introduction of MicroProfile Config, you can easily configure access to Cloudant with a CDI producer. Earlier cloudant-1.0 feature was implemented using Java Cloudant Client library which is not under active development now, so in this demonstration we are using Cloudant Java SDK library. +blog_description: "Using Cloudant with Open Liberty previously meant enabling the cloudant-1.0 feature and configuring several elements in server.xml. Now, with improvements in CDI and the introduction of MicroProfile Config, you can easily configure access to Cloudant with a CDI producer. Earlier cloudant-1.0 feature was implemented using Java Cloudant Client library which is not under active development now, so in this demonstration we are using Cloudant Java SDK library." +open-graph-image: https://openliberty.io/img/twitter_card.jpg +open-graph-image-alt: Open Liberty Logo +--- += Access Cloudant client with Open Liberty using CDI +Reshmi Vijayan +:imagesdir: / +:url-prefix: +:url-about: / + +Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. Now, with improvements in CDI and the introduction of MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). Earlier cloudant-1.0 feature was implemented using Java Cloudant Client library which is not under active development now, so in this demonstration we are using Cloudant Java SDK library. + +== A CDI producer for Cloudant +With a CDI producer, you can easily provide a Cloudant client to your application by which you can inject the client into various parts of the application in a type-safe and flexible way, while leveraging the benefits of dependency injection (such as lifecycle management and configuration).This example demonstrates how to create a CDI producer to inject a Cloudant client: +[source, java] +---- +@ApplicationScoped +public class CloudantProducer { + + @Produces + public Cloudant createCloudant() { + return new Cloudant(“cloudant”, new BasicAuthenticator.Builder().build()); + } +} +---- + +Here is an example of using the CDI producer to inject a Cloudant client in a JAX-RS application. +[source, java] +---- +@Inject +Cloudant client; +@POST +@Path("/add") +@Consumes(MediaType.APPLICATION_JSON) +public void add(CrewMember crewMember) { + Document newCrewMember = new Document(); + newCrewMember.put("Name",crewMember.getName()); + newCrewMember.put("Rank",crewMember.getRank()); + newCrewMember.put("CrewID",crewMember.getCrewID()); + + PostDocumentOptions createDocumentOptions = + new PostDocumentOptions.Builder() + .db(dbname) + .document(newCrewMember) + .build(); + DocumentResult createDocumentResponse = client + .postDocument(createDocumentOptions) + .execute() + .getResult(); +} +---- +In the above code: + +* `@Inject` is used to inject the Cloudant client instance provided by the CDI producer. +* The `postDocument` method demonstrates how to create and post a document in the cloudant database using the injected CloudantClient. + +== Enhancing the CDI producer +One of the advantages of using a CDI producer is that it can be tailored to your needs. For improved security,the `createCloudant` method can be enhanced to include authentication with a user name and password: + +[source,java] +---- +@Produces +public Cloudant createCloudant() { + String password = PasswordUtil.passwordDecode(encodedPassword); + BasicAuthenticator authenticator = new BasicAuthenticator.Builder() + .username(username) + .password(password) + .build(); + + return new Cloudant("cloudant", authenticator); +} +---- +This requires `passwordUtilities-1.0`, available as a Maven dependency: +[source,xml] +---- + + io.openliberty.features + passwordUtilities-1.0 + 18.0.0.4 + +---- + +You also need to enable the feature in `server.xml`: +[source, xml] +---- +passwordUtilities-1.0 +---- + +By default, Cloudant communication occurs over HTTPS (SSL). However, if you're working with a custom Cloudant instance or self-signed certificates, you may need to configure your Open Liberty application to trust the custom certificate. + +You can configure SSL by modifying the server.xml to use your custom SSL certificates: +[source, xml] +---- + + + + + + +---- + +== Configure with MicroProfile Config +Now that you have a more advanced CDI producer, it would be nice to add configurability to some of the variables, like the user and password. Using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] makes configuring the Cloudant driver simple. You can add the following to your CDI producer to add configuration: +[source, java] +---- +@Inject + @ConfigProperty(name = "cloudant.host", defaultValue = "localhost") + String host; + + @Inject + @ConfigProperty(name = "cloudant.port", defaultValue = "5984") + String port; + + @Inject + @ConfigProperty(name = "cloudant.username") + String username; + + @Inject + @ConfigProperty(name = "cloudant.password") + String password; + + @Inject + @ConfigProperty(name = "cloudant.dbname") + String dbname; +---- +Now, by placing the following snippet in your ``microprofile-config.properties`` or `server.env` file, the values for user and password will be pulled into the CloudantProducer class: +[source, text] +---- +cloudant.user=admin +cloudant.password={aes}AEEjCqvh7XAwDxrdYC6BUbqYlwqI8NAxRkWWWq7muxZu +---- +== No need for a Cloudant feature +Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant Java Driver API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using newer versions of Cloudant with a CDI producer. + +The Cloudant client should be bundled in your application. To do this with Maven you can use a dependency: + +[source, xml] +---- + + com.ibm.cloud + cloudant + x.x.x + +---- +If you have multiple applications accessing Cloudant, instead of bundling the Cloudant client library, you can configure a shared library in your `server.xml` like this: +[source, xml] +---- + + + + + + + + + + + +---- + +This illustrates how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config that help you to integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. +The full sample is available on GitHub here:link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant] \ No newline at end of file From 56c8ec854109353994757c868f6958e5c8a4febe Mon Sep 17 00:00:00 2001 From: revijay Date: Sun, 24 Nov 2024 14:51:00 +0530 Subject: [PATCH 02/22] Updated blog file cloudant-with-open-liberty.adoc --- ...2024-11-25-cloudant-with-open-liberty.adoc | 171 ++++++++++-------- 1 file changed, 93 insertions(+), 78 deletions(-) mode change 100644 => 100755 posts/2024-11-25-cloudant-with-open-liberty.adoc diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc old mode 100644 new mode 100755 index a54715df5..b4c1b49f8 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -1,12 +1,12 @@ --- layout: post -title: "Access Cloudant client with Open Liberty using CDI" +title: "Access Cloudant with Open Liberty using CDI" categories: blog author_picture: https://avatars3.githubusercontent.com/revijay author_github: https://github.com/revijay -seo-title: Access Cloudant client with Open Liberty using CDI - OpenLiberty.io -seo-description: Using Cloudant with Open Liberty previously meant enabling the cloudant-1.0 feature and configuring several elements in server.xml. Now, with improvements in CDI and the introduction of MicroProfile Config, you can easily configure access to Cloudant with a CDI producer. Earlier cloudant-1.0 feature was implemented using Java Cloudant Client library which is not under active development now, so in this demonstration we are using Cloudant Java SDK library. -blog_description: "Using Cloudant with Open Liberty previously meant enabling the cloudant-1.0 feature and configuring several elements in server.xml. Now, with improvements in CDI and the introduction of MicroProfile Config, you can easily configure access to Cloudant with a CDI producer. Earlier cloudant-1.0 feature was implemented using Java Cloudant Client library which is not under active development now, so in this demonstration we are using Cloudant Java SDK library." +seo-title: Access Cloudant with Open Liberty using CDI - OpenLiberty.io +seo-description: Using Cloudant with Open Liberty previously meant enabling the cloudant-1.0 feature and configuring several elements in server.xml. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer. The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. +blog_description: "Using Cloudant with Open Liberty previously meant enabling the cloudant-1.0 feature and configuring several elements in server.xml. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer. The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java." open-graph-image: https://openliberty.io/img/twitter_card.jpg open-graph-image-alt: Open Liberty Logo --- @@ -16,27 +16,80 @@ Reshmi Vijayan :url-prefix: :url-about: / -Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. Now, with improvements in CDI and the introduction of MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). Earlier cloudant-1.0 feature was implemented using Java Cloudant Client library which is not under active development now, so in this demonstration we are using Cloudant Java SDK library. +Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. == A CDI producer for Cloudant -With a CDI producer, you can easily provide a Cloudant client to your application by which you can inject the client into various parts of the application in a type-safe and flexible way, while leveraging the benefits of dependency injection (such as lifecycle management and configuration).This example demonstrates how to create a CDI producer to inject a Cloudant client: +With a CDI producer, you can easily provide a Cloudant client to your application by which you can inject the client into various parts of the application in a type-safe and flexible way, while leveraging the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to make the configuration of Cloudant driver simple. +This example demonstrates how to create a CDI producer to inject a Cloudant client: [source, java] ---- @ApplicationScoped public class CloudantProducer { + @Inject + @ConfigProperty(name = "cloudant.host", defaultValue = "localhost") + String host; + + @Inject + @ConfigProperty(name = "cloudant.port", defaultValue = "5984") + String port; + + @Inject + @ConfigProperty(name = "cloudant.username") + String username; + + @Inject + @ConfigProperty(name = "cloudant.password") + String encodedPassword; + + + @Inject + @ConfigProperty(name = "cloudant.dbname") + String dbname; + @Produces public Cloudant createCloudant() { - return new Cloudant(“cloudant”, new BasicAuthenticator.Builder().build()); + String password = PasswordUtil.passwordDecode(encodedPassword); + BasicAuthenticator authenticator = new BasicAuthenticator.Builder() + .username(username) + .password(password) + .build(); + + Cloudant service = new Cloudant("cloudant", authenticator); + service.setServiceUrl("http://" + host + ":" + port); + + return service; } } ---- +One of the advantages of using a CDI producer is that it can be tailored to your needs. For improved security,the createCloudant method is enhanced to include authentication with a user name and password. This requires the following Maven dependency: +[source,xml] +---- + + com.ibm.websphere.appserver.api + com.ibm.websphere.appserver.api.passwordUtil + 1.0.95 + +---- +You also need to enable the feature in `server.xml`: +[source, xml] +---- +passwordUtilities-1.0 +---- +Now, by placing the following snippet in your ``microprofile-config.properties`` or `server.env` file, the values for user and password will be pulled into the CloudantProducer class: +[source, text] +---- +cloudant.user=admin +cloudant.password={aes}AEEjCqvh7XAwDxrdYC6BUbqYlwqI8NAxRkWWWq7muxZu +---- +== Injecting the Cloudant client Here is an example of using the CDI producer to inject a Cloudant client in a JAX-RS application. [source, java] ---- @Inject Cloudant client; + @POST @Path("/add") @Consumes(MediaType.APPLICATION_JSON) @@ -57,90 +110,52 @@ public void add(CrewMember crewMember) { .getResult(); } ---- + In the above code: * `@Inject` is used to inject the Cloudant client instance provided by the CDI producer. -* The `postDocument` method demonstrates how to create and post a document in the cloudant database using the injected CloudantClient. +* The `PostDocumentOptions` is a builder class that allows you to specify various options when posting the document. You must provide the db name and the document content. +* The `postDocument` is the main method that posts the document to the specified database. It returns a DocumentResult object which contains metadata about the inserted document (like its _id and _rev). -== Enhancing the CDI producer -One of the advantages of using a CDI producer is that it can be tailored to your needs. For improved security,the `createCloudant` method can be enhanced to include authentication with a user name and password: - -[source,java] +Similarly, we can delete a document from Cloudant using its `id`. +[source, java] ---- -@Produces -public Cloudant createCloudant() { - String password = PasswordUtil.passwordDecode(encodedPassword); - BasicAuthenticator authenticator = new BasicAuthenticator.Builder() - .username(username) - .password(password) +@DELETE +@Path("/{id}") +public String remove(@PathParam("id") String id) { + GetDocumentOptions documentInfoOptions = + new GetDocumentOptions.Builder() + .db(dbname) + .docId(id) .build(); - return new Cloudant("cloudant", authenticator); -} ----- -This requires `passwordUtilities-1.0`, available as a Maven dependency: -[source,xml] ----- - - io.openliberty.features - passwordUtilities-1.0 - 18.0.0.4 - ----- - -You also need to enable the feature in `server.xml`: -[source, xml] ----- -passwordUtilities-1.0 ----- - -By default, Cloudant communication occurs over HTTPS (SSL). However, if you're working with a custom Cloudant instance or self-signed certificates, you may need to configure your Open Liberty application to trust the custom certificate. - -You can configure SSL by modifying the server.xml to use your custom SSL certificates: -[source, xml] ----- - - - - - - ----- + Document document = client + .getDocument(documentInfoOptions) + .execute() + .getResult(); -== Configure with MicroProfile Config -Now that you have a more advanced CDI producer, it would be nice to add configurability to some of the variables, like the user and password. Using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] makes configuring the Cloudant driver simple. You can add the following to your CDI producer to add configuration: -[source, java] + DeleteDocumentOptions deleteDocumentOptions = + new DeleteDocumentOptions.Builder() + .db(dbname) + .docId(id) + .rev(document.getRev()) + .build(); + + DocumentResult deleteDocumentResponse = client + .deleteDocument(deleteDocumentOptions) + .execute() + .getResult(); +} ---- -@Inject - @ConfigProperty(name = "cloudant.host", defaultValue = "localhost") - String host; - - @Inject - @ConfigProperty(name = "cloudant.port", defaultValue = "5984") - String port; +In the above example: - @Inject - @ConfigProperty(name = "cloudant.username") - String username; +* The `GetDocumentOptions` class is used to configure parameters for retrieving a document from a Cloudant database. It allows you to specify the database name, document ID, and optional parameters like the document revision, whether to include attachments, conflicts, or deleted information in the response. The class uses the builder pattern to set these options before making the request to Cloudant. +* The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (_rev) of the document to ensure that the correct version is deleted (to prevent race conditions). This class uses the builder pattern to set options before sending the delete request to Cloudant. - @Inject - @ConfigProperty(name = "cloudant.password") - String password; - - @Inject - @ConfigProperty(name = "cloudant.dbname") - String dbname; ----- -Now, by placing the following snippet in your ``microprofile-config.properties`` or `server.env` file, the values for user and password will be pulled into the CloudantProducer class: -[source, text] ----- -cloudant.user=admin -cloudant.password={aes}AEEjCqvh7XAwDxrdYC6BUbqYlwqI8NAxRkWWWq7muxZu ----- == No need for a Cloudant feature -Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant Java Driver API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using newer versions of Cloudant with a CDI producer. +Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant Java Driver API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using the new Cloudant SDK for Java. -The Cloudant client should be bundled in your application. To do this with Maven you can use a dependency: +The Cloudant SDK for Java should be bundled in your application. To do this with Maven you can use a dependency: [source, xml] ---- From 753eb7709fd1e9f3c36b3f7cf6ca4ff1fed38bf7 Mon Sep 17 00:00:00 2001 From: revijay Date: Tue, 26 Nov 2024 09:41:50 +0530 Subject: [PATCH 03/22] Incorporated review comments --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index b4c1b49f8..23dc0a89a 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -42,11 +42,6 @@ public class CloudantProducer { @ConfigProperty(name = "cloudant.password") String encodedPassword; - - @Inject - @ConfigProperty(name = "cloudant.dbname") - String dbname; - @Produces public Cloudant createCloudant() { String password = PasswordUtil.passwordDecode(encodedPassword); @@ -62,7 +57,7 @@ public class CloudantProducer { } } ---- -One of the advantages of using a CDI producer is that it can be tailored to your needs. For improved security,the createCloudant method is enhanced to include authentication with a user name and password. This requires the following Maven dependency: +One of the advantages of using a CDI producer is that it can be tailored to your needs. For improved security,the createCloudant method uses Open Liberty's password decoding. This requires the following Maven dependency: [source,xml] ---- @@ -81,6 +76,7 @@ Now, by placing the following snippet in your ``microprofile-config.properties`` ---- cloudant.user=admin cloudant.password={aes}AEEjCqvh7XAwDxrdYC6BUbqYlwqI8NAxRkWWWq7muxZu +cloudant.dbname=testdb ---- == Injecting the Cloudant client @@ -90,6 +86,10 @@ Here is an example of using the CDI producer to inject a Cloudant client in a JA @Inject Cloudant client; +@Inject +@ConfigProperty(name = "cloudant.dbname") +String dbname; + @POST @Path("/add") @Consumes(MediaType.APPLICATION_JSON) @@ -153,7 +153,7 @@ In the above example: * The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (_rev) of the document to ensure that the correct version is deleted (to prevent race conditions). This class uses the builder pattern to set options before sending the delete request to Cloudant. == No need for a Cloudant feature -Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant Java Driver API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using the new Cloudant SDK for Java. +Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant SDK for Java's API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using the new Cloudant SDK for Java. The Cloudant SDK for Java should be bundled in your application. To do this with Maven you can use a dependency: @@ -165,7 +165,7 @@ The Cloudant SDK for Java should be bundled in your application. To do this with x.x.x ---- -If you have multiple applications accessing Cloudant, instead of bundling the Cloudant client library, you can configure a shared library in your `server.xml` like this: +If you have multiple applications accessing Cloudant, instead of bundling the Cloudant SDK for Java with each application, you can configure a shared library in your `server.xml` like this: [source, xml] ---- From 546fb88342977d4786ce6e1b3ebe81be8828d60c Mon Sep 17 00:00:00 2001 From: revijay Date: Wed, 27 Nov 2024 00:07:46 +0530 Subject: [PATCH 04/22] Updated blog to add an intro and summary section --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 23dc0a89a..4a3b5cf63 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -16,6 +16,8 @@ Reshmi Vijayan :url-prefix: :url-about: / +Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a powerful solution for document-oriented storage with high availability and scalability. Cloudant provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Its ability to scale flawless, offer high availability, and support real-time data access makes it a perfect fit for dynamic, modern applications running in the cloud. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient, scalable data storage. + Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. == A CDI producer for Cloudant @@ -181,5 +183,11 @@ If you have multiple applications accessing Cloudant, instead of bundling the Cl ---- -This illustrates how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config that help you to integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. -The full sample is available on GitHub here:link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant] \ No newline at end of file +== Summary +In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config that help you to integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. +The full sample is available on GitHub here:link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant]. + +By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. + +== Next Steps +As your application evolves, consider exploring more advanced capabilities of both Cloudant and Open Liberty to unlock their full potential in building resilient, high-performance cloud-native applications. Whether you're scaling your infrastructure, improving security, or enhancing performance, the possibilities are vast for developers leveraging these technologies together. \ No newline at end of file From 6a45f8465a873d24d9a0c09318716e21d0097805 Mon Sep 17 00:00:00 2001 From: revijay Date: Wed, 27 Nov 2024 13:52:47 +0530 Subject: [PATCH 05/22] Updated introduction and added some more information in the blog --- ...2024-11-25-cloudant-with-open-liberty.adoc | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 4a3b5cf63..9bf4b30e2 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -16,7 +16,7 @@ Reshmi Vijayan :url-prefix: :url-about: / -Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a powerful solution for document-oriented storage with high availability and scalability. Cloudant provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Its ability to scale flawless, offer high availability, and support real-time data access makes it a perfect fit for dynamic, modern applications running in the cloud. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient, scalable data storage. +Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a powerful solution for document-oriented storage with high availability and scalability. Cloudant provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient, scalable data storage. Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. @@ -81,6 +81,41 @@ cloudant.password={aes}AEEjCqvh7XAwDxrdYC6BUbqYlwqI8NAxRkWWWq7muxZu cloudant.dbname=testdb ---- +Instead of using `BasicAuthentication` with a username and password, Cloudant also supports `IAM (Identity and Access Management)` authentication, which allows users and applications to authenticate using secure API keys or IAM tokens. By using IAM authentication, we can avoid the risks associated with managing passwords and ensure that only authorized entities can interact with Cloudant databases. +Follow https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant&_ga=2.42518757.1165320250.1604321683-1614077795.1594908456[IBM Cloudant Setup] guide to create a Cloudant instance and API key for accessing it. + +Bellow code snippet shows how can we create a CDI Producer to inject Cloudant client with IAM authenticator. +[source, java] +---- +@ApplicationScoped +public class CloudantProducer { + + @Inject + @ConfigProperty(name = "cloudant.host", defaultValue = "localhost") + String host; + + @Inject + @ConfigProperty(name = "cloudant.port", defaultValue = "5984") + String port; + + @Inject + @ConfigProperty(name = "cloudant.apikey") + String apikey; + + @Produces + public Cloudant createCloudant() { + IamAuthenticator authenticator = new IamAuthenticator.Builder() + .apikey("apikey") + .build(); + + Cloudant service = new Cloudant("cloudant", authenticator); + service.setServiceUrl("http://" + host + ":" + port); + + return service; + } +} +---- + == Injecting the Cloudant client Here is an example of using the CDI producer to inject a Cloudant client in a JAX-RS application. [source, java] @@ -106,7 +141,7 @@ public void add(CrewMember crewMember) { .db(dbname) .document(newCrewMember) .build(); - DocumentResult createDocumentResponse = client + DocumentResult createDocumentResponse = client .postDocument(createDocumentOptions) .execute() .getResult(); @@ -187,7 +222,4 @@ If you have multiple applications accessing Cloudant, instead of bundling the Cl In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config that help you to integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. The full sample is available on GitHub here:link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant]. -By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. - -== Next Steps -As your application evolves, consider exploring more advanced capabilities of both Cloudant and Open Liberty to unlock their full potential in building resilient, high-performance cloud-native applications. Whether you're scaling your infrastructure, improving security, or enhancing performance, the possibilities are vast for developers leveraging these technologies together. \ No newline at end of file +By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. \ No newline at end of file From 4a50e247224f482600564108e48227419ab490ca Mon Sep 17 00:00:00 2001 From: revijay Date: Wed, 27 Nov 2024 20:29:31 +0530 Subject: [PATCH 06/22] Incorporated review comments --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 9bf4b30e2..439ee413e 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -82,9 +82,9 @@ cloudant.dbname=testdb ---- Instead of using `BasicAuthentication` with a username and password, Cloudant also supports `IAM (Identity and Access Management)` authentication, which allows users and applications to authenticate using secure API keys or IAM tokens. By using IAM authentication, we can avoid the risks associated with managing passwords and ensure that only authorized entities can interact with Cloudant databases. -Follow https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant&_ga=2.42518757.1165320250.1604321683-1614077795.1594908456[IBM Cloudant Setup] guide to create a Cloudant instance and API key for accessing it. +Follow the https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant&_ga=2.42518757.1165320250.1604321683-1614077795.1594908456[IBM Cloudant Setup] guide to create a Cloudant instance and API key for accessing it. -Bellow code snippet shows how can we create a CDI Producer to inject Cloudant client with IAM authenticator. +The code snippet below shows how can we create a CDI Producer to inject a Cloudant client with IAM authentication. [source, java] ---- @ApplicationScoped @@ -104,7 +104,7 @@ public class CloudantProducer { @Produces public Cloudant createCloudant() { - IamAuthenticator authenticator = new IamAuthenticator.Builder() + IamAuthenticator authenticator = new IamAuthenticator.Builder() .apikey("apikey") .build(); @@ -219,7 +219,7 @@ If you have multiple applications accessing Cloudant, instead of bundling the Cl ---- == Summary -In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config that help you to integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. -The full sample is available on GitHub here:link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant]. +In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config to help integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. +The full sample is available on GitHub here: link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant]. By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. \ No newline at end of file From f9125dc109fc61745a567d352244e910abd05c74 Mon Sep 17 00:00:00 2001 From: Mark Swatosh Date: Wed, 27 Nov 2024 09:52:42 -0600 Subject: [PATCH 07/22] Remove unnecessary URL params --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 439ee413e..a27a3a8a4 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -82,7 +82,7 @@ cloudant.dbname=testdb ---- Instead of using `BasicAuthentication` with a username and password, Cloudant also supports `IAM (Identity and Access Management)` authentication, which allows users and applications to authenticate using secure API keys or IAM tokens. By using IAM authentication, we can avoid the risks associated with managing passwords and ensure that only authorized entities can interact with Cloudant databases. -Follow the https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant&_ga=2.42518757.1165320250.1604321683-1614077795.1594908456[IBM Cloudant Setup] guide to create a Cloudant instance and API key for accessing it. +Follow the https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant[IBM Cloudant Setup] guide to create a Cloudant instance and API key for accessing it. The code snippet below shows how can we create a CDI Producer to inject a Cloudant client with IAM authentication. [source, java] From 2194585aa999a33b27e17c7689dca8e030058366 Mon Sep 17 00:00:00 2001 From: revijay Date: Thu, 28 Nov 2024 16:02:57 +0530 Subject: [PATCH 08/22] committed some minor changes in the code when working with IAM --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 439ee413e..092a08a3d 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -91,13 +91,9 @@ The code snippet below shows how can we create a CDI Producer to inject a Clouda public class CloudantProducer { @Inject - @ConfigProperty(name = "cloudant.host", defaultValue = "localhost") + @ConfigProperty(name = "cloudant.host") String host; - @Inject - @ConfigProperty(name = "cloudant.port", defaultValue = "5984") - String port; - @Inject @ConfigProperty(name = "cloudant.apikey") String apikey; @@ -109,7 +105,7 @@ public class CloudantProducer { .build(); Cloudant service = new Cloudant("cloudant", authenticator); - service.setServiceUrl("http://" + host + ":" + port); + service.setServiceUrl("https://" + host); return service; } From 4d006468f34d32654431c30ec9da9049c2dcedc0 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 09:56:57 +0530 Subject: [PATCH 09/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index f0a0c2fd2..ea090f2ac 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -16,7 +16,7 @@ Reshmi Vijayan :url-prefix: :url-about: / -Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a powerful solution for document-oriented storage with high availability and scalability. Cloudant provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient, scalable data storage. +Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient scalable data storage. Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. From b4e8d46f64b12139a91729614dfae58dedb60451 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 10:06:15 +0530 Subject: [PATCH 10/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index ea090f2ac..131cac9f9 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -18,7 +18,7 @@ Reshmi Vijayan Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient scalable data storage. -Using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). The cloudant-1.0 feature was implemented using Java Cloudant Client library which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. +Previously, using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). == A CDI producer for Cloudant With a CDI producer, you can easily provide a Cloudant client to your application by which you can inject the client into various parts of the application in a type-safe and flexible way, while leveraging the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to make the configuration of Cloudant driver simple. From f346a674a988b6b5c3c28099b28d8cd5eb413743 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 10:07:25 +0530 Subject: [PATCH 11/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 131cac9f9..4a8e652c7 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -21,7 +21,7 @@ Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides Previously, using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). == A CDI producer for Cloudant -With a CDI producer, you can easily provide a Cloudant client to your application by which you can inject the client into various parts of the application in a type-safe and flexible way, while leveraging the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to make the configuration of Cloudant driver simple. +With a CDI producer, you can easily provide a Cloudant client to your application and inject the client into various parts of the application in a type-safe and flexible way, with the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to simplify the configuration of the Cloudant driver. This example demonstrates how to create a CDI producer to inject a Cloudant client: [source, java] ---- From 5fec7c7e02ea3d586d357c3d5c5c72878235a15f Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 11:01:12 +0530 Subject: [PATCH 12/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 4a8e652c7..ed86fe18b 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -59,7 +59,7 @@ public class CloudantProducer { } } ---- -One of the advantages of using a CDI producer is that it can be tailored to your needs. For improved security,the createCloudant method uses Open Liberty's password decoding. This requires the following Maven dependency: +One advantage of using a CDI producer is that you can tailor it to your needs. For improved security, the `createCloudant` method uses Open Liberty's password decoding, which requires the following Maven dependency: [source,xml] ---- From e101667f8187e2fe86ee1d09fde2c146d8e4db05 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 11:01:48 +0530 Subject: [PATCH 13/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index ed86fe18b..d8885f5b5 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -68,7 +68,7 @@ One advantage of using a CDI producer is that you can tailor it to your needs. F 1.0.95 ---- -You also need to enable the feature in `server.xml`: +You must also enable the Password Utilities feature in your `server.xml`file: [source, xml] ---- passwordUtilities-1.0 From d4d0009ff5cbb47ff7fe260da4fbf12309b941d8 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 11:02:08 +0530 Subject: [PATCH 14/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index d8885f5b5..fa86dd31b 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -180,7 +180,7 @@ public String remove(@PathParam("id") String id) { .getResult(); } ---- -In the above example: +In the previous example: * The `GetDocumentOptions` class is used to configure parameters for retrieving a document from a Cloudant database. It allows you to specify the database name, document ID, and optional parameters like the document revision, whether to include attachments, conflicts, or deleted information in the response. The class uses the builder pattern to set these options before making the request to Cloudant. * The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (_rev) of the document to ensure that the correct version is deleted (to prevent race conditions). This class uses the builder pattern to set options before sending the delete request to Cloudant. From 78da96d133a5832190c3033b796f0726f67a263b Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 11:03:06 +0530 Subject: [PATCH 15/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index fa86dd31b..9e81a4d70 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -183,7 +183,7 @@ public String remove(@PathParam("id") String id) { In the previous example: * The `GetDocumentOptions` class is used to configure parameters for retrieving a document from a Cloudant database. It allows you to specify the database name, document ID, and optional parameters like the document revision, whether to include attachments, conflicts, or deleted information in the response. The class uses the builder pattern to set these options before making the request to Cloudant. -* The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (_rev) of the document to ensure that the correct version is deleted (to prevent race conditions). This class uses the builder pattern to set options before sending the delete request to Cloudant. +* The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (`_rev`) of the document to ensure that the correct version is deleted and prevent race conditions. This class uses the builder pattern to set options before sending the delete request to Cloudant. == No need for a Cloudant feature Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant SDK for Java's API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using the new Cloudant SDK for Java. From 37cd405a6794601e7d29e3d2a13c910302571c50 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 11:03:40 +0530 Subject: [PATCH 16/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 9e81a4d70..046c5a739 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -188,7 +188,7 @@ In the previous example: == No need for a Cloudant feature Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant SDK for Java's API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using the new Cloudant SDK for Java. -The Cloudant SDK for Java should be bundled in your application. To do this with Maven you can use a dependency: +Bundle the Cloudant SDK for Java in your application. To do this with Maven you can use a dependency: [source, xml] ---- From 51ba3ce2905e872f7e66c275ba88881f10e1876e Mon Sep 17 00:00:00 2001 From: revijay Date: Wed, 4 Dec 2024 12:15:43 +0530 Subject: [PATCH 17/22] Updated based on review comments --- ...2024-11-25-cloudant-with-open-liberty.adoc | 71 ++++++++++++------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 046c5a739..cae381b60 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -20,6 +20,47 @@ Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides Previously, using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). +== Bundle the Cloudant SDK for Java + +Prior to implementing the functionality, make sure to configure the essential dependencies in your application. Depending on your build tool (Maven or Gradle), you can include the necessary Cloudant SDK libraries by adding the corresponding dependency. + +* For Maven: Add the below dependency in pom.xml file: + +[source, xml] +---- + + com.ibm.cloud + cloudant + x.x.x + +---- +Be sure to check for the latest version on link:https://central.sonatype.com/?smo=true[Maven Central]. + +* For Gradle: In your build.gradle file, add the following inside the dependencies block: + +[source, groovy] +---- +dependencies { + implementation 'com.ibm.cloud:ibm-cloud-sdk-core:x.x.x' +} +---- + +If you have multiple applications accessing Cloudant, instead of bundling the Cloudant SDK for Java with each application, you can configure a shared library in your `server.xml` like this: +[source, xml] +---- + + + + + + + + + + + +---- + == A CDI producer for Cloudant With a CDI producer, you can easily provide a Cloudant client to your application and inject the client into various parts of the application in a type-safe and flexible way, with the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to simplify the configuration of the Cloudant driver. This example demonstrates how to create a CDI producer to inject a Cloudant client: @@ -186,36 +227,14 @@ In the previous example: * The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (`_rev`) of the document to ensure that the correct version is deleted and prevent race conditions. This class uses the builder pattern to set options before sending the delete request to Cloudant. == No need for a Cloudant feature -Previously, using Cloudant required enabling the `cloudant-1.0` feature. Even if the Cloudant SDK for Java's API changes, simple updates to your CDI producer will allow it to continue to work. You should remove the `cloudant-1.0` feature from your `server.xml` when using the new Cloudant SDK for Java. - -Bundle the Cloudant SDK for Java in your application. To do this with Maven you can use a dependency: - -[source, xml] ----- - - com.ibm.cloud - cloudant - x.x.x - ----- -If you have multiple applications accessing Cloudant, instead of bundling the Cloudant SDK for Java with each application, you can configure a shared library in your `server.xml` like this: -[source, xml] ----- - - - +Previously, integrating Cloudant into your application required enabling the `cloudant-1.0` feature in the `server.xml` file. The CDI producer now automates the integration process, allowing you to inject Cloudant resources directly into your application without needing to explicitly enable the `cloudant-1.0` feature. - - - +What makes this approach even more flexible is that, even if the Cloudant SDK for Java’s API undergoes changes in the future, you won’t need to make extensive updates to your application code, meaning that simple updates to the producer itself will be sufficient to ensure continued compatibility with newer versions of the SDK. - - - ----- +As a result, when migrating to the new Cloudant SDK for Java, it is recommended that you remove the `cloudant-1.0` feature from your server.xml file. This not only streamlines your configuration but also aligns with the new, simplified approach of using the Cloudant CDI producer for integration. == Summary In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config to help integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. -The full sample is available on GitHub here: link:https://github.com/OpenLiberty/sample-cloudant[https://github.com/OpenLiberty/sample-cloudant]. +The full sample is available link:https://github.com/OpenLiberty/sample-cloudant[on GitHub]. By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. \ No newline at end of file From 5ee3fddd49b30b1720e12b49a225b5c1c4cd3c1a Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Wed, 4 Dec 2024 12:45:54 +0530 Subject: [PATCH 18/22] Update 2024-11-25-cloudant-with-open-liberty.adoc Removed extra line --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index cae381b60..1475cd261 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -227,9 +227,7 @@ In the previous example: * The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (`_rev`) of the document to ensure that the correct version is deleted and prevent race conditions. This class uses the builder pattern to set options before sending the delete request to Cloudant. == No need for a Cloudant feature -Previously, integrating Cloudant into your application required enabling the `cloudant-1.0` feature in the `server.xml` file. The CDI producer now automates the integration process, allowing you to inject Cloudant resources directly into your application without needing to explicitly enable the `cloudant-1.0` feature. - -What makes this approach even more flexible is that, even if the Cloudant SDK for Java’s API undergoes changes in the future, you won’t need to make extensive updates to your application code, meaning that simple updates to the producer itself will be sufficient to ensure continued compatibility with newer versions of the SDK. +Previously, integrating Cloudant into your application required enabling the `cloudant-1.0` feature in the `server.xml` file. The CDI producer now automates the integration process, allowing you to inject Cloudant resources directly into your application without needing to explicitly enable the `cloudant-1.0` feature. What makes this approach even more flexible is that, even if the Cloudant SDK for Java’s API undergoes changes in the future, you won’t need to make extensive updates to your application code, meaning that simple updates to the producer itself will be sufficient to ensure continued compatibility with newer versions of the SDK. As a result, when migrating to the new Cloudant SDK for Java, it is recommended that you remove the `cloudant-1.0` feature from your server.xml file. This not only streamlines your configuration but also aligns with the new, simplified approach of using the Cloudant CDI producer for integration. @@ -237,4 +235,4 @@ As a result, when migrating to the new Cloudant SDK for Java, it is recommended In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config to help integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. The full sample is available link:https://github.com/OpenLiberty/sample-cloudant[on GitHub]. -By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. \ No newline at end of file +By combining the strengths of Cloudant and Open Liberty, developers can create powerful, cloud-native applications that are both flexible and scalable, with minimal infrastructure management. This integration ensures real-time data access, robust data storage, and seamless scaling—all essential for modern, data-driven applications in the cloud. From 15816ad2a4052206d8bf809e10cc0a5722c43db1 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Fri, 6 Dec 2024 20:54:48 +0530 Subject: [PATCH 19/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 1475cd261..1506701f5 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -18,7 +18,9 @@ Reshmi Vijayan Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient scalable data storage. -Previously, using Cloudant with Open Liberty previously meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). +Previously, using Cloudant with Open Liberty meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). + +As a result, if you are migrating from using the `cloudant-1.0` feature to the new Cloudant SDK for Java, you should remove the `cloudant-1.0` feature from your server.xml file. This not only streamlines your configuration but also aligns with the new simplified approach of using the Cloudant CDI producer for integration. == Bundle the Cloudant SDK for Java From af13019e15372124506f2e2df7d0606aedc2b200 Mon Sep 17 00:00:00 2001 From: Reshmi Vijayan Date: Fri, 6 Dec 2024 20:55:30 +0530 Subject: [PATCH 20/22] Update posts/2024-11-25-cloudant-with-open-liberty.adoc Co-authored-by: David Mueller <48686014+dmuelle@users.noreply.github.com> --- posts/2024-11-25-cloudant-with-open-liberty.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-11-25-cloudant-with-open-liberty.adoc index 1506701f5..dd864205f 100755 --- a/posts/2024-11-25-cloudant-with-open-liberty.adoc +++ b/posts/2024-11-25-cloudant-with-open-liberty.adoc @@ -193,7 +193,7 @@ In the above code: * The `PostDocumentOptions` is a builder class that allows you to specify various options when posting the document. You must provide the db name and the document content. * The `postDocument` is the main method that posts the document to the specified database. It returns a DocumentResult object which contains metadata about the inserted document (like its _id and _rev). -Similarly, we can delete a document from Cloudant using its `id`. +Similarly, we can delete a document from Cloudant by using the document `id`. [source, java] ---- @DELETE From 2ea7a9fe0a5a2666e44ef8bbb55447f5fe9f9680 Mon Sep 17 00:00:00 2001 From: revijay Date: Fri, 6 Dec 2024 21:22:52 +0530 Subject: [PATCH 21/22] Renamed post file name --- ...pen-liberty.adoc => 2024-12-9-cloudant-with-open-liberty.adoc} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename posts/{2024-11-25-cloudant-with-open-liberty.adoc => 2024-12-9-cloudant-with-open-liberty.adoc} (100%) diff --git a/posts/2024-11-25-cloudant-with-open-liberty.adoc b/posts/2024-12-9-cloudant-with-open-liberty.adoc similarity index 100% rename from posts/2024-11-25-cloudant-with-open-liberty.adoc rename to posts/2024-12-9-cloudant-with-open-liberty.adoc From 8307d3761a8c0c9f82884e372f5da3b149190820 Mon Sep 17 00:00:00 2001 From: David Mueller Date: Fri, 6 Dec 2024 11:52:19 -0500 Subject: [PATCH 22/22] minor edits --- .../2024-12-9-cloudant-with-open-liberty.adoc | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/posts/2024-12-9-cloudant-with-open-liberty.adoc b/posts/2024-12-9-cloudant-with-open-liberty.adoc index dd864205f..95eb0e741 100755 --- a/posts/2024-12-9-cloudant-with-open-liberty.adoc +++ b/posts/2024-12-9-cloudant-with-open-liberty.adoc @@ -18,15 +18,15 @@ Reshmi Vijayan Cloudant, an IBM-managed NoSQL database built on top of Apache CouchDB, provides a robust, flexible, and highly scalable NoSQL database solution that is tailor-made for cloud-native applications. Cloudant with Open Liberty offers a seamless way to build, deploy, and scale your Java applications with efficient scalable data storage. -Previously, using Cloudant with Open Liberty meant enabling the `cloudant-1.0` feature and configuring several elements in ``server.xml``. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). +Previously, using Cloudant with Open Liberty meant enabling the `cloudant-1.0` feature and configuring several elements in the `server.xml` file. The `cloudant-1.0` feature was implemented using Java Cloudant Client library, which is no longer supported, so in this demonstration we are using the new Cloudant SDK for Java. With CDI and MicroProfile Config, you can easily configure access to Cloudant with a CDI producer (for an introduction to using CDI producers, see the https://openliberty.io/guides/cdi-intro.html[Injecting Dependencies into Microservices guide]). -As a result, if you are migrating from using the `cloudant-1.0` feature to the new Cloudant SDK for Java, you should remove the `cloudant-1.0` feature from your server.xml file. This not only streamlines your configuration but also aligns with the new simplified approach of using the Cloudant CDI producer for integration. +As a result, if you are migrating from using the `cloudant-1.0` feature to the new Cloudant SDK for Java, you should remove the `cloudant-1.0` feature from your server.xml file. This not only streamlines your configuration but also aligns with the new simplified approach of using the Cloudant CDI producer for integration. == Bundle the Cloudant SDK for Java Prior to implementing the functionality, make sure to configure the essential dependencies in your application. Depending on your build tool (Maven or Gradle), you can include the necessary Cloudant SDK libraries by adding the corresponding dependency. -* For Maven: Add the below dependency in pom.xml file: +* For Maven: Add the following dependency in pom.xml file: [source, xml] ---- @@ -34,7 +34,7 @@ Prior to implementing the functionality, make sure to configure the essential de com.ibm.cloud cloudant x.x.x - + ---- Be sure to check for the latest version on link:https://central.sonatype.com/?smo=true[Maven Central]. @@ -64,7 +64,7 @@ If you have multiple applications accessing Cloudant, instead of bundling the Cl ---- == A CDI producer for Cloudant -With a CDI producer, you can easily provide a Cloudant client to your application and inject the client into various parts of the application in a type-safe and flexible way, with the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to simplify the configuration of the Cloudant driver. +With a CDI producer, you can easily provide a Cloudant client to your application and inject the client into various parts of the application in a type-safe and flexible way, with the benefits of dependency injection (such as lifecycle management and configuration). Also, we are using link:{url-prefix}/guides/microprofile-config-intro.html[MicroProfile Config] to simplify the configuration of the Cloudant driver. This example demonstrates how to create a CDI producer to inject a Cloudant client: [source, java] ---- @@ -111,7 +111,7 @@ One advantage of using a CDI producer is that you can tailor it to your needs. F 1.0.95 ---- -You must also enable the Password Utilities feature in your `server.xml`file: +You must also enable the Password Utilities feature in your `server.xml` file: [source, xml] ---- passwordUtilities-1.0 @@ -127,7 +127,7 @@ cloudant.dbname=testdb Instead of using `BasicAuthentication` with a username and password, Cloudant also supports `IAM (Identity and Access Management)` authentication, which allows users and applications to authenticate using secure API keys or IAM tokens. By using IAM authentication, we can avoid the risks associated with managing passwords and ensure that only authorized entities can interact with Cloudant databases. Follow the https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant[IBM Cloudant Setup] guide to create a Cloudant instance and API key for accessing it. -The code snippet below shows how can we create a CDI Producer to inject a Cloudant client with IAM authentication. +The following code snippet shows how can we create a CDI Producer to inject a Cloudant client with IAM authentication. [source, java] ---- @ApplicationScoped @@ -156,7 +156,7 @@ public class CloudantProducer { ---- == Injecting the Cloudant client -Here is an example of using the CDI producer to inject a Cloudant client in a JAX-RS application. +Here is an example of using the CDI producer to inject a Cloudant client in a JAX-RS application. [source, java] ---- @Inject @@ -170,7 +170,7 @@ String dbname; @Path("/add") @Consumes(MediaType.APPLICATION_JSON) public void add(CrewMember crewMember) { - Document newCrewMember = new Document(); + Document newCrewMember = new Document(); newCrewMember.put("Name",crewMember.getName()); newCrewMember.put("Rank",crewMember.getRank()); newCrewMember.put("CrewID",crewMember.getCrewID()); @@ -187,7 +187,7 @@ public void add(CrewMember crewMember) { } ---- -In the above code: +In this example: * `@Inject` is used to inject the Cloudant client instance provided by the CDI producer. * The `PostDocumentOptions` is a builder class that allows you to specify various options when posting the document. You must provide the db name and the document content. @@ -213,10 +213,10 @@ public String remove(@PathParam("id") String id) { DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder() .db(dbname) - .docId(id) + .docId(id) .rev(document.getRev()) .build(); - + DocumentResult deleteDocumentResponse = client .deleteDocument(deleteDocumentOptions) .execute() @@ -228,11 +228,6 @@ In the previous example: * The `GetDocumentOptions` class is used to configure parameters for retrieving a document from a Cloudant database. It allows you to specify the database name, document ID, and optional parameters like the document revision, whether to include attachments, conflicts, or deleted information in the response. The class uses the builder pattern to set these options before making the request to Cloudant. * The `DeleteDocumentOptions` class is used to configure parameters for deleting a document from a Cloudant database. It allows you to specify the database name, the document ID, and the revision (`_rev`) of the document to ensure that the correct version is deleted and prevent race conditions. This class uses the builder pattern to set options before sending the delete request to Cloudant. -== No need for a Cloudant feature -Previously, integrating Cloudant into your application required enabling the `cloudant-1.0` feature in the `server.xml` file. The CDI producer now automates the integration process, allowing you to inject Cloudant resources directly into your application without needing to explicitly enable the `cloudant-1.0` feature. What makes this approach even more flexible is that, even if the Cloudant SDK for Java’s API undergoes changes in the future, you won’t need to make extensive updates to your application code, meaning that simple updates to the producer itself will be sufficient to ensure continued compatibility with newer versions of the SDK. - -As a result, when migrating to the new Cloudant SDK for Java, it is recommended that you remove the `cloudant-1.0` feature from your server.xml file. This not only streamlines your configuration but also aligns with the new, simplified approach of using the Cloudant CDI producer for integration. - == Summary In this blog post, we explored how easy it is to create a CDI producer for Cloudant, and configure it with MicroProfile Config to help integrate Cloudant into an Open Liberty-based Java application, leveraging the power of Cloudant's NoSQL database with the flexibility of Open Liberty’s lightweight, cloud-native architecture. The full sample is available link:https://github.com/OpenLiberty/sample-cloudant[on GitHub].