Tuesday, 21 April 2015

Invoking REST Services (GET Method ) from Store JSP

WCS provides various resource handlers like CartHandler, OrderHandler, PersonHandler, PromotionHandler etc., Let us explore how to invoke GET request on these resources from one of the Store JSP code.
For illustration purpose let us assume that you want to invoke GET request on CartHandler resource to retrieve shopping cart details. CartHandler resource class is annotated with store/{storeId}/cart and getCart method is annotated with @self as shown in below code snippet.

Cart Handler



Now to invoke this REST Service from a REST Client (HTTP Requester for FF or PostMan for Chrome), you will use the below URL:

URL:
http://localhost/wcs/resources/store/10001/cart/@self?pageNumber=1&pageSize=20
HTTP Method –> GET
JSON Body -> NULL

Note:
wcs/resources is the context path for Rest.war. All REST Handler classes are declared in Rest.war/web-inf/config/providers.properties. web.xml points to this providers.properties file as shown below:

<servlet>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <servlet-class>

       org.apache.wink.server.internal.servlet.RestServlet
  </servlet-class>
  <init-param>
     <param-name>propertiesLocation</param-name>
     <param-value>/WEB-INF/config/rest-config.properties</param-value>
  </init-param>
  <init-param>
     <param-name>applicationConfigLocation</param-name>
     <param-value>

       /WEB-INF/config/providers-ext.properties;
       /WEB-INF/config/providers.properties;/WEB-INF/config/resources-ext.properties;
       /WEB-INF/config/resources.properties
     </param-value>
  </init-param>
  <init-param>
</servlet>



If you want to make use of this GET Cart Service in ShoppingCart.jsp to display the cart details, you have two options:
  1. On page load, make an Ajax call to this REST URL and build the DOM using the returned JSON object and javascript functions.
  2. Use java scriptlets on JSP code with java.net.URL object to open connection to the REST URL and retrieve the response. Then use JSTL tag library to loop through the response and build the DOM.

But if you go with second option, you have to handle exception scenario, connection timeOut issues, server hostName and portNumber configuration, caching etc.,
Luckily WCS framework helps to overcome these issues easily and provides a simpler way of invoking GET REST Services, through wcf:rest JSTL Tag.
The tag handler class RestTag encapsulates all the complexities involved in invoking the REST Service and allows UI developers to concentrate on presentation layer.

Here is the sample code to invoke the GET Service using wcf:rest tag

<wcf:rest var="myOrders" url="store/{storeId}/cart/@self">
      <wcf:var name="storeId" value = “10001" encode="true"/>
      <wcf:param name="pageSize" value="20"/>
      <wcf:param name="pageNumber" value="1"/>
</wcf:rest>
 
REST tag builds the final URL by:
  1. Converting relative URL to full URL. It looks at REST_CONFIG variable set in EnvironmentSetup.jspf file as request attribute to identify the port number, server host name etc.,
  2. Replaces variables in the URL with value passed with wcf:var tag. ( {storeId} will be replaced with 10001 ) in above example.
  3. Any value set with wcf:param tag will be passed as query parameters.
So final URL will be:
http://localhost/wcs/resources/store/10001/cart/@self?pageSize=20&pageNumber=1

So by using wcf:rest tag, you can easily invoke any GET Rest request from your JSP code.

Few other benefits of REST tag are:
  1. REST Tag also takes care of passing headers (WCToken, WCTrustedToken, Cookies etc) as part of the request.
  2. REST Tag implements caching at request level. So when GET request to same resource is invoked second time within the same request, the response comes from cache.
  3.  REST Tag takes care of Local binding / Remote binding setting done in component configuration files. So if binding is Local (Stores.war runs within WC.ear), request dispatcher is used to invoke the REST call instead of opening URL connection.

No comments:

Post a Comment