org.clazzes.util.http
Class ResourceServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.clazzes.util.http.ResourceServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class ResourceServlet
extends javax.servlet.http.HttpServlet

A servlet, which exports parts of the ServletContext's resources as HTTP resources with configuration options on which items should be delivered in compressed form.

This servlet may be configured through initialization parameters inside a classical servlet container like in the following web.xml example:

 <servlet>
  <servlet-name>foobar-resources</servlet-name>
  <servlet-class>org.clazzes.util.http.ResourceServlet</servlet-class>
  <init-param>
   <param-name>resourcePath</param-name>
   <param-value>/foobar</param-value>
  </init-param>
  <init-param>
   <param-name>excludeMimeType</param-name>
   <param-value>image/png,image/jpeg,image/gif</param-value>
  </init-param>
  <init-param>
   <param-name>aliases</param-name>
   <param-value>/=/index.html,/index.htm=/index.html</param-value>
  </init-param>
  <init-param>
   <param-name>redirects</param-name>
   <param-value>/app1=appServlet,/app2=/ctx2/appServlet</param-value>
  </init-param>
 </servlet>
 

Additional, seldom used initialization parameters are minCompressionSize and bufferSize, which are actually integer values.

In OSGi frameworks it is recommended to set up this servlet using blueprint or a similar technology using the supplied bean property setters like in the following example:

  <bean id="resourceServlet" class="org.clazzes.util.http.ResourceServlet">
    <property name="resourcePath" value="/foobar-resources"></property>
    <property name="excludeMimeTypes">
      <set>
        <value>image/png</value>
        <value>image/gif</value>
        <value>image/jpeg</value>
        <value>image/tiff</value>
      </set>
    </property>
    <property name="aliases">
      <map>
        <entry key="/"          value="/index.html"/>       
        <entry key="/index.htm" value="/index.html"/>       
      </map>
    </property>
    <property name="redirects">
      <map>
        <entry key="/app1" value="appServlet"/>       
        <entry key="/app2" value="/ctx2/appServlet"/>       
      </map>
    </property>
  </bean>
 

See Also:
Serialized Form

Constructor Summary
ResourceServlet()
           
 
Method Summary
 void addAlias(String pathInfo, String alias)
          Add an alias for a given path info.
 void addExcludeMimeType(String excludeMimeType)
          Add an element to the list of MIME types to be excluded from compression.
 void addRedirect(String pathInfo, String url)
          Add a path-to-URL redirect.
protected  void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
           
 Map<String,String> getAliases()
           
 int getBufferSize()
           
 Set<String> getExcludeMimeTypes()
           
 int getMinCompressionSize()
           
 Map<String,String> getRedirects()
           
 String getResourcePath()
           
 String getServletInfo()
           
 void init()
           
 void setAliases(Map<String,String> aliases)
           
 void setBufferSize(int bufferSize)
           
 void setExcludeMimeTypes(Set<String> excludeMimeTypes)
           
 void setMinCompressionSize(int minCompressionSize)
           
 void setRedirects(Map<String,String> redirects)
          The keys of this map are path infos like returned by HttpServletRequest.getPathInfo(), which start with a '/' sign.
 void setResourcePath(String resourcePath)
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceServlet

public ResourceServlet()
Method Detail

doGet

protected void doGet(javax.servlet.http.HttpServletRequest req,
                     javax.servlet.http.HttpServletResponse resp)
              throws javax.servlet.ServletException,
                     IOException
Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException

getBufferSize

public int getBufferSize()
Returns:
the internal buffer size.

setBufferSize

public void setBufferSize(int bufferSize)
Parameters:
bufferSize - the internal buffer size to set. The default value is 4096 and should be suitable for almost any purpose.

getResourcePath

public String getResourcePath()
Returns:
the resource path, which is exported from the given ClassLoader. If set to null, only redirections are available.

setResourcePath

public void setResourcePath(String resourcePath)
Parameters:
resourcePath - the resource path, which is exported from the given ClassLoader to set. All request exports resources acquired through ServletContext.getResourceAsStream(String) using this resource path appended by HttpServletRequest.getPathInfo().

getMinCompressionSize

public int getMinCompressionSize()
Returns:
the the minimal size of objects to be compressed.

setMinCompressionSize

public void setMinCompressionSize(int minCompressionSize)
Parameters:
minCompressionSize - the minimal size of objects to be compressed to set.

getExcludeMimeTypes

public Set<String> getExcludeMimeTypes()
Returns:
The list of MIME types to be excluded from compression.

setExcludeMimeTypes

public void setExcludeMimeTypes(Set<String> excludeMimeTypes)
Parameters:
excludeMimeTypes - The list of MIME types to be excluded from compression to set. Most image types are already compressed, so should consider to include their MIME types in this list.

addExcludeMimeType

public void addExcludeMimeType(String excludeMimeType)
Add an element to the list of MIME types to be excluded from compression.

Parameters:
excludeMimeType - The MIME type to add.

getServletInfo

public String getServletInfo()
Specified by:
getServletInfo in interface javax.servlet.Servlet
Overrides:
getServletInfo in class javax.servlet.GenericServlet

init

public void init()
          throws javax.servlet.ServletException
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException

getRedirects

public Map<String,String> getRedirects()
Returns:
The mapping of requests path infos to redirect URLs.

setRedirects

public void setRedirects(Map<String,String> redirects)

The keys of this map are path infos like returned by HttpServletRequest.getPathInfo(), which start with a '/' sign. The values are relative or absolute URLs, which are passed to HttpServletResponse.sendRedirect(String).

The key may be optionally postfixed by a list of locales in the format /localized-url?locales=de,en meaning that any request for /localized-url with an appropriate Accept-Language HTTP header will be redirected to the target with an URL paremeter named locale with a value of the selected domain as per LocaleHelper.selectLocale(String, String[]) using the comma-separated list of server-side locales de or en.

Parameters:
redirects - The mapping of requests path infos to redirect URLs to set.

addRedirect

public void addRedirect(String pathInfo,
                        String url)
Add a path-to-URL redirect.

Parameters:
pathInfo - A path info as returned by HttpServletRequest.getPathInfo().
url - A redirect URL passed to HttpServletResponse.sendRedirect(String).

getAliases

public Map<String,String> getAliases()
Returns:
the resource aliases.

setAliases

public void setAliases(Map<String,String> aliases)
Parameters:
aliases - the resource aliases to set. The keys of this map are path infos like returned by HttpServletRequest.getPathInfo(), which start with a '/' sign. The values are translated path infos and should therefore start with a '/' sign, too.

addAlias

public void addAlias(String pathInfo,
                     String alias)
Add an alias for a given path info.

Parameters:
pathInfo - A path info as returned by HttpServletRequest.getPathInfo().
alias - An alias path info, which should start with a '/' sign.


Copyright © 2011. All Rights Reserved.