java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
org.klomp.snark.web.BasicServlet
- All Implemented Interfaces:
Serializable, Servlet, ServletConfig
- Direct Known Subclasses:
I2PSnarkServlet
Based on DefaultServlet from Jetty 6.1.26, heavily simplified and modified to remove all dependencies on Jetty libs.
Supports HEAD and GET only, for resources from the .war and local files. Supports files and resource only. Supports MIME types with local overrides and additions. Supports Last-Modified. Supports single request ranges.
Does not support directories or "welcome files". Does not support gzip. Does not support multiple request ranges. Does not cache.
POST returns 405. Directories return 403. Jar resources are sent with a long cache directive.
------------------------------------------------------------
The default servlet. This servlet, normally mapped to /, provides the handling for static content, OPTION and TRACE methods for the context. The following initParameters are supported, these can be set on the servlet itself:
resourceBase Set to replace the context resource base warBase Path allowed for resource in war
Nested Class Summary
Modifier and TypeClassDescriptionprivate classHTTP content backed by a file on disk.static interfaceFrom Jetty HttpContent.java.private classHTTP content backed by an entry in the war file.private classFile content with byte limit.Field Summary
FieldsModifier and TypeFieldDescriptionprivate ByteCacheCache supplying the file transfer buffers.protected final I2PAppContextThe global application context, transient so the servlet stays serializable.protected final LogLog for this servlet's class, transient so the servlet stays serializable.private final MimeTypesResolver of the served files' mime types.protected FileBase directory for serving static files.private StringFilesystem path of the unpacked war content.private static final intSame as PeerState.PARTSIZE.private static final intBrowser cache time of the torrent files, in seconds.private static final longThe serialization version of the servlet.private static final intBrowser cache time of war resources, in seconds.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprotected voidaddMimeMapping(String extension, String type) Add a MIME type mapping for an extension.protected static StringSimple version ofURIUtil.addPaths().private voidcopy(InputStream in, long skip, OutputStream out, long len) Copy bytes from input stream to output stream.private voidcopy(InputStream in, OutputStream out) Write from in to out.protected static StringdecodePath(String path) Simple version ofURIUtil.decodePath().protected voiddoDelete(HttpServletRequest request, HttpServletResponse response) Handles HTTP DELETE requests.protected voiddoGet(HttpServletRequest request, HttpServletResponse response) protected voiddoOptions(HttpServletRequest request, HttpServletResponse response) Handles HTTP OPTIONS requests.protected voiddoPost(HttpServletRequest request, HttpServletResponse response) Handles HTTP POST requests.protected voiddoTrace(HttpServletRequest request, HttpServletResponse response) Handles HTTP TRACE requests.protected static StringencodePath(String path) Simple version ofURIUtil.encodePath(String).getContent(String pathInContext, long limit) Resource to serve, mapping a path to a resource.protected StringgetMimeType(String filename) The MIME type by filename extension.getResource(String pathInContext) Resource to serve, mapping a path to a resource.voidinit(ServletConfig cfg) Initializes the servlet with resource and war base directories.protected voidloadMimeMap(String resourcePath) Load MIME type mappings from a resource file.protected booleanpassConditionalHeaders(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) Check modification date headers.protected voidsendData(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) Sends the content to the client, handling range requests.protected voidsetResourceBase(File base) Files are served from here.protected voidsetWarBase(String base) Only paths starting with this in the path are served.protected voidwriteHeaders(HttpServletResponse response, BasicServlet.HttpContent content, long count) Write response headers for the content.Methods inherited from class HttpServlet
doHead, doPut, getLastModified, service, serviceMethods inherited from class GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
Field Details
serialVersionUID
private static final long serialVersionUIDThe serialization version of the servlet.- See Also:
_context
The global application context, transient so the servlet stays serializable._log
Log for this servlet's class, transient so the servlet stays serializable._resourceBase
Base directory for serving static files. Effectively immutable after startup; volatile so request threads never lock to read it._warBase
Filesystem path of the unpacked war content._mimeTypes
Resolver of the served files' mime types.BUFSIZE
_cache
Cache supplying the file transfer buffers.WAR_CACHE_CONTROL_SECS
private static final int WAR_CACHE_CONTROL_SECSBrowser cache time of war resources, in seconds.- See Also:
FILE_CACHE_CONTROL_SECS
private static final int FILE_CACHE_CONTROL_SECSBrowser cache time of the torrent files, in seconds.- See Also:
Constructor Details
BasicServlet
public BasicServlet()Bind to the global context and build a fresh MIME table.
Method Details
init
Initializes the servlet with resource and war base directories.- Specified by:
initin interfaceServlet- Overrides:
initin classGenericServlet- Parameters:
cfg- the servlet configuration- Throws:
ServletException- if initialization fails
setResourceBase
Files are served from here.- Parameters:
base- the directory to serve files from- Throws:
UnavailableException- if the base directory doesn't exist
setWarBase
Only paths starting with this in the path are served.- Parameters:
base- the base path that must be matched
getResource
Resource to serve, mapping a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.- Parameters:
pathInContext- The path to find a resource for.- Returns:
- The resource to serve or null if not existing
getContent
Resource to serve, mapping a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.- Parameters:
pathInContext- The path to find a resource for.limit- the maximum content length to serve, or 0 for unlimited- Returns:
- The resource to serve or null. Returns null for directories
doGet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException - Overrides:
doGetin classHttpServlet- Throws:
ServletExceptionIOException
doPost
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException Handles HTTP POST requests. Returns 405 Method Not Allowed.- Overrides:
doPostin classHttpServlet- Parameters:
request- the HTTP requestresponse- the HTTP response- Throws:
ServletException- if a servlet error occursIOException- if an I/O error occurs
doTrace
protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException Handles HTTP TRACE requests. Returns 405 Method Not Allowed.- Overrides:
doTracein classHttpServlet- Parameters:
request- the HTTP requestresponse- the HTTP response- Throws:
ServletException- if a servlet error occursIOException- if an I/O error occurs
doOptions
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException Handles HTTP OPTIONS requests. Returns 405 Method Not Allowed.- Overrides:
doOptionsin classHttpServlet- Parameters:
request- the HTTP requestresponse- the HTTP response- Throws:
ServletException- if a servlet error occursIOException- if an I/O error occurs
doDelete
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException Handles HTTP DELETE requests. Returns 405 Method Not Allowed.- Overrides:
doDeletein classHttpServlet- Parameters:
request- the HTTP requestresponse- the HTTP response- Throws:
ServletException- if a servlet error occursIOException- if an I/O error occurs
passConditionalHeaders
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) throws IOException Check modification date headers.- Parameters:
request- the HTTP requestresponse- the HTTP responsecontent- the body to be served, queried for type, length and modification time- Returns:
- true to keep going, false if handled here
- Throws:
IOException- if an I/O error occurs
sendData
protected void sendData(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) throws IOException Sends the content to the client, handling range requests.- Parameters:
request- the HTTP requestresponse- the HTTP responsecontent- the body to be served, whose stream the caller must not reuse- Throws:
IOException- if an I/O error occurs
writeHeaders
protected void writeHeaders(HttpServletResponse response, BasicServlet.HttpContent content, long count) throws IOException Write response headers for the content.- Parameters:
response- the HTTP responsecontent- the body being served, consulted for type and cache lifetimecount- the content length- Throws:
IOException- if an I/O error occurs
loadMimeMap
Load MIME type mappings from a resource file.- Parameters:
resourcePath- the resource path in the classpath, without ".properties" extension
getMimeType
addMimeMapping
addPaths
decodePath
Simple version ofURIUtil.decodePath().- Parameters:
path- the path to decode- Returns:
- the decoded path
- Throws:
MalformedURLException- if the path contains invalid escape sequences
encodePath
Simple version ofURIUtil.encodePath(String).- Parameters:
path- the path to encode- Returns:
- the encoded path
copy
Write from in to out.- Parameters:
in- the input stream to read fromout- the output stream to write to- Throws:
IOException- if an I/O error occurs
copy
Copy bytes from input stream to output stream.- Parameters:
in- the input stream to read fromskip- the number of bytes to skip before copyingout- the output stream to write tolen- the number of bytes to copy, or -1 for all remaining bytes- Throws:
IOException- if an I/O error occurs