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:
- requestParameter - This is the request parameter that will have
the value used to determine the resource to serve. For instance, if you set
this to "res", and use the ResourceStreamFromURL ResourceStream
implementation, then the servlet expects to receive a request parameter
named "res" that is a URL, the contents from which will be returned.
Default: res. Required: No.
- resourceStreamClass - This is the fully-qualified name of the
class which implements the ResourceStream interface. This class is
responsible for returning to this servlet an InputStream to the resource to
be served. Default: None. Required: Yes.
- serveUnbounded - The value of this is either "true" or "false".
When set to true, it means that ANY requested resource that can be resolved
by the servlet (and the associated ResourceStream class) will be served.
In other words, if this servlet receives a request for a resource in a JAR
that it can find, it will be served, whether you intend for it to be
servable or not. When set to false, only resources listed in the named
mapping file will be served. Setting this to true can be a security
issue obviously, so you need to be very careful when doing so, and you
will probably want to use some sort of security mechanism in front of this
servlet in that case. Required: No. Default: false (not that in this case
the servlet may not initialize because a mapping file would be required,
which may not be present).
- resourceMappingFile - This is a context-relative full path to a
configuration file that does two things. First, it lists all the resources
that can be served, and no resource that is not listed can be served.
Second, it defines the content type for each resource. Required: no (unless
serveUnbound is false, in which case it IS required). Default: none.
- defaultContentType - This is simply the content type that will
be used if the content type is not specified otherwise. The ResourceStream
implementation class is first asked for the contentType. If it does not
supply it, the mapping file, if present, is checked, If it is still not
determined, then the default value here is used. Required: yes.
Default: none.
- streamParam1 - This is an arbitrary piece of data that will be
passed to the ResourceStream implementation class for every requested
resource. This can be whatever you want. Required: no. Default: none.
- streamParam2 - This is another arbitrary piece of data that will
be passed to the ResourceStream implementation class for every requested
resource. This can be whatever you want. Required: no. Default: none.
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
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 |
|
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 |
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.
ResourceServerServlet
public ResourceServerServlet()
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
- HTTPServletRequestresponse
- 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