Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

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

The Cortex proxy servlet is implemented as a SlingServlet, which is bound to the elasticpath/components/cortex Sling resource type. Client applications send requests to the Cortex proxy servlet URL specifying the store scope. For example:
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

To implement the proxy servlet, create a JCR node within the store's content tree. The proxy servlet will resolve the base store by walking up the content tree until a node with a cq:cortexBaseStore property is located. Example request:
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; 
    }
  }