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

Popular Posts