10-Jun-03 (Created: 10-Jun-03) | More in 'Howto-Advanced'

How to use and setup HttpEvents?

Philosophy

HttpEvents is a concept where the application programmer can monitor what is happening in the application server. The programmer can monitor and responds to such events as session start, request begin, request end etc. The implementation is somewhat inspired by the global.asax file in the asp.net platform.

Currently this is a bare bones implementation. For instnace the need to use a full event based programming where each function call or event can be intercepted by more than one is not fully explored. Currently this is not a full blown publish/subscribe model. This will be investigated as I go further down the road.

The current implementation is expected to serve the basic needs.

As part of the future, I will also look at servlet filters and any additional facilities that the newer servlet JDKs have to offer will be looked at. I will also study the asp.net HttpPipeline a bit closer.

The IHttpEvents class


public interface IHttpEvents
{
   public static String NAME="IHttpEvents";

   public void applicationStart() throws AspireServletException;

   public void applicationStop()  throws AspireServletException;

   public void sessionStart(HttpSession session, 
                            HttpServletRequest request, 
                            HttpServletResponse response) throws AspireServletException;

   public void sessionStop() throws AspireServletException;

   public void beginRequest(HttpServletRequest request, 
                            HttpServletResponse response) throws AspireServletException;

   public void endRequest(HttpServletRequest request, 
                          HttpServletResponse response) throws AspireServletException;

   public void userLogin(String username, 
                         HttpSession session, 
                         HttpServletRequest request, 
                         HttpServletResponse response) throws AspireServletException;

   public void userChange(String oldUser, 
                          String newUser, 
                          HttpSession session, 
                          HttpServletRequest request, 
                          HttpServletResponse response) throws AspireServletException;

}

Each method in this interface refers to a significant event in the application server. Aspire will call the corresponding method when that event occurs.

Implement the interface

You can take advantage of this by implementing this interface and telling Aspire where the implemantation is available. Here is an implementation example:


public class com.mypkg.MyHttpEventMonitor implements IHttpEvents {}

Although you can directly implement all of the interfaces, there is a helper concrete class that you can inherit from so that you don?t have to implement all of the functions. The default implmentation writes a log message to the log file. This class is called "com.ai.servlets.DefaultHttpEventEvents".

when you inherit from another concrete class, it is a good idea to call the parent method. This is nevertheless subjective and depends on the base class implementation.

Identify it to aspire via the properties file


Request.IHttpEvents.classname=com.mypkg.MyHttpEventMonitor

com.ai.servlets.DefaultHttpEvents1

There is an additional class that you can use as an example called "com.ai.servlets.DefaultHttpEvents1". This class is implemented for the purposes of AKC. But you can use it as an example. This class is generic enough that you can use it for more than AKC. The behavior of this class is to load a set of key value pairs from a database into the session when the user logs in. Because of this configurability this class has its own properties file section.

Properties file

Here is how you configure this class in the properties file

# HttpEvents
request.IHttpEvents.classname=com.ai.servlets.DefaultHttpEvents1
request.aspire.sessionSupport.newUserSessionLoader.classname=com.ai.parts.SessionLoaderPart
request.aspire.sessionSupport.newUserSessionLoader.loadVariablesRequestName=loadUserSessionParams

request.loadUserSessionParams.classname=com.ai.db.DBRequestExecutor2
request.loadUserSessionParams.db=reportsDB
request.loadUserSessionParams.stmt=\
	select first_name as user_first_name \
		last_name as user_last_name \
		email as user_email \
	from users \
	where user_id = {profile_user.quote}

Source code for defaulthttpevents1


package com.ai.servlets;

import com.ai.application.utils.*;
import com.ai.application.interfaces.*;
import javax.servlet.http.*;
import java.util.*;

public class DefaultHttpEvents1 extends DefaultHttpEvents
{
   public void userLogin(String username,  HttpSession session, HttpServletRequest request, HttpServletResponse response)
         throws AspireServletException
   {
      AppObjects.log("Info:user loggedin event");

      Hashtable args = new Hashtable();
      args.put("aspire_session",session);
      args.put("profile_user",username);
      try
      {
         AppObjects.getObject(AspireConstants.SESSION_SUPPORT_NEW_USER_SESSION_LOADER,args);
      }
      catch(RequestExecutionException x)
      {
         throw new AspireServletException("Error:" + x.getRootCause(), x);
      }
   }//eof-function
}//eof-class

Aspire Build information

This facility is available from build 18.5 onwards

Relevent Aspire source files


1. com.ai.servlets.IHttpEvents
2. com.ai.servlets.SWIHttpEvents
3. com.ai.servlets.DefaultHttpEvents
4. com.ai.servlets.DefaultHttpEvents1
5. com.ai.servlets.BaseServlet
6. com.ai.servletutils.SessionUtils