View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import javax.servlet.http.HttpServletResponse;
7   import javax.servlet.http.HttpServletRequest;
8   import java.io.InputStream;
9   import java.io.IOException;
10  import java.io.OutputStream;
11  
12  public class ResourceDownloadUtils
13  {
14      private static final Log log = LogFactory.getLog(ResourceDownloadUtils.class);
15      private static final long TEN_YEARS = 1000L * 60L * 60L * 24L *365L * 10L;
16  
17      public static void serveFileImpl(HttpServletResponse httpServletResponse, InputStream in) throws IOException
18      {
19          OutputStream out = httpServletResponse.getOutputStream();
20  
21          try
22          {
23              byte[] buffer = new byte[16 * 1024];
24              int read_count;
25  
26              while ((read_count = in.read(buffer)) != -1)
27              {
28                  out.write(buffer, 0, read_count);
29              }
30  
31              log.info("Serving file done.");
32          }
33          catch (Exception e)
34          {
35              log.info("I/O Error serving the requested file: " + e.toString());
36          }
37          finally
38          {
39              if (in != null)
40              {
41                  try
42                  {
43                      in.close();
44                  } catch (IOException e)
45                  {
46                      // noop.
47                  }
48              }
49              if (out != null)
50              {
51                  try
52                  {
53                      out.flush();
54  
55                      //out.close();
56                  }
57                  catch (Exception e)
58                  {
59                      log.info("Error flushing output stream: " + e.toString());
60                  }
61              }
62          }
63      }
64  
65      /**
66       * Set 'expire' headers to cache for ten years.
67       */
68      public static void addCachingHeaders(HttpServletResponse httpServletResponse)
69      {
70          if (!Boolean.getBoolean("atlassian.disable.caches"))
71          {
72              httpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + TEN_YEARS);
73              httpServletResponse.setHeader("Cache-Control", "max-age=" + TEN_YEARS);
74              httpServletResponse.addHeader("Cache-Control", "private");
75          }
76      }
77  
78      /**
79       * Set 'expire' headers to cache for ten years.  This method is called from UrlRewriteFilter, and therefore needs
80       * to have 'request' and 'response' as parameters.
81       *
82       * @see <a href="http://tuckey.org/urlrewrite/manual/2.6/">http://tuckey.org/urlrewrite/manual/2.6/</a>
83       */
84      public static void addCachingHeaders(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
85      {
86          addCachingHeaders(httpServletResponse);
87      }
88  }