Learning to use Cortex Java SDK
Learning to use Cortex Java SDK
Cortex Java SDK can consume Cortex resources in client applications that are built with Java and OSGi.
Setup
Cortex Java SDK is available from the Elastic Path Public Maven Repository.
<dependency> <groupId>com.elasticpath.rest</groupId> <artifactId>cortex-jaxrs-client</artifactId> <version>${java.cortex.jaxrs.client.version}</version> </dependency>
Usage
Cortex Java SDK is based on the JAX-RS Java API and includes a set of extensions to facilitate development with Cortex. The SDK provides both a straight JAX-RS Client service as well as a user-scoped CortexClient that scopes Cortex calls to a specific user.
JAX-RS Client
The JAX-RS Client can call any REST service, not just Cortex. The Client is injected via an OSGi SCR annotation:
@Reference javax.ws.rs.client.Client jaxrsClient;
JAX-RS Client Configuration
The client instance can be configured by editing the settings in the OSGi admin console. Additional configuration is achieved by adding and removing ClientConfigurator services to the list.
Name | Details |
---|---|
apache-connector | Configures the JAX-RS client to use the Apache HttpClient Connector. If this configuration is removed, JAX-RS will fall back to using Java's default HttpURLConnection. |
oauth2 | Adds a JAX-RS filter to set an OAuth2 Authorization access token header to requests. The token to use is managed by the CortexClient instance described below. |
jackson | Supplies a Jackson ObjectMapper for JAX-RS to use for JSON marshalling. |
json-unmarshaller | Instructs JAX-RS to utilize Elastic Path's JSON Unmarshaller to map JSON data into data objects. |
CortexClient
A CortexClient instance provides a user-scoped wrapper around the JAX-RS client and facilitates the use of the JSON Unmarshaller types. One will usually use the CortexClient as almost all Cortex operations require the user's access token. The Cortex Client uses the configured JAX-RS Client to make REST calls to Cortex.
Using Cortex Client
@Reference com.elasticpath.rest.client.CortexClientFactory clientFactory; ... com.elasticpath.rest.client.CortexClient client = clientFactory.create(accesToken, scope);
Sending Requests
Once a Client is obtained, it can be used to fetch a view:
CartView cart = client.get(CartView.class);
CartView is a custom class with @JsonPath and @JsonProperty annotations, which are described later in this document.
Constructing URIs
Cortex Java SDK provides four class level annotations to construct Cortex resource URLs:
- @Zoom: Adds a Zoom query parameter to the request URL.
- @Path: Used together with @Zoom, @Path specifies the relation/link to follow from the Zoom request.
- @FollowLocation: Adds a FollowLocation query parameter to the request URL.
Example: Zoom and FollowLocation
A class that models the expected response should be annotated as follows:
@Zoom(@Path("order")) @FollowLocation public class Cart {}
baseUrl = "http://localhost:9080/cortex/" path = "carts/geometrixx/default" result = cortexUrlFactory.addQueryParametersToResourceUrl(baseUrl, path, Cart.class) //result = "http://localhost:9080/cortex/carts/geometrixx/default?zoom=order&followLocation=true"
Example: Without Query Parameters
baseUrl = "http://localhost:9080/cortex/" path = "carts/geometrixx/default" result = cortexUrlFactory.createResourceUrl(baseUrl, path) //result = "http://localhost:9080/cortex/carts/geometrixx/default"
Example: With Entry Point Resource
A class that models the expected response should be annotated as follows:
@EntryPointUri(“carts/{scope}/default”) @Zoom(@Path("lineitems")) @FollowLocation public class Carts {}
baseUrl = “http://localhost:9080/cortex/” store = “geometrixx” result = cortexUrlFactory.createResourceUrlFromAnnotations(baseUrl, store, Carts.class) //result = "http://localhost:9080/cortex/carts/geometrixx/default?zoom=lineitems&followLocation=true"
Unmarshalling Cortex Responses
Cortex Java SDK unmarshalling is provided by the Elastic Path JSON Unmarshaller open source project. This section shows some simple examples of how to use the JSON Unmarshaller. For more complex unmarhalling examples, see the JSON Unmarshaller project page.
Cortex Java SDK provides two annotations to extract data from a Cortex response:
- @JsonProperty: Extracts a JSON property from a Cortex response.
-
@JsonPath: Uses JSONPath to extract either a single or multiple JSON properties from a Cortex response. This annotation is useful to extract nested JSON properties.Tip: JSONPath Expression Tester
Cortex Studio comes bundled with a JSONPath tester for creating and testing JSONPath expressions.
{ "self": { "type": "elasticpath.carts.cart", "uri": "/commerce-legacy/carts/mobee/gwu=?zoom=lineitems:element", "href": "http://api.demo.elasticpath.com/cortex/carts/mobee/gwu=?zoom=lineitems:element", "max-age": 0 }, "total-quantity": 1, "_lineitems": [ { "_element": [ { "self": { "type": "elasticpath.carts.line-item", "uri": "/commerce-legacy/carts/mobee/gwu=/lineitems/gq4=", "href": "http://api.demo.elasticpath.com/cortex/carts/mobee/gwu=/lineitems/gq4=", "max-age": 0 }, "links": [ { "rel": "list", "type": "elasticpath.collections.links", "uri": "/commerce-legacy/carts/mobee/gwu=/lineitems", "href": "http://api.demo.elasticpath.com/cortex/carts/mobee/gwu=/lineitems" } ], "quantity": 1 } ] } ], "links": [ { "rel": "lineitems", "rev": "cart", "type": "elasticpath.collections.links", "uri": "/commerce-legacy/carts/mobee/gwu=/lineitems", "href": "http://api.demo.elasticpath.com/cortex/carts/mobee/gwu=/lineitems" }, { "rel": "order", "rev": "cart", "type": "elasticpath.orders.order", "uri": "/commerce-legacy/orders/mobee/hbr=", "href": "http://api.demo.elasticpath.com/cortex/orders/mobee/hbr=" }, { "rel": "total", "rev": "cart", "type": "elasticpath.totals.total", "uri": "/commerce-legacy/totals/carts/mobee/gwu=", "href": "http://api.demo.elasticpath.com/cortex/totals/carts/mobee/gwu=" } ] }
Example: Extract JSON Property using @JsonProperty
public class Cart {) @JsonProperty("total-quantity") private String totalQuantity; }
Example: Extract Nested JSON Properties using @JsonPath
public class Cart { //Non-nested JSON property @JsonPath("$.total-quantity") private int totalQuantity; //Nested Property @JsonPath("$._lineitems[0]._element[0].quantity") private int quantity; //Nested Array @JsonPath("$._lineitems[0]._element") private List<LineItem> lineItems; }