View Javadoc

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