Package com.exedio.cope.pattern
Class MediaServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- com.exedio.cope.pattern.MediaServlet
-
- All Implemented Interfaces:
Serializable
,Servlet
,ServletConfig
public class MediaServlet extends HttpServlet
A servlet providing access to the contents ofMediaPath
and its subclasses. In order to use it, you have to deploy the servlet in yourweb.xml
, providing the name of the cope model via an init-parameter. Typically, yourweb.xml
would contain a snippet like this:<servlet> <servlet-name>media</servlet-name> <servlet-class>com.exedio.cope.pattern.MediaServlet</servlet-class> <init-param> <param-name>model</param-name> <param-value>
com.exedio.shop.Main#model
</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>media</servlet-name> <url-pattern>/media/*</url-pattern> </servlet-mapping>- Author:
- Ralf Wiebicke
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MediaServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy()
protected boolean
doFlushBufferOnNotModified(MediaPath path, Item item)
Tomcat automatically addsContent-Length: 0
to the response if the response body is smaller than the buffer size.protected void
doGet(HttpServletRequest request, HttpServletResponse response)
protected Duration
getMaximumAge(MediaPath path, Item item)
Let this method return the maximum age, that will be put into headerCache-Control: max-age=<seconds>
of the response.void
init()
void
initConnected(Model model)
protected boolean
isAccessControlAllowOriginWildcard(MediaPath path, Item item)
Let this method return true, if you want to add a headerAccess-Control-Allow-Origin: *
to the response.protected boolean
isCacheControlPrivate(MediaPath path, Item item)
Let this method return true, if you want to add a headerCache-Control: private
to the response.protected void
onException(HttpServletRequest request, Exception exception)
-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
-
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
-
-
-
Method Detail
-
init
public final void init() throws ServletException
- Overrides:
init
in classGenericServlet
- Throws:
ServletException
-
initConnected
public void initConnected(Model model)
-
destroy
public final void destroy()
- Specified by:
destroy
in interfaceServlet
- Overrides:
destroy
in classGenericServlet
-
doGet
protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
- Overrides:
doGet
in classHttpServlet
- Throws:
IOException
-
getMaximumAge
protected Duration getMaximumAge(MediaPath path, Item item)
Let this method return the maximum age, that will be put into headerCache-Control: max-age=<seconds>
of the response. If method returns null, no maximum age is sent. Negative values are treated like zero. Fractional seconds are ignored. This method is not called for @UrlFingerPrinting
pathes - such pathes do send a maximum age of 363 days.The default implementation returns
ConnectProperties.getMediaServletMaximumAge()
.See RFC 2616 Section 14.9.3 Modifications of the Basic Expiration Mechanism, https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist .
- Parameters:
path
- the media path of the current requestitem
- the item of the current request
-
isCacheControlPrivate
protected boolean isCacheControlPrivate(MediaPath path, Item item)
Let this method return true, if you want to add a headerCache-Control: private
to the response. This forbids shared caches, such as company proxies to cache such urls.The default implementation returns true iff
url guessing
is prevented.See RFC 2616 Section 14.9.1 What is Cacheable, https://httpd.apache.org/docs/2.2/mod/mod_cache.html#cachestoreprivate , and https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist .
- Parameters:
path
- the media path of the current requestitem
- the item of the current request
-
isAccessControlAllowOriginWildcard
protected boolean isAccessControlAllowOriginWildcard(MediaPath path, Item item)
Let this method return true, if you want to add a headerAccess-Control-Allow-Origin: *
to the response. This is typically needed for fonts served from a different domain. The default implementation returns false.- Parameters:
path
- the media path of the current requestitem
- the item of the current request
-
doFlushBufferOnNotModified
protected boolean doFlushBufferOnNotModified(MediaPath path, Item item)
Tomcat automatically addsContent-Length: 0
to the response if the response body is smaller than the buffer size. Unfortunately it does so for 304 responses as well. Apache 2.4 does not accept 304 responses with aContent-Length
unequal to the original 200 response. The call toflushBuffer
prevents Tomcat from adding aContent-Length
header to the response. Of course, this is a hot fix. Remove it, if you find a better solution to avoid theContent-Length
header.- Parameters:
path
- the media path of the current requestitem
- the item of the current request
-
onException
protected void onException(HttpServletRequest request, Exception exception)
-
-