org.vfny.geoserver.servlets
Class AbstractService

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended byorg.vfny.geoserver.servlets.AbstractService
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
WFService, WMService

public abstract class AbstractService
extends javax.servlet.http.HttpServlet

Represents a service that all others extend from. Subclasses should provide response and exception handlers as appropriate.

It is really important to adhere to the following workflow:

  1. get a Request reader
  2. ask the Request Reader for the Request object
  3. Provide the resulting Request with the ServletRequest that generated it
  4. get the appropiate ResponseHandler
  5. ask it to execute the Request
  6. set the response content type
  7. write to the http response's output stream
  8. pending - call Response cleanup

If anything goes wrong a ServiceException can be thrown and will be written to the output stream instead.

This is because we have to be sure that no exception have been produced before setting the response's content type, so we can set the exception specific content type; and that Response.getContentType is called AFTER Response.execute, since the MIME type can depend on any request parameter or another kind of desission making during the execute process. (i.e. FORMAT in WMS GetMap)

TODO: We need to call Response.abort() if anything goes wrong to allow the Response a chance to cleanup after itself.

See Also:
Serialized Form

Nested Class Summary
static interface AbstractService.ServiceStratagy
          Interface used for ServiceMode stratagy objects.
 
Field Summary
static java.util.Map serviceStratagys
          GR: if SPEED, FILE and BUFFER are static instances, so their methods should be synchronized, ending in a not multiuser server, so I made saftyMode dynamically instantiated in init() and the stratagy choosed at server config level in web.xml.
 
Constructor Summary
AbstractService()
           
 
Method Summary
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          DOCUMENT ME!
protected  void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          DOCUMENT ME!
protected  void doService(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Request serviceRequest)
          Peforms service according to ServiceStratagy.
protected abstract  ExceptionHandler getExceptionHandler()
          DOCUMENT ME!
protected abstract  KvpRequestReader getKvpReader(java.util.Map params)
          DOCUMENT ME!
protected  java.lang.String getMimeType()
          DOCUMENT ME!
protected abstract  Response getResponseHandler()
          DOCUMENT ME!
protected abstract  XmlRequestReader getXmlRequestReader()
          DOCUMENT ME!
 void init(javax.servlet.ServletConfig config)
          loads the "serviceStratagy" servlet context parameter and checks it if reffers to a valid ServiceStratagy (by now, one of SPEED, FILE or BUFFER); if no, just sets the stratagy to BUFFER as default
protected  boolean requestSupportsGzip(javax.servlet.http.HttpServletRequest request)
          Checks if the client requests supports gzipped responses by quering it's 'accept-encoding' header.
protected  void send(javax.servlet.http.HttpServletResponse response, java.lang.CharSequence content)
          DOCUMENT ME!
protected  void send(javax.servlet.http.HttpServletResponse response, Response result)
          DOCUMENT ME!
protected  void sendError(javax.servlet.http.HttpServletResponse response, ServiceException se)
          Send a serviceException produced during getService opperation.
protected  void sendError(javax.servlet.http.HttpServletResponse response, java.lang.Throwable t)
          Send error produced during getService opperation.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serviceStratagys

public static final java.util.Map serviceStratagys
GR: if SPEED, FILE and BUFFER are static instances, so their methods should be synchronized, ending in a not multiuser server, so I made saftyMode dynamically instantiated in init() and the stratagy choosed at server config level in web.xml. If I'm wrong, just tell me. If this is correct, may be it will be better to allow for user customized ServiceStratagy implementations to be parametrized by a servlet context param JG: You are exactly right! My-Bad, I was just trying to understand what chris was talking about.

Constructor Detail

AbstractService

public AbstractService()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
loads the "serviceStratagy" servlet context parameter and checks it if reffers to a valid ServiceStratagy (by now, one of SPEED, FILE or BUFFER); if no, just sets the stratagy to BUFFER as default

Parameters:
config - the servlet environment
Throws:
javax.servlet.ServletException - if the configured stratagy class is not a derivate of ServiceStratagy or it is thrown by the parent class

doGet

protected void doGet(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws javax.servlet.ServletException,
                     java.io.IOException
DOCUMENT ME!

Parameters:
request - DOCUMENT ME!
response - DOCUMENT ME!
Throws:
javax.servlet.ServletException - DOCUMENT ME!
java.io.IOException - DOCUMENT ME!

doPost

protected void doPost(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws javax.servlet.ServletException,
                      java.io.IOException
DOCUMENT ME!

Parameters:
request - DOCUMENT ME!
response - DOCUMENT ME!
Throws:
javax.servlet.ServletException - DOCUMENT ME!
java.io.IOException - DOCUMENT ME!

doService

protected void doService(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response,
                         Request serviceRequest)
                  throws javax.servlet.ServletException
Peforms service according to ServiceStratagy.

This method has very strict requirements, please see the class description for the specifics.

It has a lot of try/catch blocks, but they are fairly necessary to handle things correctly and to avoid as many ugly servlet responses, so that everything is wrapped correctly.

Parameters:
request -
response -
serviceRequest -
Throws:
javax.servlet.ServletException - if the stratagy can't be instantiated

getResponseHandler

protected abstract Response getResponseHandler()
DOCUMENT ME!

Returns:
DOCUMENT ME!

getKvpReader

protected abstract KvpRequestReader getKvpReader(java.util.Map params)
DOCUMENT ME!

Parameters:
params - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
ServiceException - if the set of key/value pairs of parameters defined by params can't be parsed to a valid RequestKvpReader

getXmlRequestReader

protected abstract XmlRequestReader getXmlRequestReader()
DOCUMENT ME!

Returns:
DOCUMENT ME!

getExceptionHandler

protected abstract ExceptionHandler getExceptionHandler()
DOCUMENT ME!

Returns:
DOCUMENT ME!

getMimeType

protected java.lang.String getMimeType()
DOCUMENT ME!

Returns:
DOCUMENT ME!

send

protected void send(javax.servlet.http.HttpServletResponse response,
                    java.lang.CharSequence content)
DOCUMENT ME!

Parameters:
response - DOCUMENT ME!
content - DOCUMENT ME!

sendError

protected void sendError(javax.servlet.http.HttpServletResponse response,
                         java.lang.Throwable t)
Send error produced during getService opperation.

Some errors know how to write themselves out WfsTransactionException for instance. It looks like this might be is handled by getExceptionHandler().newServiceException( t, pre, null ). I still would not mind seeing a check for ServiceConfig Exception here.

This code says that it deals with UNCAUGHT EXCEPTIONS, so I think it would be wise to explicitly catch ServiceExceptions.

Parameters:
response - DOCUMENT ME!
t - DOCUMENT ME!

sendError

protected void sendError(javax.servlet.http.HttpServletResponse response,
                         ServiceException se)
Send a serviceException produced during getService opperation.

Parameters:
response - DOCUMENT ME!
se - DOCUMENT ME!

send

protected void send(javax.servlet.http.HttpServletResponse response,
                    Response result)
DOCUMENT ME!

Parameters:
response - DOCUMENT ME!
result - DOCUMENT ME!

requestSupportsGzip

protected boolean requestSupportsGzip(javax.servlet.http.HttpServletRequest request)
Checks if the client requests supports gzipped responses by quering it's 'accept-encoding' header.

Parameters:
request - the request to query the HTTP header from
Returns:
true if 'gzip' if one of the supported content encodings of request, false otherwise.