Package org.klomp.snark.web
Class BasicServlet
- 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
class BasicServlet extends HttpServlet
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
- Since:
- Jetty 7
- Author:
- Greg Wilkins (gregw), Nigel Canonizado
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceBasicServlet.HttpContentfrom Jetty HttpContent.java
-
Field Summary
Fields Modifier and Type Field Description protected I2PAppContext_contextprotected Log_logprotected File_resourceBase
-
Constructor Summary
Constructors Constructor Description BasicServlet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddMimeMapping(String extension, String type)protected static StringaddPaths(String base, String path)Simple version of URIUtil.addPaths()protected static StringdecodePath(String path)Simple version of URIUtil.decodePath()protected voiddoDelete(HttpServletRequest request, HttpServletResponse response)protected voiddoGet(HttpServletRequest request, HttpServletResponse response)protected voiddoOptions(HttpServletRequest request, HttpServletResponse response)protected voiddoPost(HttpServletRequest request, HttpServletResponse response)protected voiddoTrace(HttpServletRequest request, HttpServletResponse response)protected static StringencodePath(String path)Simple version of URIUtil.encodePath()BasicServlet.HttpContentgetContent(String pathInContext, long limit)get Resource to serve.protected StringgetMimeType(String filename)Get the MIME type by filename extension.FilegetResource(String pathInContext)get Resource to serve.voidinit(ServletConfig cfg)protected voidloadMimeMap(String resourcePath)protected booleanpassConditionalHeaders(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content)Check modification date headers.protected voidsendData(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content)protected voidsetResourceBase(File base)Files are served from hereprotected voidsetWarBase(String base)Only paths starting with this in the path are servedprotected voidwriteHeaders(HttpServletResponse response, BasicServlet.HttpContent content, long count)-
Methods inherited from class javax.servlet.http.HttpServlet
doHead, doPut, getLastModified, service, service
-
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
-
-
-
Field Detail
-
_context
protected final transient I2PAppContext _context
-
_log
protected final transient Log _log
-
_resourceBase
protected File _resourceBase
-
-
Method Detail
-
init
public void init(ServletConfig cfg) throws ServletException
- Specified by:
initin interfaceServlet- Overrides:
initin classGenericServlet- Throws:
ServletException
-
setResourceBase
protected void setResourceBase(File base) throws UnavailableException
Files are served from here- Throws:
UnavailableException
-
setWarBase
protected void setWarBase(String base)
Only paths starting with this in the path are served
-
getResource
public File getResource(String pathInContext)
get Resource to serve. Map 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
public BasicServlet.HttpContent getContent(String pathInContext, long limit)
get Resource to serve. Map 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. 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
- Overrides:
doPostin classHttpServlet- Throws:
ServletExceptionIOException
-
doTrace
protected void doTrace(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- Overrides:
doTracein classHttpServlet- Throws:
ServletExceptionIOException
-
doOptions
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- Overrides:
doOptionsin classHttpServlet- Throws:
ServletExceptionIOException
-
doDelete
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- Overrides:
doDeletein classHttpServlet- Throws:
ServletExceptionIOException
-
passConditionalHeaders
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) throws IOException
Check modification date headers.- Returns:
- true to keep going, false if handled here
- Throws:
IOException
-
sendData
protected void sendData(HttpServletRequest request, HttpServletResponse response, BasicServlet.HttpContent content) throws IOException
- Throws:
IOException
-
writeHeaders
protected void writeHeaders(HttpServletResponse response, BasicServlet.HttpContent content, long count) throws IOException
- Throws:
IOException
-
loadMimeMap
protected void loadMimeMap(String resourcePath)
- Parameters:
resourcePath- in the classpath, without ".properties" extension
-
getMimeType
protected String getMimeType(String filename)
Get the MIME type by filename extension.- Parameters:
filename- A file name- Returns:
- MIME type matching the longest dot extension of the file name.
-
addPaths
protected static String addPaths(String base, String path)
Simple version of URIUtil.addPaths()- Parameters:
path- may be null
-
decodePath
protected static String decodePath(String path) throws MalformedURLException
Simple version of URIUtil.decodePath()- Throws:
MalformedURLException
-
-