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