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   
10  /**
11   * A DownloadableResource that simply forwards the request to the given location.
12   * This should be used to reference dynamic resources available in the web application e.g dwr js files
13   */
14  public class ForwardableResource implements DownloadableResource
15  {
16      private ResourceLocation resourceLocation;
17  
18      public ForwardableResource(ResourceLocation resourceLocation)
19      {
20          this.resourceLocation = resourceLocation;
21      }
22  
23      public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
24      {
25          return false;
26      }
27  
28      public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
29      {
30          try
31          {
32              response.setContentType(getContentType()); // this will be used if content-type is not set by the forward handler, e.g. for webapp content in Tomcat
33              request.getRequestDispatcher(getLocation()).forward(request, response);
34          }
35          catch(ServletException e)
36          {
37              throw new DownloadException(e.getMessage());
38          }
39          catch (IOException ioe)
40          {
41              throw new DownloadException(ioe.getMessage());
42          }
43      }
44  
45      public String getContentType()
46      {
47          return resourceLocation.getContentType();
48      }
49  
50      protected String getLocation()
51      {
52          return resourceLocation.getLocation();
53      }
54  }
55