Cortex Proxy Servlet
Cortex Proxy Servlet
The Cortex proxy servlet allows clients applications to access Cortex directly, without having to use the AEM Commerce API. The Cortex proxy servlet returns responses in JSON objects, which the client application will have to parse.
The proxy servlet is useful for web browsers to retrieve and present data directly from Cortex using a client-side scripting language like Ajax.
Limitations
- The Cortex proxy servlet only supports GET, PUT, POST, and DELETE requests. 405 Method Not Allowed returns for any other operation.
- Attaching traits to Cortex requests is not supported.
Configuration
Enable the Cortex proxy servlet from the OSGi console, using the Cortex proxy servlet configuration. For more information, see Runtime Configurations.
How Cortex Proxy Servlet Works
GET http://localhost:4502/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/carts/geometrixx/default
When the proxy receives the request, the URL portion after .proxy (e.g. /carts/geometrixx/default) is used to construct the Cortex request. The proxy replicates the request’s query string parameters and headers on the outbound Cortex request.
Responses returned from the proxy will contain links to Cortex via the proxy. If Cortex returns 307 Temporary Redirect, the response’s location header identifies where the client application can find the resource. Clients configured to follow redirects can automatically follow the location header to the resource.
Implementing the Proxy Servlet
GET http://localhost:4502/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/carts/geometrixx/default
Example: JCR content node at /content/geometrixx-outdoors/en_US/cortex
.content.xml
<?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:mix="http://www.jcp.org/jcr/mix/1.0" jcr:primaryType="cq:Page"> <jcr:content jcr:primaryType="nt:unstructured" jcr:title="Cortex Proxy" hideInNav="true" sling:resourceType="elasticpath/components/cortex"> </jcr:content> </jcr:root>
Example: Request/Response
Request
GET /content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge= HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Host: localhost:4502 User-Agent: HTTPie/0.9.2
Response
HTTP/1.1 200 OK Cache-Control: no-cache, no-transform Content-Length: 2007 Content-Type: application/json Date: Fri, 29 May 2015 21:45:55 GMT Server: Apache-Coyote/1.1 X-Content-Type-Options: nosniff X-Powered-By: Jetty(9.2.9.v20150224) { "links": [ { "href": "http://localhost:4502/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=/lineitems", "rel": "lineitems", "rev": "cart", "type": "elasticpath.collections.links", "uri": "/commerce-legacy/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=/lineitems" }, { "href": "http://localhost:4502/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/discounts/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=", "rel": "discount", "type": "elasticpath.discounts.discount", "uri": "/commerce-legacy/discounts/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=" }, ... { "href": "http://localhost:4502/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/totals/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=", "rel": "total", "rev": "cart", "type": "elasticpath.totals.total", "uri": "/commerce-legacy/totals/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=" } ], "self": { "href": "http://localhost:4502/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=", "type": "elasticpath.carts.cart", "uri": "/commerce-legacy/carts/geometrixx/gi3gezlcg4ztsljzg5qtkljumi3dcljzmizteljxmu3ggzrugaztszrtge=" }, "total-quantity": 0 }
Example: Javascript client (using jQuery)
The following example shows how the Cortex proxy servlet could be used from a Javascript client.
var cortexCart = cortexCart || {} cortexCart.fetchCart = function() { $.ajax( { type:'GET', url:'/content/geometrixx-outdoors/en_US/cortex/jcr:content.proxy/carts/geometrixx/default?zoom=lineitems:element,lineitems:element:item:price,lineitems:element:item:code,lineitems:element:total,total,appliedpromotions:element,lineitems:element:appliedpromotions.element,discount', complete:function(result) { var obj = parseResponse(result.responseText); console.log(obj) } }); function parseResponse(response) { var json = JSON.parse(response); return json._lineitems; } }