java search
Loading
Help improve Java Search

Tuesday, July 03, 2012

Integrate Jersey and Spring

This post describes a way to integrate spring with Jersey resource classes. It expands on the earlier sample Restful Web Services With Jersey API. The code here has been implemented on following configuration
  • Tomcat 7
  • Java 7
  • Spring 3.1.1
  • Jersey 1.12
To show the use of Spring, I added MyService class which will be injected into RestWS class. For this example, we start out with the sample code in Restful Web Services With Jersey API and make the following changes...
  1. Web.xml: In the Web Deployment Descriptor, the Jersey Servlet has to be modified to use com.sun.jersey.spi.spring.container.servlet.SpringServlet instead of com.sun.jersey.spi.container.servlet.ServletContainer
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>JerseyRest</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>Jersey Web Application</servlet-name>
            <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
            <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>com.blogspot.aoj.restws</param-value>
            </init-param>
    
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Jersey Web Application</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
    </web-app>
  2. RestWS: There is not much change in the RestWS.java file, other than adding the field MyService which will be injected by spring. The following sample code shows @Autowire, which works with @Component annotation for spring to inject dependencies. However, you can implement the same example without Autowiring by simply creating a bean in spring context file.
    /**
     * @author Abhi Vuyyuru
     */
    
    package com.blogspot.aoj.restws;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.blogspot.aoj.service.MyService;
    
    @Path("/restws")
    @Component
    public class RestWS {
        
        @Autowired
        private MyService myService;
    
        @GET
        @Path("concat/i/{i}/j/{j}")
        @Produces(MediaType.TEXT_HTML)
        public String concat(@PathParam("i") String i, @PathParam("j") String j) {
            return myService.concat(i, j);
        }
    
        public MyService getMyService() {
            return myService;
        }
    
        public void setMyService(MyService myService) {
            this.myService = myService;
        }
    
    }
  3. /WEB-INF/applicationContext.xml: The following code shows code for Autowire and otherwise. Simply comment the unused version
    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">         <bean id="myService" class="com.blogspot.aoj.service.MyService" />     <bean id="restWS" class="com.blogspot.aoj.restws.RestWS">         <property name="myService" ref="myService"></property>     </bean>     <!-- <context:component-scan base-package="com.blogspot.aoj.restws" /> --> </beans>
  4. Client will be the same as used in the previous code sample Restful Web Services With Jersey API
  5. The Jersey Spring Servlet is part of the jersey-spring.jar file which can be downloaded from the Jersey download site
  6. Finally, the following is the ant build file that I used.
    <project name="jerseyRest" default="compile" basedir=".">
        <property file="build.properties" />
        <property file="${user.home}/build.properties" />
        <property name="app.name" value="jerseyRest" />
        <property name="app.path" value="/${app.name}" />
        <property name="app.version" value="0.1-dev" />
        <property name="build.home" value="${basedir}/build" />
        <property name="catalina.home" value="c:/tomcat7" />
        <property name="dist.home" value="${basedir}/dist" />
        <property name="docs.home" value="${basedir}/docs" />
        <property name="manager.url" value="http://localhost:8080/manager/text" />
        <property name="manager.username" value="tomcat" />
        <property name="manager.password" value="tomcat" />
        <property name="src.home" value="${basedir}/src" />
        <property name="web.home" value="${basedir}/WebContent" />
        <path id="compile.classpath">
            <fileset dir="${catalina.home}/bin">
                <include name="*.jar" />
            </fileset>
            <pathelement location="${catalina.home}/lib" />
            <fileset dir="${catalina.home}/lib">
                <include name="*.jar" />
            </fileset>
            <fileset dir="${web.home}/WEB-INF/lib">
                <include name="*.jar" />
            </fileset>
    
        </path>
        <taskdef resource="org/apache/catalina/ant/catalina.tasks" classpathref="compile.classpath" />
        <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="compile.classpath" />
        <taskdef name="list" classname="org.apache.catalina.ant.ListTask" classpathref="compile.classpath" />
        <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="compile.classpath" />
        <taskdef name="findleaks" classname="org.apache.catalina.ant.FindLeaksTask" classpathref="compile.classpath" />
        <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="compile.classpath" />
        <taskdef name="start" classname="org.apache.catalina.ant.StartTask" classpathref="compile.classpath" />
        <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" classpathref="compile.classpath" />
        <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="compile.classpath" />
    
        <property name="compile.debug" value="true" />
        <property name="compile.deprecation" value="false" />
        <property name="compile.optimize" value="true" />
    
    
        <target name="all" depends="clean,compile" description="Clean build and dist directories, then compile" />
    
        <target name="clean" description="Delete old build and dist directories">
            <delete dir="${build.home}" />
            <delete dir="${dist.home}" />
        </target>
    
    
        <target name="compile" depends="prepare" description="Compile Java sources">
            <mkdir dir="${build.home}/WEB-INF/classes" />
            <javac srcdir="${src.home}" destdir="${build.home}/WEB-INF/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}">
                <classpath refid="compile.classpath" />
            </javac>
            <copy todir="${build.home}/WEB-INF/classes">
                <fileset dir="${src.home}" excludes="**/*.java" />
            </copy>
        </target>
    
        <target name="dist" depends="compile,javadoc" description="Create binary distribution">
            <mkdir dir="${dist.home}/docs" />
            <copy todir="${dist.home}/docs">
                <fileset dir="${docs.home}" />
            </copy>
            <jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${build.home}" />
        </target>
    
    
        <target name="install" depends="compile" description="Install application to servlet container">
            <deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" localWar="file://${build.home}" />
        </target>
    
    
        <target name="javadoc" depends="compile" description="Create Javadoc API documentation">
            <mkdir dir="${dist.home}/docs/api" />
            <javadoc sourcepath="${src.home}" destdir="${dist.home}/docs/api" packagenames="*">
                <classpath refid="compile.classpath" />
            </javadoc>
        </target>
    
    
    
        <target name="list" description="List installed applications on servlet container">
            <list url="${manager.url}" username="${manager.username}" password="${manager.password}" />
        </target>
    
    
        <target name="prepare">
            <mkdir dir="${build.home}" />
            <mkdir dir="${build.home}/WEB-INF" />
            <mkdir dir="${build.home}/WEB-INF/classes" />
            <copy todir="${build.home}">
                <fileset dir="${web.home}" />
            </copy>
            <mkdir dir="${build.home}/WEB-INF/lib" />
        </target>
    
        <target name="reload" depends="compile" description="Reload application on servlet container">
            <reload url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" />
        </target>
        <target name="remove" description="Remove application on servlet container">
            <undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" />
        </target>
    </project>

Monday, March 28, 2011

Resful Web Services With Jersey API

This post describes how to use Jersey API to create RESTful Web Services. To keep it simple, I'm not going describe how REST works or should be implemented. Rather, it is simple example on how to use the API to implement some of the more common features of a REST service. The post describes how to
  • Create a RESTful Web Service using Jersey
  • Create GET, POST, PUT and DELETE handlers in the service
  • Create a Jersey Client to invoke the various methods on the Service
The example here has been tested on Tomcat7, with Java SE 6. The following steps explain how to use the API
More ...............
  1. Download Jersey from http://jersey.java.net/
  2. Create a Dynamic Web Project(JerseyRest) in Eclipse
  3. Add the Jersey Jar files to the classpath. The following files were used:http://www.blogger.com/img/blank.gif
    Server:
    • asm-3.1.jar
    • jersey-core-1.4.jar
    • jersey-server-1.4.jar
    Client:
    • jersey-client-1.4.jar
    • jersey-core-1.4.jar
  4. Add the Jersey Servlet to Web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>JerseyRest</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
    <servlet-name>Jersey Web Application</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.blogspot.aoj.restws</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
    </web-app>
    Note:
    • The Jersey Servlet maps to all requests under /rest
    • The package where your service class resides is provided in the package-name init param.
    Create the Service Class
    /**
    * @author Abhi Vuyyuru
    */

    package com.blogspot.aoj.restws;

    import javax.ws.rs.Consumes;
    import javax.ws.rs.DELETE;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("/restws")
    public class RestWS {
    @GET
    @Produces("text/plain")
    @Path("hello")
    public String sayHello() {
    return "Hello World";
    }

    @GET
    @Path("concat/i/{i}/j/{j}")
    @Produces("text/plain")
    public String concat(@PathParam("i") String i, @PathParam("j") String j) {
    System.out.println("GET Input : " + i + j);
    return (i + j);
    }

    @POST
    @Path("posts")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String consumeReq(@FormParam("i") String i, @FormParam("j") String j) {
    System.out.println("POST Input : " + i + j);
    return i + j;
    }

    @PUT
    @Path("puts")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void putReq(@FormParam("i") String i, @FormParam("j") String j) {
    System.out.println("PUT Input : " + i + j);
    return;
    }

    @DELETE
    @Path("dels/i/{i}/j/{j}")
    public void deleteReq(@PathParam("i") String i, @PathParam("j") String j) {
    System.out.println("DELETE Input : " + i + j);
    return;
    }

    }
  5. Create the Client
    /**
    * @author Abhi Vuyyuru
    */
    package com.blogspot.aoj.clients;

    import java.io.IOException;

    import javax.ws.rs.core.MultivaluedMap;

    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.WebResource;
    import com.sun.jersey.core.util.MultivaluedMapImpl;

    public class RestWSClient {

    public static void main(String[] args) throws IOException {
    Client client = Client.create();
    WebResource wsClient = client.resource("http://localhost:8080/JerseyRest/restws/");

    String helloResp = wsClient.path("hello").get(String.class);
    System.out.println(helloResp);

    MultivaluedMap queryParams = new MultivaluedMapImpl();
    queryParams.add("i", "val1");
    queryParams.add("j", "val2");
    String postResp = wsClient.path("posts").post(String.class, queryParams);
    System.out.println(postResp);

    String getResp = wsClient.path("concat").path("i/" + "val1").path("j/" + "val2").get(String.class);
    System.out.println(getResp);

    wsClient.path("puts").put(queryParams);

    wsClient.path("dels").path("i/" + "val1").path("j/" + "val2").delete();

    }
    }

Tuesday, August 31, 2010

File Upload with Servlet 3.0

This code works on Glassfish 3.
This is a simple example of how to upload files using JSP and Servlet 3.0 which is a part of Java EE 6. While in earlier versions of Servlets we had to use commons fileupload or other libraries, this feature has been integrated into the Servlet 3.0 specification. Here is the code for a simple servlet and a JSP file that makes a file upload request to the servlet.

Full post
  1. FileUploadServlet.java
    package com.blogspot.aoj.servlet3;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.MultipartConfig;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.Part;

    import org.apache.log4j.Logger;

    @WebServlet(urlPatterns = "/fileUpload")
    @MultipartConfig
    public class FileUploadServlet extends HttpServlet {
    private static Logger logger = Logger.getLogger(FileUploadServlet.class);

    public FileUploadServlet() {
    super();
    }

    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    for (Part part : request.getParts()) {
    logger.info(part.getName());
    InputStream is = request.getPart(part.getName()).getInputStream();
    int i = is.available();
    byte[] b = new byte[i];
    is.read(b);
    logger.info("Length : " + b.length);
    String fileName = getFileName(part);
    logger.info("File name : " + fileName);
    FileOutputStream os = new FileOutputStream("c:/temp/logs/" + fileName);
    os.write(b);
    is.close();
    }

    }

    private String getFileName(Part part) {
    String partHeader = part.getHeader("content-disposition");
    logger.info("Part Header = " + partHeader);
    for (String cd : part.getHeader("content-disposition").split(";")) {
    if (cd.trim().startsWith("filename")) {
    return cd.substring(cd.indexOf('=') + 1).trim()
    .replace("\"", "");
    }
    }
    return null;

    }

    protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    }

    }
    Note:
    • There is no need to use a deployment descriptor the @WebServlet annotation is enough, which uses the urlPatterns property to define servlet mappings
    • The @MultipartConfig is used to indicate that the servlet expects requests wof multipart/form-data MIME type which is required for file upload. Using @MultipartConfig allows you to use the request.getParts get the parts of the request
    • The getFileName method gets the file name from the content-disposition header.
  2. upload.jsp
    <html>
    <head>
    <title>File Upload with Servlet 3.0</title>
    </head>
    <body>
    <form action="fileUpload" enctype="multipart/form-data" method="post">
    <input type="file" name="uploadFile" /> <input type="submit" /></form>
    </body>
    </html>

Tuesday, July 27, 2010

Implementing REST WebService using JAX-WS

This example was implemented on Weblogic 10.3 server.
The following example demonstrates how to implement a simple REST WebService using JAX-WS. This example shows the following two ways to pass parameters to a REST Webservice as part of a HTTP GET Request
  • Query String
    http://localhost:7001/RestfulWS/RestfulWS?i=1&j=5
  • Path Info
    http://localhost:7001/RestfulWS/RestfulWS/i/1/j/5
In order to implement you have to
More
  1. Implement the Provider Interface
    @WebServiceProvider(targetNamespace = "http://java-x.blogspot.com/wsSamples/restfulWebService1", serviceName = "RestfulWS")
    @BindingType(value = HTTPBinding.HTTP_BINDING)
    public class RestfulWS implements Provider<Source> {
    The provider interface has only one method invoke(). Setting the BindingType annotation to HTTP_BINDING ensures that HTTP GET requests are sent to this service. The most common way to implementing Provider interface is using Provider which is used to pass XML data back to the client

  2. Implementing the invoke Method: The invoke method handles the all the logic for the Service. In the following snippet, you can see how to get the request type and the handling logic based on whether the request parameters are sent in a QueryString or as part of the request path.
       String requestMethod = (String) messageContext.get(MessageContext.HTTP_REQUEST_METHOD);
    String query = (String) messageContext.get(MessageContext.QUERY_STRING);
    String path = (String) messageContext.get(MessageContext.PATH_INFO);
  3. Implementing the process request: In this method, you can see that the request parameter string is parsed by using separators "&", "=" and "/". This enables handling of both QueryString ("&" and "=") and Path Info ("/") requests.
      StringTokenizer st = new StringTokenizer(queryString, "=&/");
  4. Implementing createSource Method: This method creates an XML string that will be sent as the response as a StreamSource.
    return new StreamSource(new ByteArrayInputStream(result.getBytes()))
  5. Create a web.xml descriptor: The descriptor file should contain a servlet-mapping for the servlet "[ServiceName]Servlethttp" to accept all requests to [ServiceName]/* to allow path info requests. Make sure that you do add the servlet declaration as this is done by JWSC ant task and may create a duplicate.
    <servlet-mapping>
    <servlet-name>RestfulWSServlethttp</servlet-name>
    <url-pattern>/RestfulWS/*</url-pattern>
    </servlet-mapping>

    The reason for creating a servlet-maping is because the JWSC ant task creates the servlet and a servlet mapping in the web.xml, however, the mapping generated by JWSC maps to [ServiceName]/ and not [ServiceName]/*, which means that path info requests will not be processed.
  6. Creating the Web Service using ANT: The following ant task creates the webservice as an exploded WAR.
    <target name="build.service" description="Target that builds the Web Service">
    <echo message="${java.class.path}" />
    <mkdir dir="${ear.dir}" />
    <jwsc srcdir="./src/com/blogspot/aoj/restws/" destdir="${ear.dir}" fork="true" keepGenerated="true" deprecation="${deprecation}" debug="${debug}" verbose="false">
    <jws file="${ws.file}.java" explode="true" type="JAXWS" >
    <descriptor file="./resources/web.xml" />
    </jws>
    </jwsc>
    </target>
    The task has to contain the location of web.xml file created earlier. This file will be modified by JWSC
The complete sources
  • RestfulWS.java:
    package com.blogspot.aoj.restws;

    /**
    * @author Abhi Vuyyuru
    */

    import java.io.ByteArrayInputStream;
    import java.util.StringTokenizer;

    import javax.annotation.Resource;
    import javax.xml.transform.Source;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.ws.BindingType;
    import javax.xml.ws.Provider;
    import javax.xml.ws.WebServiceContext;
    import javax.xml.ws.WebServiceProvider;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.http.HTTPBinding;

    @WebServiceProvider(targetNamespace = "http://java-x.blogspot.com/wsSamples/restfulWebService1", serviceName = "RestfulWS")
    @BindingType(value = HTTPBinding.HTTP_BINDING)
    public class RestfulWS implements Provider<Source> {
    public Source invoke(Source source) {
    try {
    MessageContext messageContext = wsContext.getMessageContext();
    String requestMethod = (String) messageContext.get(MessageContext.HTTP_REQUEST_METHOD);
    String query = (String) messageContext.get(MessageContext.QUERY_STRING);
    String path = (String) messageContext.get(MessageContext.PATH_INFO);

    if (requestMethod.equals("GET")) {
    if (query != null && query.contains("i=") && query.contains("j=")) {
    return processRequest(query);
    } else {
    return processRequest(path);
    }
    } else {
    throw new Exception("Unsupported operation");
    }

    } catch (Exception e) {
    e.printStackTrace();
    return createSource(1, e.getMessage());
    }
    }

    private Source processRequest(String queryString) {
    StringTokenizer st = new StringTokenizer(queryString, "=&/");
    st.nextToken();
    int i = Integer.parseInt(st.nextToken());
    st.nextToken();
    int j = Integer.parseInt(st.nextToken());

    return createSource(i + j, "Sum");
    }

    private Source createSource(int status, String errorMessage) {
    String result = null;
    result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><Response><StatusCode>" + status
    + "</StatusCode><StatusMessage>" + errorMessage + "</StatusMessage></Response>";
    return new StreamSource(new ByteArrayInputStream(result.getBytes()));
    }

    @Resource(type = Object.class)
    protected WebServiceContext wsContext;
    }
  • resources/web.xml

    <?xml version='1.0' encoding='UTF-8'?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5">
    <display-name>RestfulWSWebApp</display-name>
    <servlet-mapping>
    <servlet-name>RestfulWSServlethttp</servlet-name>
    <url-pattern>/RestfulWS/*</url-pattern>
    </servlet-mapping>
    </web-app>
  • build.xml
    <project name="wsSamples" default="all" basedir=".">
    <!-- set global properties for this build -->
    <property file="./build.properties" />
    <property name="ws.file" value="RestfulWS" />
    <property name="ear.dir" value="${build.dir}/wsSamples" />
    <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" />
    <!--Target that builds sample Web Service and client-->
    <target name="all" description="Target that builds sample Web Service and client" depends="clean,build.service,deploy" />
    <!--Target that builds sample Web Service and client-->
    <target name="build" description="Target that builds sample Web Service and client" depends="build.service" />
    <target name="clean">
    <delete dir="${ear.dir}" />
    <delete dir="${client.dir}" />
    </target>

    <!-- Target that builds the Web Service-->
    <target name="build.service" description="Target that builds the Web Service">
    <echo message="${java.class.path}" />
    <mkdir dir="${ear.dir}" />

    <jwsc srcdir="./src/com/blogspot/aoj/restws/" destdir="${ear.dir}" fork="true" keepGenerated="true" deprecation="${deprecation}" debug="${debug}" verbose="false">
    <jws file="${ws.file}.java" explode="true" type="JAXWS" >
    <descriptor file="./resources/web.xml" />
    </jws>
    </jwsc>
    </target>

    <!-- Target that deploys the Web Service-->
    <target name="deploy" description="Target that deploys the Web Service">
    <wldeploy action="deploy" source="${ear.dir}" user="${wls.username}" password="${wls.password}" verbose="true" failonerror="${failondeploy}" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" />
    </target>
    </project>
  • build.properties
    bea.home=c:/bea
    wl.home=${bea.home}/wlserver_10.3
    wls.hostname=localhost
    wls.port=7001
    wls.username=weblogic
    wls.password=password
    wls.server.name=AdminServer
    debuglevel=lines,vars,source
    sourceVersion=1.6

Wednesday, July 29, 2009

Spring AOP with XML-based Configuration

The previous post described how to use AspectJ annotations to implement Spring AOP. However, many programmers prefer to put configuration outside code. In such case Spring support for AOP through XML declaration of AOP components comes in handy. In this post we'll see how the same application can be implemented using Schema-based AOP support in Spring. An example of using pointcuts method parameters is also shown. Here's the code, followed by the explanations.

The Simple application ...
  • Search Engine Interface :
    /*
    * Author: Abhi Vuyyuru
    */
    package search;

    import java.util.List;

    public interface SearchEngine {
    public List<String> search(String prefix);
    }
    SearchEngine.java
  • Search Engine Class :
    /*
    * Author: Abhi Vuyyuru
    */
    package search;

    import java.util.ArrayList;
    import java.util.List;

    public class SearchEngineImpl implements SearchEngine {

    public List<String> search(String prefix) {
    System.out.println("In search implementation");
    List<String> list =
    new ArrayList<String>();
    list.add
    (prefix + " result 1");
    list.add
    (prefix + " result 2");
    return list;
    }

    }
    SearchEngineImpl.java
  • Main method:
    /*
    * Author: Abhi Vuyyuru
    */
    package main;

    import java.util.List;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    import search.SearchEngine;

    public class AopTest {

    public static void main(String[] args) {
    ApplicationContext ctx = new FileSystemXmlApplicationContext(
    "applicationContext.xml");

    SearchEngine searchEngine =
    (SearchEngine) ctx.getBean("searchEngine");
    List<String> searchResults = searchEngine.search
    ("search");
    System.out.println
    ("Number of results : " + searchResults.size());

    }
    }
    AopTest.java
Spring AOP classes and configuration
  • Appication Conext : This the spring configuration file for the example.
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    &nbspxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    &nbspxmlns:tx="http://www.springframework.org/schema/tx"
    &nbspxsi:schemaLocation="
    &nbsphttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    &nbsphttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    &nbsphttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!--
    <aop:aspectj-autoproxy proxy-target-class="true"/> <bean
    &nbspid="adviceObject" class="aop.AspectJAdvice" />
    &nbsp-->
    <aop:config>
    <!-- Pointcut for every method invocation -->
    <aop:pointcut id="searchEnginePc" expression="execution(* *(..))" />

    <!-- Pointcut for every method invocation with a String parameter -->
    <aop:pointcut id="pointcutWithParam" expression="execution(* *(..)) and args(prefix)" />

    <!-- After returning advice -->
    <aop:aspect id="springAspect" ref="adviceObject">

    <aop:around pointcut-ref="searchEnginePc" method="aroundAdvice" />

    <aop:after-returning pointcut-ref="searchEnginePc"
    &nbspmethod="afterAdvice" />

    <aop:before pointcut-ref="pointcutWithParam" method="beforeAdvice" />

    </aop:aspect>
    </aop:config>

    <bean id="adviceObject" class="aop.SpringAdvice" />
    <bean id="searchEngine" class="search.SearchEngineImpl" />
    </beans>
    applicationContext.xml
    • This xml is used for the default Spring AOP implementation with Java dynamic proxies. Note that the main method is programmed to the SearchEngine interface rather than the concrete class.
      SearchEngine searchEngine = (SearchEngine) ctx.getBean("searchEngine");
      Hence, the AOP configuration was set to
      <aop:config >
      If you have to use the concrete class in your application, as shown below
      SearchEngineImpl searchEngine = (SearchEngineImpl) ctx.getBean("searchEngine");
      You have to change the AOP configuration to the following
      <!-- proxy-target-class forces the use of CGLIB proxies, which allows proxying classes in addition to interfaces.-->
      <aop:config proxy-target-class="true">
      This will force the use of CGLIB proxies which allow proxying concrete classes too.
    • The aop-config element defines three different types of advice, around-advice, before advice, and after return advice.
      • Before Advice: Applied before a join point. This does not have the ability to prevent jointpoint (method execution) unless it throws an exception. The Before advice in the example uses a pointcut that uses arguments and can be used as a model for applying Advice for methods based on the parameter passed to the methods.
      • After Return Advice: Applied after the join points returns without exception.
      • Around Advice: Is applied around a join point. Note that the around advice is NOT called "before and after" the join point, but rather "around" the join point. If you look at the code below, the around advice is invoked when the join point is about to execute, but the around advice takes control of the execution. If you do not call the pjp.proceed() method in the following code, the join point will not be invoked.
        /*
        * Applied around a any public method.
        */
        public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Around advice before joinpoint");
        Object obj = pjp.proceed
        ();
        System.out.println
        ("Around advice after running");
        return obj;
        }
        Around advice has the ability to change the behavior of the join point, and even to stop the join point execution.
    • The Aspect:
    /*
    * Author: Abhi Vuyyuru
    */
    package aop;

    import org.aspectj.lang.ProceedingJoinPoint;

    public class SpringAdvice {
    /*
    * Applied around a any public method, based on XML configuration
    */
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("Around advice before joinpoint");
    Object obj = pjp.proceed
    ();
    System.out.println
    ("Around advice after running");
    return obj;
    }

    /*
    * Applied before the execution of any method which takes a String argument.
    * Based on XML configuration
    */
    public void beforeAdvice(String prefix) {
    System.out.println("Before advice : " + prefix);
    }

    /*
    * Applied after returning, based on XML configuration
    */
    public void afterAdvice() {
    System.out.println("After Returning advice");
    }



    }
    AspectJAdvice.java

Tuesday, July 28, 2009

Spring AOP with AspectJ Annotations

This post describes how to use Spring AOP with AspectJ aspects using AspectJ annotations. An example of using pointcuts method parameters is also shown. I assume that you are familiar with the basic AOP concepts, however, here are a couple of things that you must to know if you are new to AOP.
  • Cross-cutting concerns: Any part of the application that has implications through most of the major modules of the application may be termed as a cross-cutting concern. Coding such cross-cutting concerns into the business methods will not only increase code-complexity but also reduce re-usability. Application security and transaction management are the best examples of cross-cutting concerns.
  • Aspect: An aspect is the "unit of modularity" of any of the cross-cutting concerns within the applications. In Spring AOP, aspects are implemented using regular Java class
    • with annotations AspectJ is enabled
    • declaratively, without annotations, in the application context.
  • Join point and pointcut: join point is any point during the program execution of a program, such as the execution of a method or the handling of an exception. Spring AOP only supports methods as join points. A pointcut is a predicate which is used to identify join points.
  • Advice: Action applied at join points. Advice can be applied before, after or around a join point. In Spring, advice is modeled as an interceptor. Multiple advices hare maintained as a chain of intercptors around the join point.
One important thing to note here is that, by default, Spring uses Java Dynamic Proxies to implement Aspect Oriented Programming support. This requires you to "program to interfaces rather than classes" which is anyway a good programming practice. But if you have to code to concrete classes, you can force Spring AOP to use CGLIB proxies. How to do so is explained with the example. For an in-depth understanding of AOP concepts, refer to either AspectJ and/or Spring AOP documentation. Here's the code, followed by the explanations.

The Simple application ...
  • Search Engine Interface :
    /*
    * Author: Abhi Vuyyuru
    */
    package search;

    import java.util.List;

    public interface SearchEngine {
    public List<String> search(String prefix);
    }
    SearchEngine.java
  • Search Engine Class :
    /*
    * Author: Abhi Vuyyuru
    */
    package search;

    import java.util.ArrayList;
    import java.util.List;

    public class SearchEngineImpl implements SearchEngine {

    public List<String> search(String prefix) {
    System.out.println("In search implementation");
    List<String> list =
    new ArrayList<String>();
    list.add
    (prefix + " result 1");
    list.add
    (prefix + " result 2");
    return list;
    }

    }
    SearchEngineImpl.java
  • Main method:
    /*
    * Author: Abhi Vuyyuru
    */
    package main;

    import java.util.List;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    import search.SearchEngine;

    public class AopTest {

    public static void main(String[] args) {
    ApplicationContext ctx = new FileSystemXmlApplicationContext(
    "applicationContext.xml");

    SearchEngine searchEngine =
    (SearchEngine) ctx.getBean("searchEngine");
    List<String> searchResults = searchEngine.search
    ("search");
    System.out.println
    ("Number of results : " + searchResults.size());

    }
    }
    AopTest.java
Spring AOP classes and configuration
  • Appication Conext : This the spring configuration file for the example.
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!--Enables AspectJ auto-proxying-->
    <aop:aspectj-autoproxy />
    <bean id="adviceObject" class="aop.AspectJAdvice" />
    <bean id="searchEngine" class="search.SearchEngineImpl" />
    </beans>
    applicationContext.xml
    This xml is used for the default Spring AOP implementation with Java dynamic proxies. Note that the main method is programmed to the SearchEngine interface rather than the concrete class.
    SearchEngine searchEngine = (SearchEngine) ctx.getBean("searchEngine");

    Hence, the AOP configuration was set to
    <aop:aspectj-autoproxy />
    If you have to use the concrete class in your application, as shown below
    SearchEngineImpl searchEngine = (SearchEngineImpl) ctx.getBean("searchEngine");

    You have to change the AOP configuration to the following
    <!-- proxy-target-class forces the use of CGLIB proxies, which allows proxying classes in addition to interfaces.-->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    This will force the use of CGLIB proxies which allow proxying concrete classes too.
  • The Aspect: This aspect defines a pointcut and three different types of advice, around-advice, before advice, and after return advice.
    • Before Advice: Applied before a join point. This does not have the ability to prevent jointpoint (method execution) unless it throws an exception.
    • After Return Advice: Applied after the join points returns without exception.
    • Around Advice: Is applied around a join point. Note that the around advice is NOT called "before and after" the join point, but rather "around" the join point. If you look at the code below, the around advice is invoked when the join point is about to execute, but the around advice takes control of the execution. If you do not call the pjp.proceed() method in the following code, the join point will not be invoked.
      /*
      * Applied around a any public method.
      */
      @Around("execution(public * *(..))")
      public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
      System.out.println("Around advice before joinpoint");
      Object obj = pjp.proceed
      ();
      System.out.println
      ("Around advice after running");
      return obj;
      }
      Around advice has the ability to change the behavior of the join point, and even to stop the join point execution.
    /*
    * Author: Abhi Vuyyuru
    */
    package aop;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    @Aspect
    public class AspectJAdvice {

    /*
    * Applied around a any public method.
    */
    @Around("execution(public * *(..))")
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("Around advice before joinpoint");
    Object obj = pjp.proceed
    ();
    System.out.println
    ("Around advice after running");
    return obj;
    }

    /*
    * Applied before the execution of any method which takes a String argument.
    */
    @Before("execution(* *(..)) &&" + "args(prefix)")
    public void beforeAdvice(String prefix) {
    System.out.println("Before advice : " + prefix);
    }

    /*
    * Applied after returning from the pointcut defined by anyPublicMethod
    */
    @AfterReturning("anyPublicMethod()")
    public void afterAdvice() {
    System.out.println("After Returning advice");
    }

    /*
    * Defines a pointcut that matches any public method.
    */
    @Pointcut("execution(public * *(..))")
    private void anyPublicMethod() {
    }

    }
    AspectJAdvice.java
    • The pointcut here simply explains how a pointcut can be used instead of using predicates in the Advice annotations. Pointcuts come in handy when the advice predicates become too complex, in which case multiple pointcuts can be combined to form the proper predicate for matching the join point.
    • The Before advice in the example uses a predicate that uses arguments and can be used as a model for applying Advice for methods based on the parameter passed to the methods.

Wednesday, July 15, 2009

EasyMock for Unit Tests

One of the basic requirements for unit testing is the creation of mock objects. Easy mock is a library that helps to dynamically create mock objects. This way we can avoid much of the tedious work involved in manually coding mock objects. However, EasyMock itself is not the silver bullet, we still have to define expectations in test cases. Using EasyMock also means that you have make certain design changes (if your design follows the usual design principles you may not have to change much. Here's a sample Unit Test using EasyMock. You can download easymock from here.
  • Class Under Test:


    package sample;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class DateFormatter {
    public static final String DATE_FORMAT = "MM/dd/yyyy";

    private DateFormatterHelper helper = null;

    public DateFormatter() {
    }

    public String convertToStandardFormat(String dateString, String format) throws ParseException {
    if (dateString == null || dateString.equals(""))
    return "";
    dateString = dateString == null ? "" : dateString;
    DateFormat df = helper.getDateFormat(format);
    Date date = df.parse(dateString);
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    return sdf.format(date);
    }

    public DateFormatterHelper getHelper() {
    return helper;
    }

    public void setHelper(DateFormatterHelper helper) {
    this.helper = helper;
    }
    }
    DateFormatter.java
  • Helper Class and Interface: If you are to mock any class using EasyMock, it is required that you have an interface for the class. If you are following the old design principle "program to an interface, not an implementation", you are good here.

    package sample;

    import java.text.DateFormat;

    public interface DateFormatterHelper {
    public DateFormat getDateFormat(String format);
    }
    DateFormatterHelper.java


    package sample;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    public class DateFormatterHelperImpl implements DateFormatterHelper {

    public DateFormatterHelperImpl() {
    }

    public DateFormat getDateFormat(String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setCalendar(Calendar.getInstance());
    sdf.setLenient(false);
    return sdf;
    }


    public static void main(String args[]) {
    DateFormatterHelper helper = new DateFormatterHelperImpl();
    try {
    System.out.println(helper.getDateFormat("MM/dd/yyyy").parse("11/27/2008"));
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }
    DateFormatterHelperImpl.java
  • The test class:


    package tests;

    import static org.junit.Assert.fail;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;

    import org.easymock.EasyMock;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;

    import sample.DateFormatter;
    import sample.DateFormatterHelper;

    public class DateFormatterTests {

    private DateFormatter formatter = null;

    DateFormatterHelper helper = null;

    @Before
    public void setUp() throws Exception {
    helper = EasyMock.createMock(DateFormatterHelper.class);
    formatter = new DateFormatter();
    formatter.setHelper(helper);
    }

    @Test
    public void testConvertToStandardFormat() {

    String formatted = null;
    try {
    EasyMock.expect(helper.getDateFormat("MM-dd-yyyy")).andReturn(new SimpleDateFormat("MM-dd-yyyy"));
    EasyMock.replay(helper);
    formatted = formatter.convertToStandardFormat("11-27-2008", "MM-dd-yyyy");
    } catch (ParseException e) {
    e.printStackTrace();
    fail("Exception");
    }
    Assert.assertEquals(formatted, "11/27/2008");

    }
    }
    DateFormatterTests.java
    Note that in the EasyMock.expect method line, we use the "andReturn" method. This is to define the expected behavior when the method under test is invoked by the testcase. As can be expected, the replay method simply replays the pre-defined behavior when the actual call is made on the mock object.