View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import org.apache.commons.io.IOUtils;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  
14  public class ResourceDownloadUtils
15  {
16      private static final Logger log = LoggerFactory.getLogger(ResourceDownloadUtils.class);
17      private static final long ONE_YEAR_SECONDS = 60L * 60L * 24L * 365L;
18      private static final long ONE_YEAR_MILLISECONDS = 1000 * ONE_YEAR_SECONDS;
19  
20      /**
21       * @deprecated Since 2.0. Use {@link IOUtils#copy(InputStream, OutputStream)} instead. The method calling
22       * this should be responsible for closing streams and flushing if necessary.
23       */
24      @Deprecated
25      public static void serveFileImpl(final HttpServletResponse httpServletResponse, final InputStream in) throws IOException
26      {
27          final OutputStream out = httpServletResponse.getOutputStream();
28          try
29          {
30              IOUtils.copy(in, out);
31          }
32          finally
33          {
34              IOUtils.closeQuietly(in);
35              out.flush();
36          }
37          log.debug("Serving file done.");
38      }
39  
40      /**
41       * Set 'expire' headers to cache for one year. Also adds the additional cache control values passed in.
42       * Note, this method resets the cache control headers if set previously. 
43       */
44      public static void addCachingHeaders(HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final String... cacheControls)
45      {
46          boolean cacheDisabledByQueryParam = "false".equals(httpServletRequest.getParameter("cache"));
47          if (!Boolean.getBoolean("atlassian.disable.caches") && !cacheDisabledByQueryParam)
48          {
49              httpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + ONE_YEAR_MILLISECONDS);
50              httpServletResponse.setHeader("Cache-Control", "max-age=" + ONE_YEAR_SECONDS);
51              for (final String cacheControl : cacheControls)
52              {
53                  httpServletResponse.addHeader("Cache-Control", cacheControl);
54              }
55          }
56      }
57  
58      /**
59       * Set 'expire' headers to cache for one year, with public caching turned on.
60       *
61       * @deprecated Please use {@link #addPublicCachingHeaders(HttpServletRequest, HttpServletResponse)} or
62       * {@link #addPrivateCachingHeaders(HttpServletRequest, HttpServletResponse)} instead.
63       */
64      @Deprecated
65      public static void addCachingHeaders(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
66      {
67          addPublicCachingHeaders(httpServletRequest, httpServletResponse);
68      }
69  
70      /**
71       * Sets caching headers with public cache control. Applications should call this method from urlrewrite.xml to
72       * decorate urls like <code>/s/{build num}/.../_/resourceurl</code>.
73       *
74       * @see <a href="http://tuckey.org/urlrewrite/manual/2.6/">http://tuckey.org/urlrewrite/manual/2.6/</a>
75       */
76      public static void addPublicCachingHeaders(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
77      {
78          addCachingHeaders(httpServletRequest, httpServletResponse, "public");
79      }
80  
81      /**
82       * Sets caching headers with private cache control. Applications should call this method from urlrewrite.xml to
83       * decorate urls like <code>/sp/{build num}/.../_/resourceurl</code>.
84       *
85       * @see <a href="http://tuckey.org/urlrewrite/manual/2.6/">http://tuckey.org/urlrewrite/manual/2.6/</a>
86       */
87      public static void addPrivateCachingHeaders(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
88      {
89          addCachingHeaders(httpServletRequest, httpServletResponse, "private");
90      }
91  }