Wednesday, November 30, 2011

JSF Primefaces mobile Integration

Today I created my first mobile web application using Primefaces mobile and also integration with existing primefaces JSF application and it took me less than 15 minutes to setup and run the application in Weblogic 10.3.5. I have used the same managed bean for both desktop web and mobile web browsers and it  works great thanks to primefaces team powerd by JQuery mobile for awesome JSF components and detailed user guide.

There were few lessons learned during integration.

  1. Read JQuery mobile documentation it helps you during page creation. 
  2. If you are new to primefaces make sure you understand primefaces AJAX implementation.
  3. Read primefaces user guide (core & mobile) and and review the examples closely in the guide.
  4. Apply special focus while reading about <p:commandLink>. Feels silly?
  5. Follow mobile page design principals.
Update (04/11):

  • Setup the Render Kit Id as PRIMEFACES_MOBILE for parent <f:view> then you do not need to pass the request parameter with the URL  "?javax.faces.RenderKitId=PRIMEFACES_MOBILE" or you do not need to setup the default-render-kit-id in faces-config.xml
  • To make use of JQuery mini forms you can set mini="true" to the page element . Note needs primefaces mobile version 0.9.2
  • Add filter to dataList by adding the attribute filter
  • Fixed Header & Footer
  • Example code to create RSS reader Github 

Ex:


<f:view xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:pm="http://primefaces.org/mobile"
        contentType="text/html" renderKitId="PRIMEFACES_MOBILE">
    <pm:page title="Mobile Home" mini="true">
        <!-- Page content goes here -->
        <pm:view id="main">
            <pm:header title="Fixed Header" swatch="b" fixed="true"/>
            <pm:content>
                <p:dataList>
                    <f:attribute name="filter" value="true" />
                    <h:outputText value="Barcelona" />
                    <h:outputText value="Istanbul" />
                    <h:outputText value="New York" />
                    <h:outputText value="Paris" />
                </p:dataList>
            </pm:content>

          <pm:footer fixed="true" style="text-align: center; font-size: 10px">
                <h:outputText value="Fixed Footer"/>
            </pm:footer>

        </pm:view>
    </pm:page>
</f:view>



Wednesday, November 9, 2011

EL 2.2 and Weblogic 10.3.5

EL 2.2 has some nice features where in you can pass objects to the methods to perform some actions. This feature has nice advantage when you combine with JSF 2.0 however it will not work with JSF 1.2. If you are using JEE6 server EL 2.2 is supported out of box and for any that do not then you have to copy the el-api-2.2 and el-impl-2.2 jar files to web application class path (WEB-INF/lib) and add below context parameter to web.xml and it should work. For weblogic to support we have to add weblogic.xml file as well.

1. Copy the el-api-2.2 and el-impl-2.2 jar to web application class path (WEB-INF/lib). Below is the POM if you are using maven


 <!-- EL Dependencies -->
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>

2. Add below context param to web.xml


   <context-param>
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
    </context-param>

3. Add weblogic.xml below WEB-INF

<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
    <weblogic-version>10.3.4</weblogic-version>
    <context-root>demo</context-root>
    <container-descriptor>
        <prefer-application-packages>
            <package-name>com.sun.el.*</package-name>
            <package-name>javax.el.*</package-name>
        </prefer-application-packages>
    </container-descriptor>
</weblogic-web-app>

Now you can pass objects form JSF to actions directly.  Ex is  below


<h:form>
        <h:dataTable value="#{userManager.users}" var="user">
            <h:column>#{user.id}</h:column>
            <h:column>#{user.name}</h:column>
            <h:column>#{user.email}</h:column>
            <h:column><h:commandButton value="Edit" action="#{userManager.edit(user)}" /></h:column>
        </h:dataTable>
    </h:form>

And you Managed bean will have edit method


public void edit(User user) {
        this.user = user;
        this.edit = true;
    }



That's it happy coding :-)


Friday, October 21, 2011

JSF 2.0, Spring 3.0, JSR 299 and Weblogic 10.3.5

Unfortunately  Oracle Weblogic 10.3.5 is not fully JEE6 compatible server yet and the challenge for us was to develop new project that needs be deployed in OWS and the client is not sure of upgrade path yet.  So to make our project inline with JEE6 web profile we decided to use Spring DI internally uses JSR 299 and later it would be easy for us to re-factor our code if required since Spring DI could be optional with JEE6 as CDI and DI is provided out of the box by the web profile. The real decision for us is to use limited number of @Annotations that the developer has to worry about and also to be consistent and just us @Named, @Inject and @Scope instead of many other available annotations. Below maven file has the versions that we used to setup our project.

POM file


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.dvmr.poc</groupId>
<artifactId>jsfdar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jsf-dar</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsf.version>2.0.6</jsf.version>
<spring.version>3.0.5.RELEASE</spring.version>
<slf4j.version>1.6.1</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- JSF -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
</dependency>
<!-- slf4j/log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0.1</version>
</dependency>

<!-- Unit Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
                <!-- Add JAR to your local repository as this driver not found-->
<dependency>
<groupId>com.oracle</groupId>
 <artifactId>oracle</artifactId>
 <version>11.2.0.2.0</version>
</dependency>
<!-- Spring Inject -->
<dependency>
   <groupId>javax.inject</groupId>
   <artifactId>com.springsource.javax.inject</artifactId>
   <version>1.0.0</version>
</dependency>
</dependencies>
</project>



Simple JSF Managed Bean HelloWorld.java with JSF 2.0 Annotations


import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import com.dvmr.poc.service.ProjectsService;

@ManagedBean
@Scope("request")
public class HelloWorld {
private String hello;

@ManagedProperty(value="#{projectsService}")
private ProjectsService projectsService;

      //additional details .......
}

Now it becomes inline with JEE6 however the @Named and @Inject are using annotations form com.springsource.javax.inject-1.0.0.jar

import javax.inject.Inject;
import javax.inject.Named;

import com.dvmr.poc.service.ProjectsService;

@Named
@Scope("request")
public class HelloWorld {
private String hello;

@Inject
private ProjectsService projectsService;

        //additional details .......
}


Spring applicationContext.xml


<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.dvmr.poc"/>

</beans>


JSF faces-config.xml


<?xml version="1.0" encoding="utf-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0">
    <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

Make sure you update the web.xml  with ContextLoaderListener to load Spring context files.

That's it and you are good go now you can deploy it in weblogic 10.3.5 and using JEE6 annotations. How ever I really want to see weblogic 12g soon.

I will try to upload the project in github very soon :-)


Thursday, July 7, 2011

JAX-RS REST FileUpload and FileDownload example

In the process of comparing REST vs SOAP one of the test case for evaluation was document management service. i.e., upload and download files via services. I have previously consumed SOAP based service but it was having some serious limitations and performance issues since we have to read the complete file before writing it to output stream and has unnecessary data hops. Our goal is to deploy a simple lightweight services, with high performance dealing with documents using Jersey JAX-RS implementation.

Test case: Simple directory listing of files in a grid with an ability to upload, download and delete file as RESTful services.

Output looks some thing like this:



POM file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>wlrestgrid</groupId>
 <artifactId>wlrestgrid</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name />
 <description />
 
 <dependencies>
  <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.1</version>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-server</artifactId>
   <version>1.8</version>
  </dependency>
  <dependency>
   <groupId>asm</groupId>
   <artifactId>asm</artifactId>
   <version>3.1</version>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-json</artifactId>
   <version>1.8</version>
  </dependency>
  <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.8</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.jettison</groupId>
   <artifactId>jettison</artifactId>
   <version>1.3</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0.1</version>
  </dependency>
  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.2.2</version>
  </dependency>
 </dependencies>
 <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>
 <build>
  <sourceDirectory>${basedir}/src</sourceDirectory>
  <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
  <resources>
   <resource>
    <directory>${basedir}/src</directory>
    <excludes>
     <exclude>**/*.java</exclude>
    </excludes>
   </resource>
  </resources>
  <plugins>
  </plugins>
 </build>
</project>


web.xml file has configurations for Jersey see line 13 it is the package where the services are defined.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></display-name>
 <servlet>
  <display-name>JAX-RS REST Servlet</display-name>
  <servlet-name>JAX-RS REST Servlet</servlet-name>
  <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.dvmr.poc.rest</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Redirect</param-name>
            <param-value>true</param-value>
        </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>JAX-RS REST Servlet</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>


DocumentService.java has all the RESTful services defined to list, upload, download and delete file from a directory all the paths are highlighted.

package com.dvmr.poc.rest;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

import com.dvmr.poc.bean.FileBean;
import com.dvmr.poc.exception.NotFoundException;
import com.dvmr.poc.service.BlobService;
import com.dvmr.poc.service.impl.BlobServiceImpl;
import com.dvmr.poc.util.MultipartUtil;

/**
 * This rest based document service
 * @author vreddy.fp
 *
 */

@Path("/document")
public class DocumentService {

 /**
  * Replace with Inject annotation in JEE6 or with Spring 3.0
  */
 private final BlobService blobService = new BlobServiceImpl();

 public DocumentService() {
  super();
 }
 
 public BlobService getBlobService() {
  return blobService;
 }


 @POST
 @Path("upload")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 @Produces(MediaType.TEXT_PLAIN)
 public Response uploadFile(@Context HttpServletRequest request,
   @Context HttpServletResponse res) throws Exception {
  String response = "Unable to attach files";
  FileBean bean = MultipartUtil.parseMultipart(request, getBlobService());
  if (null != bean) {
   response = "{\"name\":\"" + bean.getFilename() + "\",\"type\":\""
   + bean.getContentType() + "\",\"size\":\"" + bean.getSize()
   + "\"}";
  }
  return Response.ok(response).build();
 }

 /**
  * In Memory solution
  * 
  * @param blobKey
  * @return
  * @throws Exception
  */
 @GET
 @Path("download")
 @Produces(MediaType.APPLICATION_OCTET_STREAM)
 public Response downloadFile(
   @DefaultValue("empty") @QueryParam(value = "blobKey") String blobKey)
   throws Exception {
  if(blobKey.equals("empty"))
   throw new NotFoundException("blobKey cannot be empty!");
  
  byte[] docStream = getBlobService().getBlob(blobKey);
  return Response
    .ok(docStream, MediaType.APPLICATION_OCTET_STREAM)
    .header("content-disposition", "attachment; filename = " + blobKey).build();
 }
 
 /**
  * list all valid files in a directory
  * @return
  * @throws JSONException
  */
 @GET
 @Path("list")
 @Produces( { MediaType.APPLICATION_JSON})
 public JSONArray listFiles() throws JSONException{
  JSONArray arr = new JSONArray();
  List<FileBean> list = getBlobService().getBlobs();
  for(Iterator<FileBean> i = list.iterator(); i.hasNext();){
   arr.put(i.next().toJson());
  }
  return arr;
 }
 
 /**
  * remove file from directory
  * @param blobKey
  * @return
  */
 @GET
 @Path("delete")
 @Produces(MediaType.TEXT_PLAIN)
 public Response deleteFile(@DefaultValue("empty") @QueryParam(value = "blobKey") String blobKey){
  
  if(blobKey.equals("empty"))
   throw new NotFoundException("blobKey cannot be empty!");
  
  getBlobService().deleteBlob(blobKey);
  return Response.status(Status.OK).build();
 }
 
}


FileBean.java is a simple POJO that holds file details.

package com.dvmr.poc.bean;

import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * 
 * @author vreddy.fp
 *
 */
@XmlRootElement
public class FileBean {
 String filename;
 long size;
 String url;
 String contentType;
 Map<String, String> formFieldsMap;

 public FileBean(String filename, long size, String url, String contentType) {
  this.filename = filename;
  this.size = size;
  this.url = url;
  this.contentType = contentType;
 }

 public FileBean() {
 }

 

 public String getFilename() {
  return filename;
 }

 public void setFilename(String filename) {
  this.filename = filename;
 }

 public long getSize() {
  return size;
 }

 public void setSize(long size) {
  this.size = size;
 }

 public String getUrl() {
  return url;
 }

 public void setUrl(String url) {
  this.url = url;
 }

 public String getContentType() {
  return contentType;
 }

 public void setContentType(String contentType) {
  this.contentType = contentType;
 }

 public Map<String, String> getFormFieldsMap() {
  return formFieldsMap;
 }

 public void setFormFieldsMap(Map<String, String> formFieldsMap) {
  this.formFieldsMap = formFieldsMap;
 }
 
 public JSONObject toJson() throws JSONException {
  JSONObject obj = new JSONObject();
  obj.put("name", filename);
  obj.put("size", new Long(size));
  obj.put("url", url);
  obj.put("contentType", contentType);
  return obj;
 }
 
}


MultipartUtil.java has the method to parse the request using commons streaming API for faster uploads and better performance for reading files.

package com.dvmr.poc.util;

import java.io.InputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;

import com.dvmr.poc.bean.FileBean;
import com.dvmr.poc.service.BlobService;

/**
 * 
 * @author vreddy.fp
 *
 */
public class MultipartUtil {

 /**
  * 
  * @param request
  * @return
  */
 public static FileBean parseMultipart(HttpServletRequest request, BlobService blobService){
  FileBean bean = null;
  if (ServletFileUpload.isMultipartContent(request)) { 
   bean = new FileBean();
            ServletFileUpload uploadHandler = new ServletFileUpload();
            InputStream stream = null;
            bean.setFormFieldsMap(new HashMap<String, String>());
            try {
                FileItemIterator itr = uploadHandler.getItemIterator(request);
                while(itr.hasNext()) {
                 FileItemStream item = itr.next();
                 String name = item.getFieldName(); // form field name
                    stream = item.openStream();
                    if(item.isFormField()) {
                     String value = Streams.asString(stream);
                     bean.getFormFieldsMap().put(name, value);
                    } else {
                       bean.setFilename(item.getName());
                         bean.setContentType(item.getContentType());
                         bean.setSize(blobService.uploadBlob(stream, item.getName()));
                    }
                }
            }catch(FileUploadException ex) {
             ex.printStackTrace();
            } catch(Exception ex) {
             ex.printStackTrace();
            }finally{
             IOUtils.closeQuietly(stream);
            }
        } 
  return bean;
 }
}

BlobService.java is the interface.

package com.dvmr.poc.service;

import java.io.InputStream;
import java.util.List;

import com.dvmr.poc.bean.FileBean;

public interface BlobService {
 /**
  * upload files to a directory
  * @param inputStream
  * @param filename
  * @return
  */
 public long uploadBlob(InputStream inputStream, String filename);
 /**
  * returns the file as a byte[]
  * @param blobKey
  * @return
  */
 public byte[] getBlob(String blobKey);
 
 /**
  * Deletes file form the directory
  * @param blobKey
  */
 public void deleteBlob(String blobKey);
 
 /**
  * get all files from the directory
  * @return
  */
 public List<FileBean> getBlobs();
}


BlobServiceImpl.java is the implementation for BlobService

package com.dvmr.poc.service.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CountingInputStream;

import com.dvmr.poc.bean.FileBean;
import com.dvmr.poc.service.BlobService;
import com.dvmr.poc.util.PropertyFileLoader;

/**
 * 
 * @author vreddy.fp
 *
 */
public class BlobServiceImpl implements BlobService {

 private PropertyFileLoader propertyFileLoader = PropertyFileLoader.getInstance("config");

 public long uploadBlob(InputStream inputStream, String filename) {
  Writer output = null;
  CountingInputStream countingInputStream = null;
  long filesize = 0;
  try {
   // if uploading form IE it get complete path
   filename = FilenameUtils.getName(filename);
   output = new FileWriter(new File(getDirctoryLoation()+ filename));
   countingInputStream = new CountingInputStream(inputStream);
   IOUtils.copy(countingInputStream, output);
   filesize = countingInputStream.getByteCount();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   IOUtils.closeQuietly(countingInputStream);
   IOUtils.closeQuietly(output);
  }
  return filesize;
 }

 /**
  * 
  */
 public byte[] getBlob(String blobKey) {
  File file = new File(getDirctoryLoation()+ blobKey);
  byte[] docStream = null;
  try {
   docStream = FileUtils.readFileToByteArray(file);
  } catch (IOException e) {
   e.printStackTrace();
  }
  return docStream;
 }

 public void deleteBlob(String blobKey) {
  FileUtils.deleteQuietly(new File(getDirctoryLoation()+ blobKey));
 }

 /**
  * 
  */
 public List<FileBean> getBlobs() {
 
  List<FileBean> list = new ArrayList<FileBean>();
  Iterator<File> files = FileUtils.iterateFiles(
    new File(getDirctoryLoation()),
    getValidFileExtentions(),
    false);

  while (files.hasNext()) {
   File file = files.next();
   FileBean fileBean = new FileBean(
     file.getName(), 
     file.length(),
     file.getAbsolutePath(), 
     FilenameUtils.getExtension(file.getName()));
   list.add(fileBean);
  }
  return list;
 }

 public String getDirctoryLoation() {
  return propertyFileLoader.getValue("directory.location");
 }

 public String[] getValidFileExtentions() {
  return propertyFileLoader.getValue("valid.file.extentions").split("\\|");
 }

}

PropertyFileLoader.java is Util class to loads config.properties file. This file as the Directory location to where the files are written and displayed.

package com.dvmr.poc.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

/**
 * 
 * @author vreddy.fp
 *
 */
public class PropertyFileLoader {

 private static PropertyFileLoader instance = null;
 private static final String PROPERTIES_FILE = ".properties";
 Properties properties = null;

 
 public PropertyFileLoader() {
  super();
 }
 
 public static PropertyFileLoader getInstance(String name) {
       if(instance == null) {
          instance = new PropertyFileLoader(name);
       }
       return instance;
    }

 public PropertyFileLoader(String name) {
  loadProperties(name);
 }

 public void loadProperties(String name) {
  properties = new Properties();
  InputStream in = this.getClass().getResourceAsStream("/com/dvmr/poc/resources/" + name + PROPERTIES_FILE);
  try {
   properties.load(in);
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   IOUtils.closeQuietly(in);
  }

 }
 
 public String getValue(String key){
  return properties.getProperty(key);
 }
}


config.properties file is defined in com.dvmr.poc.resources package.

directory.location=C\:\\temp\\logs\
valid.file.extentions=gif|jpg|JPG|png|txt|jpeg

fileutil.jsp file uses JQuery for JSOP, SlickGrid for to list the files in grid and jquery.form.js plugin to upload files using ajax. Copy necessary plugin files including css and image files to your web application.

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'fileutil.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <link rel="stylesheet" href="css/slickgrid/slick.grid.css" type="text/css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
  <link rel="stylesheet" href="css/slickgrid/examples.css" type="text/css" media="screen" charset="utf-8" />
 </head>

 <body>
  <h3>Directory Browser that displays only (gif, jpg, JPG, png, txt, jpeg)</h3>
  <table width="100%">
  <tr>
   <td valign="top" width="50%">
    <div id="myGrid" style="width:600px;height:500px;"></div>
   </td>
  </tr>
  </table>
  <hr>
  <h3>Upload file (Uses Iframe)</h3>
  <form id="myform" action="services/document/upload" method="POST" enctype="multipart/form-data">
   <div id="fileSection">
    File:
    <input type="file" name="fileup0" />
   </div>
   <input type="submit" value="Upload file" />
  </form>


 </body>
 
     <script language="JavaScript" src="lib/jquery-1.4.3.min.js"></script> 
  <script language="JavaScript" src="lib/jquery-ui-1.8.5.custom.min.js"></script> 
  <script language="JavaScript" src="lib/jquery.event.drag-2.0.min.js"></script> 
  <script language="JavaScript" src="lib/jquery.form.js"></script>
        
        <script language="JavaScript" src="javascripts/slickgrid/slick.core.js"></script> 
  <script language="JavaScript" src="javascripts/slickgrid/slick.editors.js"></script> 
  <script language="JavaScript" src="javascripts/slickgrid/slick.grid.js"></script> 
 <script>
 $(function() {
  loadGrid();
  ajaxFileUpload();
  downloadFile();
  deleteFile();
 });
 
 
 function downloadFile(){
  $(".download-file").click(function(event) {
   event.preventDefault();
   if ($("#downloadFrame").length == 0) {
    $('<iframe id="downloadFrame" style="display:none;" src="about:none"></iframe>').appendTo('body');
   }
   $("#downloadFrame").attr("src", $(this).attr("href"));
  });
 }
 
 function deleteFile(){
  $(".delete-file").live("click", function(event){
    event.preventDefault();
    $.getJSON($(this).attr("href"), function(vals) {
      loadGrid();
  });
  });
 }
 
 function ajaxFileUpload(){
  function cb_success (rt, st, xhr, wf) {
     loadGrid();
     $('#myform').resetForm();
  }
  var options = {
         success: cb_success,
         dataType: 'html',
         contentType: 'text/plain',
         method: 'POST'
     };
     $('#myform').ajaxForm(options);
 }
 
 function loadGrid(){
   var grid;
  var data = [];
  var downloadFileFormatter = function(row, cell, value, columnDef, dataContext) {
      return "<a class='download-file' href='services/document/download?blobKey=" + dataContext["name"] + "'>Download</a>" +
      "&nbsp;<a class='delete-file' href='services/document/delete?blobKey=" + dataContext["name"] + "'>Delete</a>";
  };
  var columns = [
   {id:"download", name:"Actions", formatter:downloadFileFormatter, width:110},
   {id:"name", name:"Name", field:"name", width:120, cssClass:"cell-title"},
   {id:"size", name:"Size", field:"size"},
   {id:"url", name:"Path", field:"url", width:200},
   {id:"contentType", name:"Type", field:"contentType", width:50}
  ];
 
  var options = {
   enableAddRow: false,
   enableCellNavigation: true
  };
  
   $.getJSON("services/document/list?timire="+Math.min(100, Math.round(Math.random() * 110)) ,  function(vals){
                $.each(vals, function() { 
                    data.push(this); 
                }); 
                grid = new Slick.Grid($("#myGrid"), data, columns, options); 
            }); 
 }
</script>
</html>

Source code is available @ GitHub finally :-)

Monday, June 27, 2011

Weblogic 10.3.4+ Jersey JAX-RS + JSON + SlickGrid

I want to try some RESTful services returning JSON response and use response in JS widgets as part of POC to propose them as an alternative to SOAP based services. Many developers have implemented similar functionality before and we can find many examples on the web (google). But I wanted to try it out myself before I agree with other developers who advocate for REST bases services and to be honest I am convinced after building a simple example application in less than 2 hrs.

My goal is to deploy the example application in Oracle weblogic 10.3.4 and use Jersey JAX-RS 1.5 implementation as this implementation is supported by weblogic and comes as part of shared library. Below are the sequence of steps that I have followed:

1. Enable Jersey JAX-RS implementation in weblogic. Link

2. Setup Maven for weblogic 10.3.4. Link

3. Create a Dynamic webproject in MyEclipse and create a simple service that returns JSON array as response.

POM file

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>wlrestgrid</groupId>
 <artifactId>wlrestgrid</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name />
 <description />
 <dependencies>
  <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.1</version>
  </dependency>
  <dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.1.12</version>
  </dependency>
  <dependency>
   <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.1</version>
  </dependency>
  <dependency>
   <groupId>javax.xml.stream</groupId>
   <artifactId>stax-api</artifactId>
   <version>1.0-2</version>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-core</artifactId>
   <version>1.4</version>
  </dependency>
  <dependency>
   <groupId>javax.ws.rs</groupId>
   <artifactId>jsr311-api</artifactId>
   <version>1.1.1</version>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-server</artifactId>
   <version>1.4</version>
  </dependency>
  <dependency>
   <groupId>asm</groupId>
   <artifactId>asm</artifactId>
   <version>3.1</version>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-client</artifactId>
   <version>1.4</version>
  </dependency>
  <dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-json</artifactId>
   <version>1.4</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.jettison</groupId>
   <artifactId>jettison</artifactId>
   <version>1.1</version>
  </dependency>
 </dependencies>
 <build>
  <sourceDirectory>${basedir}/src</sourceDirectory>
  <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
  <resources>
   <resource>
    <directory>${basedir}/src</directory>
    <excludes>
     <exclude>**/*.java</exclude>
    </excludes>
   </resource>
  </resources>
  <plugins>
   <plugin>
    <groupId>com.oracle.weblogic</groupId>
    <artifactId>weblogic-maven-plugin</artifactId>
    <version>10.3.4</version>
    <configuration>
     <adminurl>t3://localhost:7001</adminurl>
     <user>weblogic</user>
     <password>WEBLOGIC_PASSWORD</password>
     <upload>true</upload>
     <action>deploy</action>
     <remote>false</remote>
     <verbose>true</verbose>
     <source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
     <name>${project.build.finalName}</name>
    </configuration>
    <executions>
     <execution>
      <phase>install</phase>
      <goals>
       <goal>deploy</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>a
</project>

web.xml will have JAX-RS servlet mapping

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></display-name>
 <servlet>
  <display-name>JAX-RS REST Servlet</display-name>
  <servlet-name>JAX-RS REST Servlet</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>JAX-RS REST Servlet</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>


HelloWorld.java

package com.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

import com.bean.GridObject;


@Path("/helloworld")
public class HelloWorld {
 @Context
 private UriInfo context;

 public HelloWorld() {
 }

 @GET
 @Path("getGridData")
 @Produces( { MediaType.APPLICATION_JSON})
 public JSONArray getGridByJson() throws JSONException{
  JSONArray arr = new JSONArray();
  for(int i=0; i< 100; i++){
      arr.put(new GridObject("Task "+i, "5 days", i * 1, "01/01/2009", "01/05/2009", i % 5==0).toJson());
    }
        return arr;
 }
 
}

GridObject.java is a simple POJO object with toJson() method to return json object

package com.bean;

import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

@XmlRootElement
public class GridObject {
 private String title, duration;
 private int percentComplete;
 private boolean effortDriven;
 private String start, finish;

 
 public GridObject() {
  super();
  // TODO Auto-generated constructor stub
 }

 public GridObject(String title, String duration, int percentComplete,
   String start, String finish, boolean effortDriven) {
  this.title = title;
  this.duration = duration;
  this.percentComplete = percentComplete;
  this.start = start;
  this.finish = finish;
  this.effortDriven = effortDriven;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getDuration() {
  return duration;
 }

 public void setDuration(String duration) {
  this.duration = duration;
 }

 public int getPercentComplete() {
  return percentComplete;
 }

 public void setPercentComplete(int percentComplete) {
  this.percentComplete = percentComplete;
 }

 public boolean isEffortDriven() {
  return effortDriven;
 }

 public void setEffortDriven(boolean effortDriven) {
  this.effortDriven = effortDriven;
 }

 public String getStart() {
  return start;
 }

 public void setStart(String start) {
  this.start = start;
 }

 public String getFinish() {
  return finish;
 }

 public void setFinish(String finish) {
  this.finish = finish;
 }

 public JSONObject toJson() throws JSONException {
  JSONObject obj = new JSONObject();
  obj.put("title", title);
  obj.put("duration", duration);
  obj.put("percentComplete", new Integer(percentComplete));
  obj.put("start", start);
  obj.put("finish", finish);
  obj.put("effortDriven", new Boolean(effortDriven));
  return obj;
 }

}



4. Create a JSP page that invokes the RESTful service using JQuery.getJSON() method to get the response and use this response to generate the slickgrid. Why slickgrid?? I like it after seeing the comprehensive exmaple.

Copy all necessary javascript, css and image files from slickgrid to your web applicaiton.

slickgrid.jsp

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>SlickGrid example 1: Basic grid</title>
  <link rel="stylesheet" href="css/slickgrid/slick.grid.css" type="text/css" media="screen" charset="utf-8" />
        <link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" />
  <link rel="stylesheet" href="css/slickgrid/examples.css" type="text/css" media="screen" charset="utf-8" />
 </head>
 <body>
  <table width="100%">
  <tr>
   <td valign="top" width="50%">
    <div id="myGrid" style="width:600px;height:500px;"></div>
   </td>
   <td valign="top">
    <h2>Demonstrates:</h2>
    <ul>
     <li>basic grid with minimal configuration</li>
    </ul>
   </td>
  </tr>
  </table>

  <script language="JavaScript" src="lib/jquery-1.4.3.min.js"></script> 
  <script language="JavaScript" src="lib/jquery-ui-1.8.5.custom.min.js"></script> 
  <script language="JavaScript" src="lib/jquery.event.drag-2.0.min.js"></script> 
        
        <script language="JavaScript" src="javascripts/slickgrid/slick.core.js"></script> 
  <script language="JavaScript" src="javascripts/slickgrid/slick.editors.js"></script> 
  <script language="JavaScript" src="javascripts/slickgrid/slick.grid.js"></script> 
  <script>

  var grid;
  var data = [];
  var columns = [
   {id:"title", name:"Title", field:"title", width:120, cssClass:"cell-title"},
   {id:"duration", name:"Duration", field:"duration"},
   {id:"%", name:"% Complete", field:"percentComplete", width:80, resizable:false, formatter:GraphicalPercentCompleteCellFormatter},
   {id:"start", name:"Start", field:"start", minWidth:60},
   {id:"finish", name:"Finish", field:"finish", minWidth:60},
   {id:"effort-driven", name:"Effort Driven", sortable:false, width:80, minWidth:20, maxWidth:80, cssClass:"cell-effort-driven", field:"effortDriven", formatter:BoolCellFormatter}
  ];
 
  var options = {
   //editable: false,
   enableAddRow: false,
   enableCellNavigation: true
  };

  $(function() {
       //http://localhost:8080/wlrestgrid/services/helloworld/getGridData
             $.getJSON("services/helloworld/getGridData?timire="+Math.min(100, Math.round(Math.random() * 110)) , 
                function(vals){
                $.each(vals, function() { 
                    data.push(this); 
                }); 
                grid = new Slick.Grid($("#myGrid"), data, columns, options); 
            }); 
        });

  </script>

 </body>
</html>



5. Deploy the application to Oracle weblogic server using Maven and open the slickgrid.jsp file in the browser.

Tuesday, June 21, 2011

Oracle Weblogic 10.3.4

After installing Oracle Weblogic 10.3.4 zip installation on Window XP I was wondering how to create JNDI Data Source using the admin console and did not find a way. So finally I have to write my WLST script and after that I started to like WLST and was amazed with so many things that you can do with this little tool. Below is the WLST script to create Datasource using properties file.

1. Save WLST as CreateDataSource.py
2. Save the below properties file as domain.properties
3. For running WLST scripts follow this link

#Conditionally import wlstModule only when script is executed with jython
if __name__ == '__main__': 
    from wlstModule import *#@UnusedWildImport

from java.io import FileInputStream

propInputStream = FileInputStream("PATH_TO_PROP_FILES/domain.properties")
configProps = Properties()
configProps.load(propInputStream)

print 'starting the script ....'

connect(configProps.get("domain.admin.username"),
        configProps.get("domain.admin.password"), 
        configProps.get("domain.admin.url"))


edit()
startEdit()

server= configProps.get("domain.server.name")

print 'server is ' + server

cd("Servers/"+server)
target=cmo
cd("../..")

dsname=configProps.get("domain.ds.name")
jndiname=configProps.get("domain.jndi.name")
jdbc_url =configProps.get("domain.ds.jdbc.url")
jdbc_driver= configProps.get("domain.ds.jdbc.driver") 
jdbc_user=configProps.get("domain.ds.jdbc.user")
jdbc_pwd=configProps.get("domain.ds.jdbc.pwd")

# start creation
print 'Creating JDBCSystemResource with name '+dsname
jdbcSR = create(dsname,"JDBCSystemResource")
theJDBCResource = jdbcSR.getJDBCResource()
theJDBCResource.setName(dsname)

connectionPoolParams = theJDBCResource.getJDBCConnectionPoolParams()
connectionPoolParams.setConnectionReserveTimeoutSeconds(25)
connectionPoolParams.setMaxCapacity(100)
connectionPoolParams.setTestTableName("SQL SELECT 1 FROM DUAL")
connectionPoolParams.setConnectionCreationRetryFrequencySeconds(100);

dsParams = theJDBCResource.getJDBCDataSourceParams()
dsParams.addJNDIName(jndiname)


driverParams = theJDBCResource.getJDBCDriverParams()
driverParams.setUrl(jdbc_url)
driverParams.setDriverName(jdbc_driver)

driverParams.setPassword(jdbc_pwd)
driverProperties = driverParams.getProperties()

proper = driverProperties.createProperty("user")
proper.setValue(jdbc_user)

jdbcSR.addTarget(target)

save()
activate(block="true")

print 'Done configuring the data source'

Below are the properties files

###################
 Domain-1 Details
###################
domain.name=mydomain
domain.admin.url=t3://localhost:7001
domain.admin.username=weblogic
domain.admin.password=WL_PASSWORD
domain.server.name=myserver

domain.ds.name=JDBCDataSource
domain.jndi.name=jdbc.tempDS 


domain.ds.jdbc.url=jdbc:oracle:thin:@SERVER_NAME:1521:SERVER_INSTANCE
domain.ds.jdbc.driver=oracle.jdbc.driver.OracleDriver
domain.ds.jdbc.user=scott
domain.ds.jdbc.pwd=tiger

PF