Getting started with Stripes & Google App Engine

This is a basic example of using Stripes Framework with the cloud infrastructure of Google. I am using Stripes in one of my projects that I am thinking to move it to google app engine so I decided to experiment before the migration. More posts will follow in this topic as I progress with it. The first objective is to create the basic project structure, configure and deploy, well… a hello world web application. This is a trivial application but there are some limitations using google app engine so trying to use the framework as is will fail with some non-sense stack traces.

The following is a list of things that we will be need to complete this application:
1. Stripes Framework (home page: http://www.stripesframework.org/display/stripes/Home )
2. Google app engine sdk – if you use eclipse and google plugin the sdk is included in the plugin.
3. Eclipse (you can use whatever IDE or environment you like, it’s not mandatory to use eclipse, it’s just easier).

Note: For information on how to install the google plugin visit http://code.google.com/appengine/docs/java/tools/eclipse.html

I assume you are familiar with java web frameworks and that you will use eclipse with the google plugin.

The first step is to create a project in the eclipse IDE:

Give a name to the project and an initial package name. For the moment we do not need GWT so just deselect it.

After pressing finish we have the following project structure.

Before continuing copy the Stripes jars in the the lib directory under hello_project/web/WEB_INF/lib and add them to the project’s build path by selecting them, right click-> Build Path -> Add to Build Path. Also copy and paste the StripesResources.properties in the directory “src” (not in a package).

By default the google plugin will generate a servlet for us, under the package name that we entered in the previews step. It will also generate the following configuration files:
Servlet spec requirement:
1. web.xml (this is the well known deployment descriptor required by the servlet specification)

Google App engine specific (we will not use them for this application):
1. appengine-web.xml
2. jdoconfig.xml

Logging property files:
1. log4j.properties
2. logging.properties

For the purpose of this trivial application we will only need to edit the web.xml (a.k.a Deployment Descriptor so from know on I will call it DD).
By default the plugin registers the generated servlet in the DD, so open the DD and delete the servlet and servlet-mapping it’s not needed.

The following is the generated DD that need to be edited:

The following is the new and final DD containing the Stripes filter and dispatcher servlet:

Note that the web-app tag version has changed from 2.5 to 2.4 there are some jsp exceptions with the 2.5.

The Stripes filter includes two initial parameters the ActionResolver.Packages which tells Stripes where to find the ActionBeans (more on this in next post or in Stripes documentation) and the MultipartWrapperFactory.Class, a factory class used to upload files, but uploading files is not supported by google app engine and the Stripes filter will fail to initialize. Therefore we need to disable this by providing an empty configuration.

The first step to disable file uploading is to add MultipartWrapperFactory.Class initial parameter to the Stripes filter as shown in the DD and the second step is to create the appropriate class. So create a package in the project (I created the com.helloworld.exclude but you can create anything that make sense for your project) and add the following class:


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import net.sourceforge.stripes.config.Configuration;
import net.sourceforge.stripes.controller.FileUploadLimitExceededException;
import net.sourceforge.stripes.controller.multipart.MultipartWrapper;
import net.sourceforge.stripes.config.ConfigurableComponent;
import net.sourceforge.stripes.controller.multipart.MultipartWrapperFactory;

/**
 * GAE does not support file uploading so we need to disable this feature from Stripes.
 *
 * @author 110j
 */
public class EmptyMultipartWapper implements ConfigurableComponent, MultipartWrapperFactory {

	/**
	 * @see net.sourceforge.stripes.config.ConfigurableComponent#init(net.sourceforge.stripes.config.Configuration)
	 */
	public void init(Configuration conf) throws Exception {
	}

	/**
	 * @see net.sourceforge.stripes.controller.multipart.MultipartWrapperFactory#wrap(javax.servlet.http.HttpServletRequest)
	 */
	public MultipartWrapper wrap(HttpServletRequest request) throws IOException, FileUploadLimitExceededException {
		return null;
	}
}

Now that we disabled the file uploading lets create an ActionBean that will handle our request. Create a package in the project called com.helloworld.action and add the following class:


package com.helloworld.action;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

/**
 * The action bean that will handle the our simple request.
 *
 * @author 110j
 */
public class EasyActionBean implements ActionBean {
	private static final String VIEW = "/WEB-INF/jsp/hello_world.jsp";
	private static final String MY_MESSAGE = "hello world";
	private ActionBeanContext ctx;

	private String message;

	@DefaultHandler
	public Resolution showMe() {
		this.setMessage(MY_MESSAGE);
		return new ForwardResolution(VIEW);
	}

	/**
	 * @see net.sourceforge.stripes.action.ActionBean#getContext()
	 */
	public ActionBeanContext getContext() {
		return this.ctx;
	}

	/**
	 * @see net.sourceforge.stripes.action.ActionBean#setContext(ActionBeanContext)
	 */
	public void setContext(ActionBeanContext ctx) {
		this.ctx = ctx;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
}

Next lets create a jsp file that will render the response, create the hello_world.jsp under WEB_INF/jsp/ and add the following content:

Finally create a welcome file called index.jsp if it does not exist or rename the index.html to index.jsp and add just a forward to the action:

You can delete the servlet generated by the google plugin there is no need for it.

In order to test it you just need to run the hello_world in the Run history of eclipse as the google plugin will create one for us, or under the run button in the toolbar, and then visit the address localhost:8080

NOTE: if your application require session you need to enable it in the appengine-web.xml since it is disabled by default (but keep in mind the implication it may have in a distributed environment such as google app engine, maybe your attributes will need to implement the HttpSessionActivationListener if you plan to add them in the session).

You can upload your application by using eclipse or the command line tool, for more information visit http://code.google.com/appengine/docs/java/gettingstarted/uploading.html

To be continued!

Advertisements