Class MediaServlet

  • All Implemented Interfaces:
    Serializable, Servlet, ServletConfig

    public class MediaServlet
    extends HttpServlet
    A servlet providing access to the contents of MediaPath and its subclasses. In order to use it, you have to deploy the servlet in your web.xml, providing the name of the cope model via an init-parameter. Typically, your web.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 Detail

      • MediaServlet

        public MediaServlet()
    • Method Detail

      • initConnected

        public void initConnected​(Model model)
      • getMaximumAge

        protected Duration getMaximumAge​(MediaPath path,
                                         Item item)
        Let this method return the maximum age, that will be put into header Cache-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 request
        item - 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 header Cache-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 request
        item - 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 header Access-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 request
        item - the item of the current request
      • doFlushBufferOnNotModified

        protected boolean doFlushBufferOnNotModified​(MediaPath path,
                                                     Item item)
        Tomcat automatically adds Content-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 a Content-Length unequal to the original 200 response. The call to flushBuffer prevents Tomcat from adding a Content-Length header to the response. Of course, this is a hot fix. Remove it, if you find a better solution to avoid the Content-Length header.
        Parameters:
        path - the media path of the current request
        item - the item of the current request