REST WebServices in ATG Using Jersey

REST Services in ATG Using Jersey

This blog describes how ATG components can be exposed as RESTful web services using a JAX-RS implementation like Jersey. It is intended to give a step-by-step walkthrough on setting up Rest services in ATG application.

 Limitations of ATG restfull webservices. Some of them are:

1. Fixed URI pattern – ATG REST services slot all services into Repository and bean category and contain the component path details. This limits the flexibility to design meaningful URIs.
3. All parameters passed to ATG REST service components are passed as query params , URL params or path params.

For example, we are going to use Jersey which is a JAX-RS implementation.

Creating an ATG module with REST 

Create a new ATG module using the ATG plugin:

1. Include the required jars for Jersey in the build path of the project as well as inside the WEB-INF\lib folder of JerseyWar.war
Note: It is necessary to include the jars in the WEB-INF\lib folder because Jersey expects these classes to be present in the lib folder of the war.
Following are the required jars:
asm-3.1.jar
jersey-core-1.11.jar
jersey-server-1.11.jar
jersey-servlet-1.11.jar
jsr311-api-1.1.1.jar
The above jars can be downloaded from: http://jersey.java.net/ 

2. Create a simple ATG component as a client for testing:

a. TestRestComp.java
package com.test;

public class TestRestComp {
private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}


b. TestRestComp.properties
# /com/test/TestRestComp
#TUE Nov 19 09:50:46 
$class=com.test.TestRestComp
$scope=request
value="This is a test value"

Here,component scope as request. The session and global scope components can also be accessed in same way as described below.
3. Create a resource class for REST
We need to create a Jersey Resource class which will define our REST service.

package rest.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.ServletUtil;

import com.test.TestRestComp;

@Path("/test")
public class TestResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getTestComponentValue(){
DynamoHttpServletRequest request = ServletUtil.getCurrentRequest();
TestRestComp testComponent = (TestRestComp) request.resolveName("/com/test/TestRestComp");
return testComponent.getValue();
}
}
In the above getTestComponentValue() method we are resolving our test component /com/test/TestRestComp and then returning the value of the property “value” which we will be getting as “This is a test value” as set in the properties file.
4. Changes to be made in the web.xml of JerseyWar web application
a. Add a filter-mapping so that all the REST requests will go through the ATG servlet pipeline. This is required so that the request and response get wrapped by dynamo context.
b. Define the REST servlet
i. Specify the servlet name as Jersey REST Servlet
ii. Servlet class as com.sun.jersey.spi.container.servlet.ServletContainer
iii. Specify the <init-param> to tell the Jersey REST Servlet in which package all the resource classes are located.
c. Define a <servlet-mapping> to direct all request with the url pattern /rest/* to our Jersey REST Servlet. 

<!-- Configuration For Jersey -->
<!-- So that all the requests go through the Servlet Pipeline -->
<filter-mapping>
<filter-name>PageFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<!-- Jersey servlet configuration -->
<servlet>
<servlet-name>Jersey REST Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.service</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

5. Compile the classes. Move the resource classes from the atglib folder of the .ear to the WEB-INF/classes folder or package the resources in a jar and put them in the WEB-INF/lib folder. 
NOTE: This step is important because the Jersey REST Servlet will only search for the resources in the WEB-INF folder in the packages specified.

6. Assemble the ATG project and deploy the ear on an application server like Weblogic Application server.

7. Start the server

8. We can test our REST service using a web browser as follows:

To test our service we will be using the path http://localhost:7001/Jersey/rest/test
http://localhost:7001 - Address of the weblogic server
/Jersey – Context root of our ATG module
/rest – URL pattern for our rest service specified in the web.xml
/test – Path for our resource specified as @Path annotation in the resource class TestResource.java



You can also design services that accept parameters in line with the URI, query params or POST parameters. The output can be produced in XMl or JSON or any desired format. Please refer to the Jersey tutorial for more details. 


Comments

Popular posts from this blog

how to generate classes from swagger

How to create new user/account in BCC