View Javadoc

1   package mx.com.brained.actions;
2   
3   import javax.servlet.http.*;
4   
5   import mx.com.brained.resource.*;
6   import org.apache.struts.action.*;
7   import javax.servlet.ServletOutputStream;
8   
9   /***
10   *
11   * <p>Title: AwTaglib</p>
12   *
13   * <p>Description: Special action that handles reading and loading of special resources (usually js or png files) located in the
14   * classpath.</br>
15   * It requires two parameters for it to work properly:
16   * <ul>
17   * <li>type = containing the mime-mapping of the response</li>
18   * <li>file = containing the name of the file to be loaded and sent through the stream</li>
19   * </ul></p>
20   *
21   * <p>Copyright: Copyright (c) 2005</p>
22   *
23   * <p>Company: Brain-ED</p>
24   *
25   * @author Isidoro Treviņo
26   * @version 1.0
27   *
28   * @struts.action
29   *   type="org.springframework.web.struts.DelegatingActionProxy"
30   *   path="/resource"
31   *   validate="false"
32   *
33   * @spring.bean
34   *   name="/resource"
35   */
36  public class ResourceAction extends Action{
37      private ResourceManagerIface resource;
38  
39  
40      /***
41       * The main method that will be executed to send through the ServletOutputStream the contents of the file
42       * @param mapping ActionMapping
43       * @param form ActionForm
44       * @param request HttpServletRequest
45       * @param response HttpServletResponse
46       * @return ActionForward returns null because it directly writes a binary output
47       * @throws Exception if any error occurs (usually a file not found on classpath)
48       */
49      public ActionForward execute(ActionMapping mapping, ActionForm form,
50                                        HttpServletRequest request,
51                                        HttpServletResponse response) throws
52              Exception {
53          String type=request.getParameter("type");
54          String file=request.getParameter("file");
55          byte[] data = resource.getResource(file);
56          response.setContentType(type);
57          response.setContentLength(data.length);
58          ServletOutputStream ouputStream = response.getOutputStream();
59          ouputStream.write(data, 0, data.length);
60          ouputStream.flush();
61          ouputStream.close();
62          return null;
63      }
64  
65  
66      public ResourceAction() {
67  
68      }
69  
70      /***
71       * Method to set the proper Resource Manager (the class that itself loads the required files)
72       * @param resource ResourceManagerIface
73       * @spring.property
74       *   ref="ResourceManager"
75       */
76      public void setResource(ResourceManagerIface resource) {
77          this.resource = resource;
78      }
79  
80  
81  }