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.HttpServletThis 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#ContentSourceof the data to download to be specified by passing in aServletRequestparameter namedCONTENT_SOURCE_ID. TheDownloadServlet.ContentSourceprovides 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.ContentSourceimplemenatations must be specified via aServletinit parameter namedCONTENT_SOURCES.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDownloadServlet.ContentSourceImplement this interface to provide an Object that is capable of providing data toDownloadServlet.static classDownloadServlet.ContextThis class provides information about the request that may be necessary for theDownloadServlet.ContentSourceto provide content.
-
Field Summary
Fields Modifier and Type Field Description static StringCONTENT_SOURCE_IDThis is the ServletRequest Parameter that should be provided to identify theDownloadServlet.ContentSourceimplementation that should be used.static StringCONTENT_SOURCESThis String ("ContentSources") is the name if the Servlet Init Parameter that should be used to register all availableDownloadServlet#ContentSourceimplementations.static StringCONTENT_TYPEThe Content-type ("ContentType").static StringDEFAULT_CONTENT_TYPEThe Default Content-type ("application/octet-stream").static StringDOWNLOAD_CONTEXTThis String ("downloadContext") is the name if the ServletRequest Attribute used to store theDownloadServlet#Contextobject for this request.static StringEXTENSIONThis is theDownloadServlet#Contextattribute name used to specify the filename extension of the content.static StringHEADERSThis is theDownloadServlet#Contextattribute 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 voiddoGet(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.DownloadServlet.ContentSourcegetContentSource(String id)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.voidregisterContentSource(Class cls)This 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, 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#Contextobject 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#ContentSourceimplementations.- 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.ContentSourceimplementation that should be used. This value must match the value returned by theDownloadServlet.ContentSourceimplementation'sgetId()method.- See Also:
- Constant Field Values
-
CONTENT_TYPE
public static final String 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:
- 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#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:
- Constant Field Values
-
HEADERS
public static final String HEADERS
This is the
DownloadServlet#Contextattribute 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.ServletExceptionServlet initialization method.
- Specified by:
initin interfacejakarta.servlet.Servlet- Overrides:
initin 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#ContentSourcemust be previously registered.
-
doGet
public void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOExceptionThis 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, IOExceptionThis 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
protected DownloadServlet.ContentSource getContentSource(jakarta.servlet.ServletRequest request)
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
protected void writeContent(DownloadServlet.ContentSource source, DownloadServlet.Context context)
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
-
-