org.apache.catalina.servlets
Class DefaultServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended byorg.apache.catalina.servlets.DefaultServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
WebdavServlet

public class DefaultServlet
extends javax.servlet.http.HttpServlet

The default resource-serving servlet for most web applications, used to serve static resources such as HTML pages and images.

Version:
$Id: DefaultServlet.java 1158244 2011-08-16 12:50:33Z markt $
Author:
Craig R. McClanahan, Remy Maucherat
See Also:
Serialized Form

Nested Class Summary
protected  class DefaultServlet.Range
           
 
Field Summary
protected static int BUFFER_SIZE
          Size of file transfer buffer in bytes.
protected  int debug
          The debugging detail level for this servlet.
protected  java.lang.String fileEncoding
          File encoding to be used when reading static files.
protected static java.util.ArrayList FULL
          Full range marker.
protected  java.lang.String globalXsltFile
          Allow customized directory listing per instance.
protected  int input
          The input buffer size to use when serving resources.
protected  boolean listings
          Should we generate directory listings?
protected  java.lang.String localXsltFile
          Allow customized directory listing per directory.
protected static java.lang.String mimeSeparation
          MIME multipart separation string
protected  int output
          The output buffer size to use when serving resources.
protected  java.lang.String readmeFile
          Allow a readme file to be included.
protected  boolean readOnly
          Read only flag.
protected  ProxyDirContext resources
          Proxy directory context.
protected static java.lang.String RESOURCES_JNDI_NAME
          JNDI resources name.
protected  int sendfileSize
          Minimum size for sendfile usage in bytes.
protected static StringManager sm
          The string manager for this package.
protected static URLEncoder urlEncoder
          Array containing the safe characters set.
protected  boolean useAcceptRanges
          Should the Accept-Ranges: bytes header be send with static resources?
 
Constructor Summary
DefaultServlet()
           
 
Method Summary
protected  boolean checkIfHeaders(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
          Check if the conditions specified in the optional If headers are satisfied.
protected  boolean checkIfMatch(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
          Check if the if-match condition is satisfied.
protected  boolean checkIfModifiedSince(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
          Check if the if-modified-since condition is satisfied.
protected  boolean checkIfNoneMatch(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
          Check if the if-none-match condition is satisfied.
protected  boolean checkIfUnmodifiedSince(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
          Check if the if-unmodified-since condition is satisfied.
protected  boolean checkSendfile(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, CacheEntry entry, long length, DefaultServlet.Range range)
          Check if sendfile can be used.
protected  void copy(CacheEntry cacheEntry, java.io.InputStream is, java.io.PrintWriter writer)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  void copy(CacheEntry cacheEntry, java.io.InputStream is, javax.servlet.ServletOutputStream ostream)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  void copy(CacheEntry cacheEntry, java.io.PrintWriter writer, DefaultServlet.Range range)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  void copy(CacheEntry cacheEntry, java.io.PrintWriter writer, java.util.Iterator ranges, java.lang.String contentType)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  void copy(CacheEntry cacheEntry, javax.servlet.ServletOutputStream ostream, DefaultServlet.Range range)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  void copy(CacheEntry cacheEntry, javax.servlet.ServletOutputStream ostream, java.util.Iterator ranges, java.lang.String contentType)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  java.io.IOException copyRange(java.io.InputStream istream, javax.servlet.ServletOutputStream ostream)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  java.io.IOException copyRange(java.io.InputStream istream, javax.servlet.ServletOutputStream ostream, long start, long end)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  java.io.IOException copyRange(java.io.Reader reader, java.io.PrintWriter writer)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
protected  java.io.IOException copyRange(java.io.Reader reader, java.io.PrintWriter writer, long start, long end)
          Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).
 void destroy()
          Finalize this servlet.
protected  void displaySize(java.lang.StringBuffer buf, int filesize)
          Display the size of a file.
protected  void doDelete(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Process a POST request for the specified resource.
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Process a GET request for the specified resource.
protected  void doHead(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Process a HEAD request for the specified resource.
protected  void doOptions(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Override default implementation to ensure that TRACE is correctly handled.
protected  void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Process a POST request for the specified resource.
protected  void doPut(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Process a POST request for the specified resource.
protected  java.io.File executePartialPut(javax.servlet.http.HttpServletRequest req, DefaultServlet.Range range, java.lang.String path)
          Handle a partial PUT.
protected  java.io.InputStream findXsltInputStream(javax.naming.directory.DirContext directory)
          Return the xsl template inputstream (if possible)
protected  java.lang.String getETag(ResourceAttributes resourceAttributes)
          Get the ETag associated with a file.
protected  java.lang.String getReadme(javax.naming.directory.DirContext directory)
          Get the readme file as a string.
protected  java.lang.String getRelativePath(javax.servlet.http.HttpServletRequest request)
          Return the relative path associated with this servlet.
 void init()
          Initialize this servlet.
protected  DefaultServlet.Range parseContentRange(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Parse the content-range header.
protected  java.util.ArrayList parseRange(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, ResourceAttributes resourceAttributes)
          Parse the range header.
protected  java.io.InputStream render(java.lang.String contextPath, CacheEntry cacheEntry)
          Decide which way to render.
protected  java.io.InputStream renderHtml(java.lang.String contextPath, CacheEntry cacheEntry)
          Return an InputStream to an HTML representation of the contents of this directory.
protected  java.lang.String renderSize(long size)
          Render the specified file size (in bytes).
protected  java.io.InputStream renderXml(java.lang.String contextPath, CacheEntry cacheEntry, java.io.InputStream xsltInputStream)
          Return an InputStream to an HTML representation of the contents of this directory.
protected  java.lang.String rewriteUrl(java.lang.String path)
          URL rewriter.
protected  void serveResource(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, boolean content)
          Serve the specified resource, optionally including the data content.
 
Methods inherited from class javax.servlet.http.HttpServlet
doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
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

debug

protected int debug
The debugging detail level for this servlet.


input

protected int input
The input buffer size to use when serving resources.


listings

protected boolean listings
Should we generate directory listings?


readOnly

protected boolean readOnly
Read only flag. By default, it's set to true.


output

protected int output
The output buffer size to use when serving resources.


urlEncoder

protected static URLEncoder urlEncoder
Array containing the safe characters set.


localXsltFile

protected java.lang.String localXsltFile
Allow customized directory listing per directory.


globalXsltFile

protected java.lang.String globalXsltFile
Allow customized directory listing per instance.


readmeFile

protected java.lang.String readmeFile
Allow a readme file to be included.


resources

protected ProxyDirContext resources
Proxy directory context.


fileEncoding

protected java.lang.String fileEncoding
File encoding to be used when reading static files. If none is specified the platform default is used.


sendfileSize

protected int sendfileSize
Minimum size for sendfile usage in bytes.


useAcceptRanges

protected boolean useAcceptRanges
Should the Accept-Ranges: bytes header be send with static resources?


FULL

protected static java.util.ArrayList FULL
Full range marker.


mimeSeparation

protected static final java.lang.String mimeSeparation
MIME multipart separation string

See Also:
Constant Field Values

RESOURCES_JNDI_NAME

protected static final java.lang.String RESOURCES_JNDI_NAME
JNDI resources name.

See Also:
Constant Field Values

sm

protected static StringManager sm
The string manager for this package.


BUFFER_SIZE

protected static final int BUFFER_SIZE
Size of file transfer buffer in bytes.

See Also:
Constant Field Values
Constructor Detail

DefaultServlet

public DefaultServlet()
Method Detail

destroy

public void destroy()
Finalize this servlet.


init

public void init()
          throws javax.servlet.ServletException
Initialize this servlet.

Throws:
javax.servlet.ServletException

getRelativePath

protected java.lang.String getRelativePath(javax.servlet.http.HttpServletRequest request)
Return the relative path associated with this servlet.

Parameters:
request - The servlet request we are processing

doGet

protected void doGet(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws java.io.IOException,
                     javax.servlet.ServletException
Process a GET request for the specified resource.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
Throws:
java.io.IOException - if an input/output error occurs
javax.servlet.ServletException - if a servlet-specified error occurs

doHead

protected void doHead(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws java.io.IOException,
                      javax.servlet.ServletException
Process a HEAD request for the specified resource.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
Throws:
java.io.IOException - if an input/output error occurs
javax.servlet.ServletException - if a servlet-specified error occurs

doOptions

protected void doOptions(javax.servlet.http.HttpServletRequest req,
                         javax.servlet.http.HttpServletResponse resp)
                  throws javax.servlet.ServletException,
                         java.io.IOException
Override default implementation to ensure that TRACE is correctly handled.

Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet
resp - the HttpServletResponse object that contains the response the servlet returns to the client
Throws:
java.io.IOException - if an input or output error occurs while the servlet is handling the OPTIONS request
javax.servlet.ServletException - if the request for the OPTIONS cannot be handled

doPost

protected void doPost(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws java.io.IOException,
                      javax.servlet.ServletException
Process a POST request for the specified resource.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
Throws:
java.io.IOException - if an input/output error occurs
javax.servlet.ServletException - if a servlet-specified error occurs

doPut

protected void doPut(javax.servlet.http.HttpServletRequest req,
                     javax.servlet.http.HttpServletResponse resp)
              throws javax.servlet.ServletException,
                     java.io.IOException
Process a POST request for the specified resource.

Parameters:
req - The servlet request we are processing
resp - The servlet response we are creating
Throws:
java.io.IOException - if an input/output error occurs
javax.servlet.ServletException - if a servlet-specified error occurs

executePartialPut

protected java.io.File executePartialPut(javax.servlet.http.HttpServletRequest req,
                                         DefaultServlet.Range range,
                                         java.lang.String path)
                                  throws java.io.IOException
Handle a partial PUT. New content specified in request is appended to existing content in oldRevisionContent (if present). This code does not support simultaneous partial updates to the same resource.

Throws:
java.io.IOException

doDelete

protected void doDelete(javax.servlet.http.HttpServletRequest req,
                        javax.servlet.http.HttpServletResponse resp)
                 throws javax.servlet.ServletException,
                        java.io.IOException
Process a POST request for the specified resource.

Parameters:
req - The servlet request we are processing
resp - The servlet response we are creating
Throws:
java.io.IOException - if an input/output error occurs
javax.servlet.ServletException - if a servlet-specified error occurs

checkIfHeaders

protected boolean checkIfHeaders(javax.servlet.http.HttpServletRequest request,
                                 javax.servlet.http.HttpServletResponse response,
                                 ResourceAttributes resourceAttributes)
                          throws java.io.IOException
Check if the conditions specified in the optional If headers are satisfied.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
resourceAttributes - The resource information
Returns:
boolean true if the resource meets all the specified conditions, and false if any of the conditions is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

getETag

protected java.lang.String getETag(ResourceAttributes resourceAttributes)
Get the ETag associated with a file.

Parameters:
resourceAttributes - The resource information

rewriteUrl

protected java.lang.String rewriteUrl(java.lang.String path)
URL rewriter.

Parameters:
path - Path which has to be rewiten

displaySize

protected void displaySize(java.lang.StringBuffer buf,
                           int filesize)
Display the size of a file.


serveResource

protected void serveResource(javax.servlet.http.HttpServletRequest request,
                             javax.servlet.http.HttpServletResponse response,
                             boolean content)
                      throws java.io.IOException,
                             javax.servlet.ServletException
Serve the specified resource, optionally including the data content.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
content - Should the content be included?
Throws:
java.io.IOException - if an input/output error occurs
javax.servlet.ServletException - if a servlet-specified error occurs

parseContentRange

protected DefaultServlet.Range parseContentRange(javax.servlet.http.HttpServletRequest request,
                                                 javax.servlet.http.HttpServletResponse response)
                                          throws java.io.IOException
Parse the content-range header.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
Returns:
Range
Throws:
java.io.IOException

parseRange

protected java.util.ArrayList parseRange(javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response,
                                         ResourceAttributes resourceAttributes)
                                  throws java.io.IOException
Parse the range header.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
Returns:
Vector of ranges
Throws:
java.io.IOException

render

protected java.io.InputStream render(java.lang.String contextPath,
                                     CacheEntry cacheEntry)
Decide which way to render. HTML or XML.


renderXml

protected java.io.InputStream renderXml(java.lang.String contextPath,
                                        CacheEntry cacheEntry,
                                        java.io.InputStream xsltInputStream)
Return an InputStream to an HTML representation of the contents of this directory.

Parameters:
contextPath - Context path to which our internal paths are relative

renderHtml

protected java.io.InputStream renderHtml(java.lang.String contextPath,
                                         CacheEntry cacheEntry)
Return an InputStream to an HTML representation of the contents of this directory.

Parameters:
contextPath - Context path to which our internal paths are relative

renderSize

protected java.lang.String renderSize(long size)
Render the specified file size (in bytes).

Parameters:
size - File size (in bytes)

getReadme

protected java.lang.String getReadme(javax.naming.directory.DirContext directory)
Get the readme file as a string.


findXsltInputStream

protected java.io.InputStream findXsltInputStream(javax.naming.directory.DirContext directory)
Return the xsl template inputstream (if possible)


checkSendfile

protected boolean checkSendfile(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response,
                                CacheEntry entry,
                                long length,
                                DefaultServlet.Range range)
Check if sendfile can be used.


checkIfMatch

protected boolean checkIfMatch(javax.servlet.http.HttpServletRequest request,
                               javax.servlet.http.HttpServletResponse response,
                               ResourceAttributes resourceAttributes)
                        throws java.io.IOException
Check if the if-match condition is satisfied.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
resourceAttributes - The resource information
Returns:
boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

checkIfModifiedSince

protected boolean checkIfModifiedSince(javax.servlet.http.HttpServletRequest request,
                                       javax.servlet.http.HttpServletResponse response,
                                       ResourceAttributes resourceAttributes)
                                throws java.io.IOException
Check if the if-modified-since condition is satisfied.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
resourceAttributes - The resource information
Returns:
boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

checkIfNoneMatch

protected boolean checkIfNoneMatch(javax.servlet.http.HttpServletRequest request,
                                   javax.servlet.http.HttpServletResponse response,
                                   ResourceAttributes resourceAttributes)
                            throws java.io.IOException
Check if the if-none-match condition is satisfied.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
resourceAttributes - The resource information
Returns:
boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

checkIfUnmodifiedSince

protected boolean checkIfUnmodifiedSince(javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response,
                                         ResourceAttributes resourceAttributes)
                                  throws java.io.IOException
Check if the if-unmodified-since condition is satisfied.

Parameters:
request - The servlet request we are processing
response - The servlet response we are creating
resourceAttributes - The resource information
Returns:
boolean true if the resource meets the specified condition, and false if the condition is not satisfied, in which case request processing is stopped
Throws:
java.io.IOException

copy

protected void copy(CacheEntry cacheEntry,
                    java.io.InputStream is,
                    javax.servlet.ServletOutputStream ostream)
             throws java.io.IOException
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
cacheEntry - The resource information
ostream - The output stream to write to
Throws:
java.io.IOException - if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry,
                    java.io.InputStream is,
                    java.io.PrintWriter writer)
             throws java.io.IOException
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
cacheEntry - The resource information
writer - The writer to write to
Throws:
java.io.IOException - if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry,
                    javax.servlet.ServletOutputStream ostream,
                    DefaultServlet.Range range)
             throws java.io.IOException
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
cacheEntry - The resource information
ostream - The output stream to write to
range - Range the client wanted to retrieve
Throws:
java.io.IOException - if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry,
                    java.io.PrintWriter writer,
                    DefaultServlet.Range range)
             throws java.io.IOException
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
cacheEntry - The resource information
writer - The writer to write to
range - Range the client wanted to retrieve
Throws:
java.io.IOException - if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry,
                    javax.servlet.ServletOutputStream ostream,
                    java.util.Iterator ranges,
                    java.lang.String contentType)
             throws java.io.IOException
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
cacheEntry - The resource information
ostream - The output stream to write to
ranges - Enumeration of the ranges the client wanted to retrieve
contentType - Content type of the resource
Throws:
java.io.IOException - if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry,
                    java.io.PrintWriter writer,
                    java.util.Iterator ranges,
                    java.lang.String contentType)
             throws java.io.IOException
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
cacheEntry - The resource information
writer - The writer to write to
ranges - Enumeration of the ranges the client wanted to retrieve
contentType - Content type of the resource
Throws:
java.io.IOException - if an input/output error occurs

copyRange

protected java.io.IOException copyRange(java.io.InputStream istream,
                                        javax.servlet.ServletOutputStream ostream)
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
istream - The input stream to read from
ostream - The output stream to write to
Returns:
Exception which occurred during processing

copyRange

protected java.io.IOException copyRange(java.io.Reader reader,
                                        java.io.PrintWriter writer)
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
reader - The reader to read from
writer - The writer to write to
Returns:
Exception which occurred during processing

copyRange

protected java.io.IOException copyRange(java.io.InputStream istream,
                                        javax.servlet.ServletOutputStream ostream,
                                        long start,
                                        long end)
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
istream - The input stream to read from
ostream - The output stream to write to
start - Start of the range which will be copied
end - End of the range which will be copied
Returns:
Exception which occurred during processing

copyRange

protected java.io.IOException copyRange(java.io.Reader reader,
                                        java.io.PrintWriter writer,
                                        long start,
                                        long end)
Copy the contents of the specified input stream to the specified output stream, and ensure that both streams are closed before returning (even in the face of an exception).

Parameters:
reader - The reader to read from
writer - The writer to write to
start - Start of the range which will be copied
end - End of the range which will be copied
Returns:
Exception which occurred during processing


Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.