javawebparts.servlet
Class ResourceServerServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--javawebparts.servlet.ResourceServerServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class ResourceServerServlet
extends javax.servlet.http.HttpServlet

The ResourceServerServlet can be used to serve resources from various sources. It uses an implementation of the ResourceStream interface, each of which is specific to a given source. There is an implementation for serving resources from a JAR file in the classpath, from the file system, from a URL and from a database. Multiple instance of this servlet can be used, under different names of course, each with its own set of init parameters.

This servlet recognizes the following init parameters:



Here is an example configuration:

<servlet>
  <servlet-name>ServeResourceFromFileSystem </servlet-name>
  <servlet-class>javawebparts.servlet.ResourceServerServlet </servlet-class>
  <init-param>
    <param-name>requestParameter </param-name>
    <param-value>fsres</param-value>
  </init-param>
  <init-param>
    <param-name>resourceStreamClass </param-name>
    <param-value>javawebparts.servlet. ResourceStreamFromFileSystem</param-value>
  </init-param>
  <init-param>
    <param-name>serveUnbounded</param-name >
    <param-value>false</param-value>
  </init-param>
  <init-param>
    <param-name>resourceMappingFile </param-name>
    <param-value>/WEB-INF/resource-mappings.xml </param-value>
  </init-param>
  <init-param>
    <param-name>defaultContentType </param-name>
    <param-value>dummy</param-value>
  </init-param>
</servlet>

You can write your own implementation of ResourceStream to serve resources from other sources. One logical implementation would be to serve from a database. In this case, you may decide that the request parameter will be giving a key in a table. You may also use the streamParams to name the connection string and userID/password. The getStream() method simply needs to return an InputStream on the BLOB from the database. It should return null if the resource cannot be found (in which case nothing will be returned by the servlet). Also, if applicable, implement the getContentType() method. In the case of serving from a database, one can envision having a field in the table that stores the content type. You can of course just use the defaultContentType, or a mapping file, whichever is most appropriate (it is likely such a database implementation will be supplied with JWP in the future).

Author:
Frank W. Zammetti.
See Also:
Serialized Form

Nested Class Summary
static class ResourceServerServlet.ResourceMapping
          ResourceMapping is an inner class which holds the information for each element in the mapping file, if present.
 
Field Summary
private  java.lang.String defaultContentType
          If the ResourceStream implementation class doesn't provide the contentType, and if no mapping file is used, then this default contentType will be set for each resource served.
private static org.apache.commons.logging.Log log
          Log instance.
private  java.util.HashMap mappings
          mappings is a collection of ResourceMapping objects (which correspond to elements in a mapping file) for each resource.
private  java.lang.String requestParameter
          requestParameter is the name of the request parameter that will be examined to determine what the requested resource is.
private  java.lang.String resourceMappingFile
          resourceMappingFile is the context-relative path to a file which maps requested resources to contentTypes.
private  java.lang.String resourceStreamClass
          resourceStreamClass is the class that implements the ResourceStream interface and knows how to provide a stream to a requested resource, and optionally the contentType for the resource.
private  boolean serveUnbounded
          serveUnbounded, when set to true, means that any resource that the servlet, and by extension the applicable ResourceStream class, can find.
private  java.lang.String streamParam1
          streamParam1 is an arbitrary piece of information passed to the ResourceStream implementation class.
private  java.lang.String streamParam2
          streamParam2 is an arbitrary piece of information passed to the ResourceStream implementation class.
 
Fields inherited from class javax.servlet.http.HttpServlet
 
Fields inherited from class javax.servlet.GenericServlet
 
Constructor Summary
ResourceServerServlet()
           
 
Method Summary
 void addMapping(ResourceServerServlet.ResourceMapping rm)
          Add a ResourceMapping to the mappings collection.
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          doGet.
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          doPost.
 void init(javax.servlet.ServletConfig config)
          init.
 java.lang.String toString()
          Overriden toString method.
 
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, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log
Log instance.


requestParameter

private java.lang.String requestParameter
requestParameter is the name of the request parameter that will be examined to determine what the requested resource is.


resourceStreamClass

private java.lang.String resourceStreamClass
resourceStreamClass is the class that implements the ResourceStream interface and knows how to provide a stream to a requested resource, and optionally the contentType for the resource.


resourceMappingFile

private java.lang.String resourceMappingFile
resourceMappingFile is the context-relative path to a file which maps requested resources to contentTypes. This file is optional in many cases, but not always, depending on how other init parameters are set.


serveUnbounded

private boolean serveUnbounded
serveUnbounded, when set to true, means that any resource that the servlet, and by extension the applicable ResourceStream class, can find. You need to be careful when setting this to true because it potentially opens up security holes (i.e., resources you don't intend might be reachable).


defaultContentType

private java.lang.String defaultContentType
If the ResourceStream implementation class doesn't provide the contentType, and if no mapping file is used, then this default contentType will be set for each resource served.


mappings

private java.util.HashMap mappings
mappings is a collection of ResourceMapping objects (which correspond to elements in a mapping file) for each resource.


streamParam1

private java.lang.String streamParam1
streamParam1 is an arbitrary piece of information passed to the ResourceStream implementation class.


streamParam2

private java.lang.String streamParam2
streamParam2 is an arbitrary piece of information passed to the ResourceStream implementation class.

Constructor Detail

ResourceServerServlet

public ResourceServerServlet()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
init.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Parameters:
config - ServletConfig.
Throws:
javax.servlet.ServletException - ServletException.

addMapping

public void addMapping(ResourceServerServlet.ResourceMapping rm)
Add a ResourceMapping to the mappings collection.

Parameters:
rm - The ResourceMapping object to add to the collection.

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
doGet. Calls doPost() to do real work.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest.
response - HTTPServletResponse.
Throws:
javax.servlet.ServletException - ServletException.
java.io.IOException - IOException.

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   java.io.IOException
doPost.

Overrides:
doPost in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest
response - HTTPServletResponse
Throws:
javax.servlet.ServletException - ServletException
java.io.IOException - IOException

toString

public java.lang.String toString()
Overriden toString method.

Overrides:
toString in class java.lang.Object
Returns:
A reflexively-built string representation of this bean.


Copyright 2005 Frank W. Zammetti