View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.elements.ResourceLocation;
4   
5   import javax.servlet.ServletException;
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   import java.io.IOException;
9   import java.io.OutputStream;
10  
11  /**
12   * A DownloadableResource that simply forwards the request to the given location.
13   * This should be used to reference dynamic resources available in the web application e.g dwr js files
14   */
15  public class ForwardableResource implements DownloadableResource
16  {
17      private ResourceLocation resourceLocation;
18  
19      public ForwardableResource(ResourceLocation resourceLocation)
20      {
21          this.resourceLocation = resourceLocation;
22      }
23  
24      public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
25      {
26          return false;
27      }
28  
29      public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
30      {
31          try
32          {
33              response.setContentType(getContentType()); // this will be used if content-type is not set by the forward handler, e.g. for webapp content in Tomcat
34              request.getRequestDispatcher(getLocation()).forward(request, response);
35          }
36          catch(ServletException e)
37          {
38              throw new DownloadException(e.getMessage());
39          }
40          catch (IOException ioe)
41          {
42              throw new DownloadException(ioe.getMessage());
43          }
44      }
45  
46      /**
47       * Not implemented by a <code>ForwardableResource</code>. The supplied OutputStream will not be modified.
48       */
49      public void streamResource(OutputStream out)
50      {
51          return;
52      }
53  
54      public String getContentType()
55      {
56          return resourceLocation.getContentType();
57      }
58  
59      protected String getLocation()
60      {
61          return resourceLocation.getLocation();
62      }
63  }
64