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      private ResourceLocation resourceLocation;
18  
19      public ForwardableResource(ResourceLocation resourceLocation) {
20          this.resourceLocation = resourceLocation;
21      }
22  
23      public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response) {
24          return true;
25      }
26  
27      public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException {
28          try {
29              String type = getContentType();
30              if (StringUtils.isNotBlank(type)) {
31                  response.setContentType(type); // this will be used if content-type is not set by the forward handler, e.g. for webapp content in Tomcat
32              }
33              request.getRequestDispatcher(getLocation()).forward(request, response);
34          } catch (ServletException e) {
35              throw new DownloadException(e.getMessage());
36          } catch (IOException ioe) {
37              throw new DownloadException(ioe.getMessage());
38          }
39      }
40  
41      /**
42       * Not implemented by a <code>ForwardableResource</code>. The supplied OutputStream will not be modified.
43       */
44      public void streamResource(OutputStream out) {
45          return;
46      }
47  
48      public String getContentType() {
49          return resourceLocation.getContentType();
50      }
51  
52      protected String getLocation() {
53          return resourceLocation.getLocation();
54      }
55  
56      @Override
57      public String toString() {
58          return "Forwardable Resource: " + resourceLocation.getLocation();
59      }
60  }
61