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       * If caches are disabled (via the system property atlassian.disable.caches), this method sets 'expires' to 0.
45       */
46      public static void addCachingHeaders(HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final String... cacheControls)
47      {
48          boolean cacheDisabledByQueryParam = "false".equals(httpServletRequest.getParameter("cache"));
49          if (Boolean.getBoolean("atlassian.disable.caches"))
50          {
51              httpServletResponse.setDateHeader("Expires", 0);
52              httpServletResponse.setHeader("Cache-Control", "no-cache, must-revalidate");
53          }
54          else if (!cacheDisabledByQueryParam)
55          {
56              httpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + ONE_YEAR_MILLISECONDS);
57              httpServletResponse.setHeader("Cache-Control", "max-age=" + ONE_YEAR_SECONDS);
58              for (final String cacheControl : cacheControls)
59              {
60                  httpServletResponse.addHeader("Cache-Control", cacheControl);
61              }
62          }
63      }
64  
65      /**
66       * Set 'expire' headers to cache for one year, with public caching turned on.
67       *
68       * @deprecated Please use {@link #addPublicCachingHeaders(HttpServletRequest, HttpServletResponse)} or
69       * {@link #addPrivateCachingHeaders(HttpServletRequest, HttpServletResponse)} instead.
70       */
71      @Deprecated
72      public static void addCachingHeaders(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
73      {
74          addPublicCachingHeaders(httpServletRequest, httpServletResponse);
75      }
76  
77      /**
78       * Sets caching headers with public cache control. Applications should call this method from urlrewrite.xml to
79       * decorate urls like <code>/s/{build num}/.../_/resourceurl</code>.
80       *
81       * @see <a href="http://tuckey.org/urlrewrite/manual/2.6/">http://tuckey.org/urlrewrite/manual/2.6/</a>
82       */
83      public static void addPublicCachingHeaders(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
84      {
85          addCachingHeaders(httpServletRequest, httpServletResponse, "public");
86      }
87  
88      /**
89       * Sets caching headers with private cache control. Applications should call this method from urlrewrite.xml to
90       * decorate urls like <code>/sp/{build num}/.../_/resourceurl</code>.
91       *
92       * @see <a href="http://tuckey.org/urlrewrite/manual/2.6/">http://tuckey.org/urlrewrite/manual/2.6/</a>
93       */
94      public static void addPrivateCachingHeaders(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
95      {
96          addCachingHeaders(httpServletRequest, httpServletResponse, "private");
97      }
98  }