org.apache.catalina.servlets

Class DefaultServlet

public class DefaultServlet extends HttpServlet

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

Version: $Revision: 762909 $ $Date: 2009-04-07 21:12:35 +0200 (Tue, 07 Apr 2009) $

Author: Craig R. McClanahan Remy Maucherat

Nested Class Summary
protected classDefaultServlet.Range
Field Summary
protected static intBUFFER_SIZE
Size of file transfer buffer in bytes.
protected intdebug
The debugging detail level for this servlet.
protected StringfileEncoding
File encoding to be used when reading static files.
protected static ArrayListFULL
Full range marker.
protected StringglobalXsltFile
Allow customized directory listing per instance.
protected intinput
The input buffer size to use when serving resources.
protected booleanlistings
Should we generate directory listings?
protected StringlocalXsltFile
Allow customized directory listing per directory.
protected static StringmimeSeparation
MIME multipart separation string
protected intoutput
The output buffer size to use when serving resources.
protected StringreadmeFile
Allow a readme file to be included.
protected booleanreadOnly
Read only flag.
protected ProxyDirContextresources
Proxy directory context.
protected static StringRESOURCES_JNDI_NAME
JNDI resources name.
protected intsendfileSize
Minimum size for sendfile usage in bytes.
protected static StringManagersm
The string manager for this package.
protected static URLEncoderurlEncoder
Array containing the safe characters set.
protected booleanuseAcceptRanges
Should the Accept-Ranges: bytes header be send with static resources?
Method Summary
protected booleancheckIfHeaders(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the conditions specified in the optional If headers are satisfied.
protected booleancheckIfMatch(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-match condition is satisfied.
protected booleancheckIfModifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-modified-since condition is satisfied.
protected booleancheckIfNoneMatch(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-none-match condition is satisfied.
protected booleancheckIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-unmodified-since condition is satisfied.
protected booleancheckSendfile(HttpServletRequest request, HttpServletResponse response, CacheEntry entry, long length, DefaultServlet.Range range)
Check if sendfile can be used.
protected voidcopy(CacheEntry cacheEntry, InputStream is, 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 voidcopy(CacheEntry cacheEntry, InputStream is, 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 voidcopy(CacheEntry cacheEntry, 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 voidcopy(CacheEntry cacheEntry, 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 voidcopy(CacheEntry cacheEntry, ServletOutputStream ostream, Iterator ranges, 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 voidcopy(CacheEntry cacheEntry, PrintWriter writer, Iterator ranges, 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 IOExceptioncopyRange(InputStream istream, 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 IOExceptioncopyRange(Reader reader, 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 IOExceptioncopyRange(InputStream istream, 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 IOExceptioncopyRange(Reader reader, 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).
voiddestroy()
Finalize this servlet.
protected voiddisplaySize(StringBuffer buf, int filesize)
Display the size of a file.
protected voiddoDelete(HttpServletRequest req, HttpServletResponse resp)
Process a POST request for the specified resource.
protected voiddoGet(HttpServletRequest request, HttpServletResponse response)
Process a GET request for the specified resource.
protected voiddoHead(HttpServletRequest request, HttpServletResponse response)
Process a HEAD request for the specified resource.
protected voiddoOptions(HttpServletRequest req, HttpServletResponse resp)
Override default implementation to ensure that TRACE is correctly handled.
protected voiddoPost(HttpServletRequest request, HttpServletResponse response)
Process a POST request for the specified resource.
protected voiddoPut(HttpServletRequest req, HttpServletResponse resp)
Process a POST request for the specified resource.
protected FileexecutePartialPut(HttpServletRequest req, DefaultServlet.Range range, String path)
Handle a partial PUT.
protected InputStreamfindXsltInputStream(DirContext directory)
Return the xsl template inputstream (if possible)
protected StringgetReadme(DirContext directory)
Get the readme file as a string.
protected StringgetRelativePath(HttpServletRequest request)
Return the relative path associated with this servlet.
voidinit()
Initialize this servlet.
protected DefaultServlet.RangeparseContentRange(HttpServletRequest request, HttpServletResponse response)
Parse the content-range header.
protected ArrayListparseRange(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Parse the range header.
protected InputStreamrender(String contextPath, CacheEntry cacheEntry)
Decide which way to render.
protected InputStreamrenderHtml(String contextPath, CacheEntry cacheEntry)
Return an InputStream to an HTML representation of the contents of this directory.
protected StringrenderSize(long size)
Render the specified file size (in bytes).
protected InputStreamrenderXml(String contextPath, CacheEntry cacheEntry, InputStream xsltInputStream)
Return an InputStream to an HTML representation of the contents of this directory.
protected StringrewriteUrl(String path)
URL rewriter.
protected voidserveResource(HttpServletRequest request, HttpServletResponse response, boolean content)
Serve the specified resource, optionally including the data content.

Field Detail

BUFFER_SIZE

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

debug

protected int debug
The debugging detail level for this servlet.

fileEncoding

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

FULL

protected static ArrayList FULL
Full range marker.

globalXsltFile

protected String globalXsltFile
Allow customized directory listing per instance.

input

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

listings

protected boolean listings
Should we generate directory listings?

localXsltFile

protected String localXsltFile
Allow customized directory listing per directory.

mimeSeparation

protected static final String mimeSeparation
MIME multipart separation string

output

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

readmeFile

protected String readmeFile
Allow a readme file to be included.

readOnly

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

resources

protected ProxyDirContext resources
Proxy directory context.

RESOURCES_JNDI_NAME

protected static final String RESOURCES_JNDI_NAME
JNDI resources name.

sendfileSize

protected int sendfileSize
Minimum size for sendfile usage in bytes.

sm

protected static StringManager sm
The string manager for this package.

urlEncoder

protected static URLEncoder urlEncoder
Array containing the safe characters set.

useAcceptRanges

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

Method Detail

checkIfHeaders

protected boolean checkIfHeaders(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
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

checkIfMatch

protected boolean checkIfMatch(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-match condition is satisfied.

Parameters: request The servlet request we are processing response The servlet response we are creating resourceInfo File object

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

checkIfModifiedSince

protected boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-modified-since condition is satisfied.

Parameters: request The servlet request we are processing response The servlet response we are creating resourceInfo File object

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

checkIfNoneMatch

protected boolean checkIfNoneMatch(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-none-match condition is satisfied.

Parameters: request The servlet request we are processing response The servlet response we are creating resourceInfo File object

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

checkIfUnmodifiedSince

protected boolean checkIfUnmodifiedSince(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Check if the if-unmodified-since condition is satisfied.

Parameters: request The servlet request we are processing response The servlet response we are creating resourceInfo File object

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

checkSendfile

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

copy

protected void copy(CacheEntry cacheEntry, InputStream is, 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: resourceInfo The resource information ostream The output stream to write to

Throws: IOException if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry, InputStream is, 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: resourceInfo The resource info writer The writer to write to

Throws: IOException if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry, 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).

Parameters: resourceInfo The ResourceInfo object ostream The output stream to write to range Range the client wanted to retrieve

Throws: IOException if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry, 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).

Parameters: resourceInfo The ResourceInfo object writer The writer to write to range Range the client wanted to retrieve

Throws: IOException if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry, ServletOutputStream ostream, Iterator ranges, 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).

Parameters: resourceInfo The ResourceInfo object ostream The output stream to write to ranges Enumeration of the ranges the client wanted to retrieve contentType Content type of the resource

Throws: IOException if an input/output error occurs

copy

protected void copy(CacheEntry cacheEntry, PrintWriter writer, Iterator ranges, 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).

Parameters: resourceInfo The ResourceInfo object writer The writer to write to ranges Enumeration of the ranges the client wanted to retrieve contentType Content type of the resource

Throws: IOException if an input/output error occurs

copyRange

protected IOException copyRange(InputStream istream, 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 IOException copyRange(Reader reader, 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 IOException copyRange(InputStream istream, 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 IOException copyRange(Reader reader, 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

destroy

public void destroy()
Finalize this servlet.

displaySize

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

doDelete

protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
Process a POST request for the specified resource.

Parameters: req The servlet request we are processing resp The servlet response we are creating

Throws: IOException if an input/output error occurs ServletException if a servlet-specified error occurs

doGet

protected void doGet(HttpServletRequest request, HttpServletResponse response)
Process a GET request for the specified resource.

Parameters: request The servlet request we are processing response The servlet response we are creating

Throws: IOException if an input/output error occurs ServletException if a servlet-specified error occurs

doHead

protected void doHead(HttpServletRequest request, HttpServletResponse response)
Process a HEAD request for the specified resource.

Parameters: request The servlet request we are processing response The servlet response we are creating

Throws: IOException if an input/output error occurs ServletException if a servlet-specified error occurs

doOptions

protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
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: IOException if an input or output error occurs while the servlet is handling the OPTIONS request ServletException if the request for the OPTIONS cannot be handled

doPost

protected void doPost(HttpServletRequest request, HttpServletResponse response)
Process a POST request for the specified resource.

Parameters: request The servlet request we are processing response The servlet response we are creating

Throws: IOException if an input/output error occurs ServletException if a servlet-specified error occurs

doPut

protected void doPut(HttpServletRequest req, HttpServletResponse resp)
Process a POST request for the specified resource.

Parameters: req The servlet request we are processing resp The servlet response we are creating

Throws: IOException if an input/output error occurs ServletException if a servlet-specified error occurs

executePartialPut

protected File executePartialPut(HttpServletRequest req, DefaultServlet.Range range, String path)
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.

findXsltInputStream

protected InputStream findXsltInputStream(DirContext directory)
Return the xsl template inputstream (if possible)

getReadme

protected String getReadme(DirContext directory)
Get the readme file as a string.

getRelativePath

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

Parameters: request The servlet request we are processing

init

public void init()
Initialize this servlet.

parseContentRange

protected DefaultServlet.Range parseContentRange(HttpServletRequest request, HttpServletResponse response)
Parse the content-range header.

Parameters: request The servlet request we are processing response The servlet response we are creating

Returns: Range

parseRange

protected ArrayList parseRange(HttpServletRequest request, HttpServletResponse response, ResourceAttributes resourceAttributes)
Parse the range header.

Parameters: request The servlet request we are processing response The servlet response we are creating

Returns: Vector of ranges

render

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

renderHtml

protected InputStream renderHtml(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 String renderSize(long size)
Render the specified file size (in bytes).

Parameters: size File size (in bytes)

renderXml

protected InputStream renderXml(String contextPath, CacheEntry cacheEntry, 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

rewriteUrl

protected String rewriteUrl(String path)
URL rewriter.

Parameters: path Path which has to be rewiten

serveResource

protected void serveResource(HttpServletRequest request, HttpServletResponse response, boolean content)
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: IOException if an input/output error occurs ServletException if a servlet-specified error occurs

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