01: package org.apache.struts.action;
02:
03: import java.io.IOException;
04: import java.util.HashMap;
05: import java.util.Enumeration;
06: import java.util.Map;
07: import java.util.Iterator;
08:
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11: import javax.servlet.ServletException;
12:
13: import org.apache.struts.StrutsConstants;
14:
15: /**
16: * StrutsCommand is an abstract class for representing the
17: * method by which next view page should be included.
18: * Right now, there are 3 ways by which struts transfers
19: * controlls to view page. These are forward/include/ActionForwrd.
20: * Correspondingly, there are 3 subclass for StrutsCommand viz.
21: * ForwardCommand, IncludeCommand and ForwardConfigCommand.
22: */
23:
24: abstract public class StrutsCommand {
25:
26: /**
27: * Map to store request attributes.
28: */
29: private Map _attributes = new HashMap(10);
30:
31: /**
32: * Request attributes set in the HttpServletRequest
33: * during the processAction should be available to the
34: * view during render request.
35: * @param request
36: */
37: public StrutsCommand(HttpServletRequest request) {
38: Enumeration attributeNames = request.getAttributeNames();
39: while (attributeNames.hasMoreElements()) {
40: String attributeName = (String) attributeNames
41: .nextElement();
42: if (!attributeName.equals(StrutsConstants.STRUTS_PATH)
43: && !attributeName.startsWith("javax.portlet")) {
44: _attributes.put(attributeName, request
45: .getAttribute(attributeName));
46: }
47: }
48: }
49:
50: /**
51: * Every command class extending StrutsCommand will use
52: * following method to popuplate all the request attribute
53: * stored during processAction before passing it to the
54: * respective view processing methods.
55: * @param request
56: * @return
57: */
58: protected HttpServletRequest getRequest(HttpServletRequest request) {
59: Iterator attributeNames = _attributes.keySet().iterator();
60: while (attributeNames.hasNext()) {
61: String attributeName = (String) attributeNames.next();
62: request.setAttribute(attributeName, _attributes
63: .get(attributeName));
64: }
65: return request;
66: }
67:
68: /**
69: * Following method will be implemented by various StrutsCommands
70: * @param processor
71: * @param request
72: * @param response
73: * @throws IOException
74: * @throws ServletException
75: */
76: abstract public void execute(RequestProcessor processor,
77: HttpServletRequest request, HttpServletResponse response)
78: throws IOException, ServletException;
79: }
|