Sample Application
The following code shows an example of how to use this REST API in a Java application. Examples for individual REST requests use snippets from this example.
There is a dependency on Jersey core classes, which looks like this in a Maven pom.xml file:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
This code example gets the value of the CACHING_INTERNAL_CACHE_URL environment variable and passes it to the doTest() method, which sends requests to a cache. Note that the REST port is 8080.
Note that this application is only suitable for illustrating communication with the REST API. It sends its output to stdout, which is only available in the Oracle Application Container Cloud application logs.
Also note that there is no explicit command to create a cache. If a REST operation references the name of a cache that doesn’t exist, the cache is created automatically.
package com.oracle.cache.testing;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;
/**
* Simple CCS REST Client Application
*/
public class CCSRestClient {
// Environment variable that holds CCS service name
public static final String CCS_ENV_NAME = "CACHING_INTERNAL_CACHE_URL";
// REST port is 8080
public static final String REST_PORT = "8080";
// Cache name to use for the test
public static final String CACHE_NAME = "testcache";
/**
* doTest
* Run the actual test -- performs key/value put, get, and delete.
* @param ccsHost Hostname for the service
*/
public void doTest(String ccsHost)
{
// Test key and value -- hardwired as this is just an example
String testKey = "TheKey";
String testValue = "TheAssociatedValue";
// Construct the full URL to service and build a WebTarget
String fullURL = "http://" + ccsHost + ":" + REST_PORT + "/ccs";
WebTarget target = ClientBuilder.newClient().target(fullURL);
// Put the key/value
Response putResponse = target
.path(CACHE_NAME + "/" + testKey)
.request(MediaType.APPLICATION_OCTET_STREAM)
.put(Entity.entity(testValue, MediaType.TEXT_PLAIN));
// Output the response status -- should be 204 (success with no returned content)
System.out.println("Put response status: " + putResponse.getStatus());
// Get the value & display it for comparison
Response getResponse = target
.path(CACHE_NAME + "/" + testKey)
.request(MediaType.APPLICATION_OCTET_STREAM)
.get();
// Output the response -- status should be 200
System.out.println("Get response status: " + getResponse.getStatus());
System.out.println("Get response entity: " + getResponse.readEntity(String.class));
// Fetch the metrics
Response getMetricsResponse = target
.path(CACHE_NAME)
.request()
.get();
// Output the response -- status should be 200
System.out.println("Get metrics response status: " + getMetricsResponse.getStatus());
System.out.println("Get metrics response entity: " + getMetricsResponse.readEntity(String.class));
// Delete the key
Response deleteResponse = target
.path(CACHE_NAME + "/" + testKey)
.request()
.delete();
// Output the response -- status should be 204 (success with no returned content)
System.out.println("Delete response status: " + deleteResponse.getStatus());
// Issue a "putIfAbsent" via POST
Response postResponse = target
.path(CACHE_NAME + "/" + testKey)
.request(MediaType.APPLICATION_OCTET_STREAM)
.header("X-Method", "putIfAbsent")
.post(Entity.entity(testValue, MediaType.APPLICATION_OCTET_STREAM));
// Output the response status -- should be 204 (success with no returned content)
System.out.println("PutIfAbsent response: " + postResponse.getStatus());
// Issue a clear cache
Response deleteCacheResponse = target
.path(CACHE_NAME)
.request()
.delete();
// Output the response -- should be 204 (success with no returned content)
System.out.println("Delete cache response status: " + deleteCacheResponse.getStatus());
}
/**
* main
* Instantiates the app class and runs the test.
* @param args
*/
public static void main(String[] args)
{
String ccsHost;
CCSRestClient ccsClient = new CCSRestClient();
// Fetch the cache service name from the environment, that's the ccsHost
Map<String, String> env = System.getenv();
ccsHost = env.get(CCS_ENV_NAME);
// Execute the test
ccsClient.doTest(ccsHost);
System.exit(0);
}
}