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