Class DownloadServlet
- All Implemented Interfaces:
jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable
This Servlet provides the ability to download information from the
Server to the client. It provides the ability to set the content type
of the downloaded file, if not specified, it will attempt to guess
based on the extension (if possible). It requires the
DownloadServlet#ContentSource of the data to download to be
specified by passing in a ServletRequest parameter named
CONTENT_SOURCE_ID. The
DownloadServlet.ContentSource provides a plugable means
of obtaining data from an arbitrary source (i.e. the filesystem,
generated on the fly, from some network location, etc.). The available
DownloadServlet.ContentSource implemenatations must be
specified via a Servlet init parameter named
CONTENT_SOURCES.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceImplement this interface to provide an Object that is capable of providing data toDownloadServlet.static classThis class provides information about the request that may be necessary for theDownloadServlet.ContentSourceto provide content. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThis is the ServletRequest Parameter that should be provided to identify theDownloadServlet.ContentSourceimplementation that should be used.static final StringThis String ("ContentSources") is the name if the Servlet Init Parameter that should be used to register all availableDownloadServlet#ContentSourceimplementations.static final StringThe Content-type ("ContentType").static final StringThe Default Content-type ("application/octet-stream").static final StringThis String ("downloadContext") is the name if the ServletRequest Attribute used to store theDownloadServlet#Contextobject for this request.static final StringThis is theDownloadServlet#Contextattribute name used to specify the filename extension of the content.static final StringThis is theDownloadServlet#Contextattribute name used to specify optional additional headers.Fields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddoGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method delegates to the#doPost()method.voiddoPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method is the main method for this class when used in anHttpServletenvironment.protected DownloadServlet.ContentSourcegetContentSource(jakarta.servlet.ServletRequest request) This method locates the appropriateDownloadServlet#ContentSourcefor this request.This method looks up a DownloadServlet.ContentSource given its id.protected DownloadServlet.ContextgetDownloadContext(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method instantiates aDownloadServlet.Contextand initializes it with the Servlet, ServletConfig, ServletRequest, and ServletResponse.protected longgetLastModified(jakarta.servlet.http.HttpServletRequest request) This method gets called before the doGet/doPost method.voidinit(jakarta.servlet.ServletConfig config) Servlet initialization method.voidThis method registers the given class name as aDownloadServlet#ContentSource.voidregisterContentSource(String className) This method registers the given class name as aDownloadServlet#ContentSource.protected voidwriteContent(DownloadServlet.ContentSource source, DownloadServlet.Context context) This method is responsible for copying the data from the givenInputStreamto theServletResponse'sOutputStream.protected voidwriteHeader(DownloadServlet.ContentSource source, DownloadServlet.Context context) This method is responsible for setting the response header information.Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, service, serviceMethods inherited from class jakarta.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
DOWNLOAD_CONTEXT
This String ("downloadContext") is the name if the ServletRequest Attribute used to store the
DownloadServlet#Contextobject for this request.- See Also:
-
CONTENT_SOURCES
This String ("ContentSources") is the name if the Servlet Init Parameter that should be used to register all available
DownloadServlet#ContentSourceimplementations.- See Also:
-
CONTENT_SOURCE_ID
This is the ServletRequest Parameter that should be provided to identify the
DownloadServlet.ContentSourceimplementation that should be used. This value must match the value returned by theDownloadServlet.ContentSourceimplementation'sgetId()method.- See Also:
-
CONTENT_TYPE
The Content-type ("ContentType"). This is the
DownloadServlet#Contextattribute used to specify an explicit "Content-type". It may be set by theDownloadServlet#ContentSource, or may be passed in via a request parameter. If not specified, theEXTENSIONwill be used. If that fails, theDEFAULT_CONTENT_TYPEwill apply.- See Also:
-
DEFAULT_CONTENT_TYPE
The Default Content-type ("application/octet-stream").
- See Also:
-
EXTENSION
This is the
DownloadServlet#Contextattribute name used to specify the filename extension of the content. It is the responsibility of theDownloadServlet#ContentSourceto set this value. The value should represent the filename extension of the content if it were saved to a filesystem.- See Also:
-
HEADERS
This is the
DownloadServlet#Contextattribute name used to specify optional additional headers. It must be set toMap<String, String> object when needed. - See Also:
-
-
Constructor Details
-
DownloadServlet
public DownloadServlet()Default Constructor.
-
-
Method Details
-
init
public void init(jakarta.servlet.ServletConfig config) throws jakarta.servlet.ServletException Servlet initialization method.
- Specified by:
initin interfacejakarta.servlet.Servlet- Overrides:
initin classjakarta.servlet.http.HttpServlet- Throws:
jakarta.servlet.ServletException
-
registerContentSource
This method registers the given class name as a
DownloadServlet#ContentSource. This method will attempt to resolve and instantiate the class using the current classloader. -
registerContentSource
This method registers the given class name as a
DownloadServlet#ContentSource. This method will attempt to instantiate the class via the default constructor. -
getContentSource
This method looks up a DownloadServlet.ContentSource given its id. The
DownloadServlet#ContentSourcemust be previously registered. -
doGet
public void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException This method delegates to the
#doPost()method.- Overrides:
doGetin classjakarta.servlet.http.HttpServlet- Throws:
jakarta.servlet.ServletExceptionIOException
-
doPost
public void doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException This method is the main method for this class when used in an
HttpServletenvironment. It drives the process, which includes creating aDownloadServet#Context, choosing the appropriateDownloadServlet#ContentSource, and copying the output of theDownloadServlet#ContentSourceto theServletResponse'sOutputStream.- Overrides:
doPostin classjakarta.servlet.http.HttpServlet- Throws:
jakarta.servlet.ServletExceptionIOException
-
getDownloadContext
protected DownloadServlet.Context getDownloadContext(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method instantiates a
DownloadServlet.Contextand initializes it with the Servlet, ServletConfig, ServletRequest, and ServletResponse. -
getContentSource
This method locates the appropriate
DownloadServlet#ContentSourcefor this request. It uses the givenServletRequestto look for a ServletRequest Parameter namedCONTENT_SOURCE_ID. This value is used as the key when looking up registeredDownloadServlet#ContentSourceimplementations. -
writeHeader
protected void writeHeader(DownloadServlet.ContentSource source, DownloadServlet.Context context) throws IOException This method is responsible for setting the response header information.
- Throws:
IOException
-
writeContent
This method is responsible for copying the data from the given
InputStreamto theServletResponse'sOutputStream. TheInputStreamshould be the from theDownloadServlet#ContentSource. -
getLastModified
protected long getLastModified(jakarta.servlet.http.HttpServletRequest request) This method gets called before the doGet/doPost method. The requires us to create the
DownloadServlet#Contexthere. However, we do not have theHttpServletResponseyet, so it will be null.- Overrides:
getLastModifiedin classjakarta.servlet.http.HttpServlet
-