nu.staldal.lsp.framework
Class ThrowawayService

java.lang.Object
  extended by nu.staldal.lsp.framework.ThrowawayService
Direct Known Subclasses:
EasyService

public abstract class ThrowawayService
extends java.lang.Object

Abstract base class for Throwaway Service.

Services will be loaded and instantiated using Class.forName and Class.newInstance, so they must have a public no-arg constructor.

A new instance will be created for each request, and that instance is thrown away after the request. The execute method is only invoked once per instance, so there are no thread-safety issues. It's not a problem to use instance fields.

Use the Parameter annotation on instance fields to automatically populate them with HTTP request parameter values.


Field Summary
protected  javax.servlet.ServletContext context
          The ServletContext.
protected  DBConnection dbConn
          database connection to use for this request, or null if no database has been setup
protected  java.util.List<java.lang.String> extraArgs
          Any extra URL path components from RestfulDispatcherServlet, or null if another dispatcher is used.
static java.lang.String INCLUDE_ATTR_PREFIX
          Prefix for request attributes for include attributes.
protected  javax.sql.DataSource mainDB
          The DataSource to main database, or null if no database is setup.
protected  javax.servlet.http.HttpServletRequest request
          The HttpServletRequest.
static int REQUEST_DELETE
          Request type DELETE.
static int REQUEST_GET
          Request type GET.
static int REQUEST_INCLUDE
          Request type include.
static int REQUEST_POST
          Request type POST.
static int REQUEST_PUT
          Request type PUT.
protected  int requestType
          The request type.
protected  javax.servlet.http.HttpServletResponse response
          The HttpServletResponse.
 
Constructor Summary
ThrowawayService()
           
 
Method Summary
abstract  java.lang.String execute(java.util.Map<java.lang.String,java.lang.Object> pageParams)
          Invoked for a request to this Service.
protected  java.util.Locale getUserLocale()
          Get the user's locale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_GET

public static final int REQUEST_GET
Request type GET.

See Also:
Constant Field Values

REQUEST_POST

public static final int REQUEST_POST
Request type POST.

See Also:
Constant Field Values

REQUEST_INCLUDE

public static final int REQUEST_INCLUDE
Request type include.

See Also:
Constant Field Values

REQUEST_PUT

public static final int REQUEST_PUT
Request type PUT.

See Also:
Constant Field Values

REQUEST_DELETE

public static final int REQUEST_DELETE
Request type DELETE.

See Also:
Constant Field Values

INCLUDE_ATTR_PREFIX

public static final java.lang.String INCLUDE_ATTR_PREFIX
Prefix for request attributes for include attributes.

See Also:
Constant Field Values

context

protected javax.servlet.ServletContext context
The ServletContext.


request

protected javax.servlet.http.HttpServletRequest request
The HttpServletRequest.


response

protected javax.servlet.http.HttpServletResponse response
The HttpServletResponse.


mainDB

protected javax.sql.DataSource mainDB
The DataSource to main database, or null if no database is setup.


dbConn

protected DBConnection dbConn
database connection to use for this request, or null if no database has been setup


requestType

protected int requestType
The request type. REQUEST_GET, REQUEST_POST, REQUEST_PUT, REQUEST_DELETE or REQUEST_INCLUDE


extraArgs

protected java.util.List<java.lang.String> extraArgs
Any extra URL path components from RestfulDispatcherServlet, or null if another dispatcher is used.

Constructor Detail

ThrowawayService

public ThrowawayService()
Method Detail

getUserLocale

protected java.util.Locale getUserLocale()
Get the user's locale.

Returns:
the user's locale, never null

execute

public abstract java.lang.String execute(java.util.Map<java.lang.String,java.lang.Object> pageParams)
                                  throws java.lang.Exception
Invoked for a request to this Service.

Is invoked for GET, HEAD, POST, PUT and DELETE requests. You should not treat HEAD requests differently than GET requests, the framework will automatically discard the body and only send the headers. The requestType field indicate the type of request. See the HTTP specification for differences between GET and POST requests.

There are three choices to create the response:

  1. Return the name of a page to display, and fill pageParams with parameters to this page. In this case, response should only be used to set headers.
  2. Send the whole response by using response and return null. In this case the framework will not touch response after this method returns, and will not use pageParams. This can be used if you want to use sendError or sendRedirect.
  3. Return the name of an other service to forward the request to, prefixed by "*". Any parameters added to pageParams are retained. You may add attributes to request in order to comnunicate with the other service.

If requestType is REQUEST_INCLUDE, choice 2 and 3 may not be used, and response may not be modified in any way. You may either return the name of page, or use the SAX2 ContentHandler passed as a request attribute with name "org.xml.sax.ContentHandler" and return null. startDocument and endDocument must not be invoked on the ContentHandler, use ContentHandlerStartEndDocumentFilter if this is a problem.

Parameters:
pageParams - map for page parameters
Returns:
name of the page to view, or null to not use any page, or the name of an other service to forward to prefixed by "*"
Throws:
java.lang.Exception - may throw any Exception