Class DownloadServlet
- java.lang.Object
-
- jakarta.servlet.GenericServlet
-
- jakarta.servlet.http.HttpServlet
-
- org.glassfish.admingui.common.servlet.DownloadServlet
-
- All Implemented Interfaces:
jakarta.servlet.Servlet
,jakarta.servlet.ServletConfig
,Serializable
public class DownloadServlet extends jakarta.servlet.http.HttpServlet
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 aServletRequest
parameter namedCONTENT_SOURCE_ID
. TheDownloadServlet.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 availableDownloadServlet.ContentSource
implemenatations must be specified via aServlet
init parameter namedCONTENT_SOURCES
.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DownloadServlet.ContentSource
Implement this interface to provide an Object that is capable of providing data toDownloadServlet
.static class
DownloadServlet.Context
This class provides information about the request that may be necessary for theDownloadServlet.ContentSource
to provide content.
-
Field Summary
Fields Modifier and Type Field Description static String
CONTENT_SOURCE_ID
This is the ServletRequest Parameter that should be provided to identify theDownloadServlet.ContentSource
implementation that should be used.static String
CONTENT_SOURCES
This String ("ContentSources") is the name if the Servlet Init Parameter that should be used to register all availableDownloadServlet#ContentSource
implementations.static String
CONTENT_TYPE
The Content-type ("ContentType").static String
DEFAULT_CONTENT_TYPE
The Default Content-type ("application/octet-stream").static String
DOWNLOAD_CONTEXT
This String ("downloadContext") is the name if the ServletRequest Attribute used to store theDownloadServlet#Context
object for this request.static String
EXTENSION
This is theDownloadServlet#Context
attribute name used to specify the filename extension of the content.static String
HEADERS
This is theDownloadServlet#Context
attribute name used to specify optional additional headers.
-
Constructor Summary
Constructors Constructor Description DownloadServlet()
Default Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
This method delegates to the#doPost()
method.void
doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
This method is the main method for this class when used in anHttpServlet
environment.protected DownloadServlet.ContentSource
getContentSource(jakarta.servlet.ServletRequest request)
This method locates the appropriateDownloadServlet#ContentSource
for this request.DownloadServlet.ContentSource
getContentSource(String id)
This method looks up a DownloadServlet.ContentSource given its id.protected DownloadServlet.Context
getDownloadContext(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
This method instantiates aDownloadServlet.Context
and initializes it with the Servlet, ServletConfig, ServletRequest, and ServletResponse.protected long
getLastModified(jakarta.servlet.http.HttpServletRequest request)
This method gets called before the doGet/doPost method.void
init(jakarta.servlet.ServletConfig config)
Servlet initialization method.void
registerContentSource(Class cls)
This method registers the given class name as aDownloadServlet#ContentSource
.void
registerContentSource(String className)
This method registers the given class name as aDownloadServlet#ContentSource
.protected void
writeContent(DownloadServlet.ContentSource source, DownloadServlet.Context context)
This method is responsible for copying the data from the givenInputStream
to theServletResponse
'sOutputStream
.protected void
writeHeader(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, service
-
-
-
-
Field Detail
-
DOWNLOAD_CONTEXT
public static final String DOWNLOAD_CONTEXT
This String ("downloadContext") is the name if the ServletRequest Attribute used to store the
DownloadServlet#Context
object for this request.- See Also:
- Constant Field Values
-
CONTENT_SOURCES
public static final String CONTENT_SOURCES
This String ("ContentSources") is the name if the Servlet Init Parameter that should be used to register all available
DownloadServlet#ContentSource
implementations.- See Also:
- Constant Field Values
-
CONTENT_SOURCE_ID
public static final String CONTENT_SOURCE_ID
This is the ServletRequest Parameter that should be provided to identify the
DownloadServlet.ContentSource
implementation that should be used. This value must match the value returned by theDownloadServlet.ContentSource
implementation'sgetId()
method.- See Also:
- Constant Field Values
-
CONTENT_TYPE
public static final String CONTENT_TYPE
The Content-type ("ContentType"). This is the
DownloadServlet#Context
attribute 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, theEXTENSION
will be used. If that fails, theDEFAULT_CONTENT_TYPE
will apply.- See Also:
- Constant Field Values
-
DEFAULT_CONTENT_TYPE
public static final String DEFAULT_CONTENT_TYPE
The Default Content-type ("application/octet-stream").
- See Also:
- Constant Field Values
-
EXTENSION
public static final String EXTENSION
This is the
DownloadServlet#Context
attribute name used to specify the filename extension of the content. It is the responsibility of theDownloadServlet#ContentSource
to set this value. The value should represent the filename extension of the content if it were saved to a filesystem.- See Also:
- Constant Field Values
-
HEADERS
public static final String HEADERS
This is the
DownloadServlet#Context
attribute name used to specify optional additional headers. It must be set toMap object when needed. - See Also:
- Constant Field Values
-
-
Method Detail
-
init
public void init(jakarta.servlet.ServletConfig config) throws jakarta.servlet.ServletException
Servlet initialization method.
- Specified by:
init
in interfacejakarta.servlet.Servlet
- Overrides:
init
in classjakarta.servlet.http.HttpServlet
- Throws:
jakarta.servlet.ServletException
-
registerContentSource
public void registerContentSource(String className)
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
public void registerContentSource(Class cls)
This method registers the given class name as a
DownloadServlet#ContentSource
. This method will attempt to instantiate the class via the default constructor.
-
getContentSource
public DownloadServlet.ContentSource getContentSource(String id)
This method looks up a DownloadServlet.ContentSource given its id. The
DownloadServlet#ContentSource
must 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:
doGet
in classjakarta.servlet.http.HttpServlet
- Throws:
jakarta.servlet.ServletException
IOException
-
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
HttpServlet
environment. It drives the process, which includes creating aDownloadServet#Context
, choosing the appropriateDownloadServlet#ContentSource
, and copying the output of theDownloadServlet#ContentSource
to theServletResponse
'sOutputStream
.- Overrides:
doPost
in classjakarta.servlet.http.HttpServlet
- Throws:
jakarta.servlet.ServletException
IOException
-
getDownloadContext
protected DownloadServlet.Context getDownloadContext(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
This method instantiates a
DownloadServlet.Context
and initializes it with the Servlet, ServletConfig, ServletRequest, and ServletResponse.
-
getContentSource
protected DownloadServlet.ContentSource getContentSource(jakarta.servlet.ServletRequest request)
This method locates the appropriate
DownloadServlet#ContentSource
for this request. It uses the givenServletRequest
to look for a ServletRequest Parameter namedCONTENT_SOURCE_ID
. This value is used as the key when looking up registeredDownloadServlet#ContentSource
implementations.
-
writeHeader
protected void writeHeader(DownloadServlet.ContentSource source, DownloadServlet.Context context)
This method is responsible for setting the response header information.
-
writeContent
protected void writeContent(DownloadServlet.ContentSource source, DownloadServlet.Context context)
This method is responsible for copying the data from the given
InputStream
to theServletResponse
'sOutputStream
. TheInputStream
should 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#Context
here. However, we do not have theHttpServletResponse
yet, so it will be null.- Overrides:
getLastModified
in classjakarta.servlet.http.HttpServlet
-
-