View Javadoc
1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.elements.ResourceLocation;
4   import org.apache.commons.lang3.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 | IOException e) {
35              throw new DownloadException(e.getMessage());
36          }
37      }
38  
39      /**
40       * Not implemented by a <code>ForwardableResource</code>. The supplied OutputStream will not be modified.
41       */
42      public void streamResource(OutputStream out) {
43          return;
44      }
45  
46      public String getContentType() {
47          return resourceLocation.getContentType();
48      }
49  
50      protected String getLocation() {
51          return resourceLocation.getLocation();
52      }
53  
54      @Override
55      public String toString() {
56          return "Forwardable Resource: " + resourceLocation.getLocation();
57      }
58  }
59