View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import org.apache.commons.io.IOUtils;
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
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  {
15      private static final Log log = LogFactory.getLog(ResourceDownloadUtils.class);
16      private static final long TEN_YEARS = 1000L * 60L * 60L * 24L *365L * 10L;
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      public static void serveFileImpl(HttpServletResponse httpServletResponse, InputStream in) throws IOException
23      {
24          OutputStream out = httpServletResponse.getOutputStream();
25          try
26          {
27              IOUtils.copy(in, out);
28          }
29          catch (IOException e)
30          {
31              log.error("Error serving the requested file: ", e);
32          }
33          finally
34          {
35              IOUtils.closeQuietly(in);
36              try
37              {
38                  out.flush();
39              }
40              catch (IOException e)
41              {
42                  log.warn("Error flushing output stream: ", e);
43              }
44          }
45          log.debug("Serving file done.");
46      }
47  
48  
49      /**
50       * Set 'expire' headers to cache for ten years. Also adds the additional cache control values passed in.
51       * Note, this method resets the cache control headers if set previously. 
52       */
53      public static void addCachingHeaders(HttpServletResponse httpServletResponse, String... cacheControls)
54      {
55          if (!Boolean.getBoolean("atlassian.disable.caches"))
56          {
57              httpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + TEN_YEARS);
58              httpServletResponse.setHeader("Cache-Control", "max-age=" + TEN_YEARS);
59              for(String cacheControl : cacheControls)
60              {
61                  httpServletResponse.addHeader("Cache-Control", cacheControl);
62              }
63          }
64      }
65  
66      /**
67       * Set 'expire' headers to cache for ten years, with public caching turned on.
68       *
69       * @deprecated Please use {@link #addPublicCachingHeaders(HttpServletRequest, HttpServletResponse)} or
70       * {@link #addPrivateCachingHeaders(HttpServletRequest, HttpServletResponse)} instead.
71       */
72      public static void addCachingHeaders(HttpServletRequest httpServletRequest, 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(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
84      {
85          addCachingHeaders(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(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
95      {
96          addCachingHeaders(httpServletResponse, "private");
97      }
98  }