KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > madvoc > ActionRequest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.madvoc;
4
5 import jodd.madvoc.interceptor.ActionInterceptor;
6 import jodd.madvoc.result.ActionResult;
7 import jodd.util.StringUtil;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 /**
13  * Encapsulates single action invocation and acts as an action proxy.
14  * It invokes all assigned interceptors during action invocation and
15  * specified result set after the action method invocation.
16  */

17 public class ActionRequest {
18
19     protected final WebApplication webapp;
20     protected final ActionConfig config;
21     protected HttpServletRequest JavaDoc request;
22     protected HttpServletResponse JavaDoc response;
23
24     protected final int totalInterceptors;
25     protected int interceptorIndex;
26     protected final Object JavaDoc action;
27
28     protected boolean executed = false;
29     protected boolean rendered = false;
30
31
32     // ---------------------------------------------------------------- accessors
33

34     public HttpServletRequest JavaDoc getHttpServletRequest() {
35         return request;
36     }
37
38     public void setHttpServletRequest(HttpServletRequest JavaDoc request) {
39         this.request = request;
40     }
41
42     public HttpServletResponse JavaDoc getHttpServletResponse() {
43         return response;
44     }
45
46     public void setHttpServletResponse(HttpServletResponse JavaDoc response) {
47         this.response = response;
48     }
49
50     public ActionConfig getActionConfig() {
51         return config;
52     }
53
54     public Object JavaDoc getAction() {
55         return action;
56     }
57
58     public String JavaDoc getActionPath() {
59         return config.actionPath;
60     }
61
62     public WebApplication getWebApplication() {
63         return webapp;
64     }
65
66     public boolean isExecuted() {
67         return executed;
68     }
69
70     public boolean isRendered() {
71         return rendered;
72     }
73
74     // ---------------------------------------------------------------- ctor
75

76     /**
77      * Creates new action request.
78      */

79     protected ActionRequest(WebApplication webapp, ActionConfig config, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
80         this.webapp = webapp;
81         this.config = config;
82         this.request = request;
83         this.response = response;
84
85         totalInterceptors = (this.config.interceptors != null ? this.config.interceptors.length : 0);
86         interceptorIndex = 0;
87         this.action = webapp.buildAction(config);
88     }
89
90
91     // ---------------------------------------------------------------- invoke
92

93     /**
94      * Invokes all interceptors and the action.
95      * @see #render(String)
96      * @return action result value
97      */

98     public String JavaDoc invoke() throws Exception JavaDoc {
99         if (executed == true) {
100             throw new MadvocException("Action '" + config.actionPath + "' has already been executed.");
101         }
102         // interceptors
103
if (interceptorIndex < totalInterceptors) {
104             ActionInterceptor interceptor = config.interceptors[interceptorIndex];
105             interceptorIndex++;
106             return interceptor.intercept(this);
107         }
108         // action
109
config.usage++;
110         Object JavaDoc actionInvocationResult = config.actionMethod.invoke(action);
111         executed = true;
112         return (actionInvocationResult != null ? actionInvocationResult.toString() : null);
113     }
114
115     // ---------------------------------------------------------------- result
116

117     protected String JavaDoc resultName;
118
119     protected String JavaDoc resultValue;
120
121     /**
122      * Returns result name. Availiable only after rendering.
123      */

124     public String JavaDoc getResultName() {
125         return resultName;
126     }
127
128     /**
129      * Returns result value. Availiable only after rendering.
130      */

131     public String JavaDoc getResultValue() {
132         return resultValue;
133     }
134
135     /**
136      * Invokes a result after the action invocation.
137      * @see #invoke()
138      */

139     public void render(String JavaDoc value) throws Exception JavaDoc {
140         if (executed == false) {
141             throw new MadvocException("Result can not be rendered before the action.");
142         }
143         if (rendered == true) {
144             throw new MadvocException("Result for action '" + config.actionPath + "' already rendered.");
145         }
146         resultValue = value;
147         if (resultValue == null) {
148             return;
149         }
150
151         resultName = webapp.getDefaultActionResultName();
152         int columnIndex = resultValue.indexOf(':');
153         if (columnIndex != -1) {
154             resultName = resultValue.substring(0, columnIndex);
155             resultValue = resultValue.substring(columnIndex + 1);
156         }
157         resultValue = buildResultTarget(config.actionPath, resultValue);
158         resultValue = webapp.getResultAlias(resultValue);
159         columnIndex = resultValue.indexOf(':');
160         if (columnIndex != -1) {
161             resultName = resultValue.substring(0, columnIndex);
162             resultValue = resultValue.substring(columnIndex + 1);
163         }
164
165         ActionResult result = webapp.lookupActionResult(resultName);
166         if (result == null) {
167             throw new MadvocException("Unable to find action result '" + resultName + "'.");
168         }
169
170         result.execute(this, resultValue);
171         rendered = true;
172     }
173
174     /**
175      * Builds result target string by inserting result value after the path
176      * and before the extension. Extension is stripped.
177      * If result value starts with '/' then it represents an absolute path
178      * and it will be returned as is.
179      * If result value starts with '.' then method name will be also stripped.
180      */

181     protected String JavaDoc buildResultTarget(String JavaDoc actionPath, String JavaDoc resultValue) {
182         if (resultValue.startsWith("/")) {
183             return resultValue;
184         }
185         int slashNdx = actionPath.lastIndexOf('/');
186         if (slashNdx == -1) {
187             slashNdx = 0;
188         }
189         int ndx = StringUtil.lastIndexOf(actionPath, '.', actionPath.length(), slashNdx);
190         if (ndx != -1) {
191             actionPath = actionPath.substring(0, ndx);
192         }
193         boolean dot = true;
194         if (resultValue.startsWith(".")) {
195             ndx = StringUtil.lastIndexOf(actionPath, '.', actionPath.length(), slashNdx);
196             if (ndx != -1) {
197                 actionPath = actionPath.substring(0, ndx);
198                 dot = false;
199             }
200         }
201         StringBuilder JavaDoc target = new StringBuilder JavaDoc(actionPath.length() + resultValue.length() + 8);
202         target.append(actionPath);
203         if (resultValue.length() != 0) {
204             if (dot == true) {
205                 target.append('.');
206             }
207             target.append(resultValue);
208         }
209         return target.toString();
210     }
211 }
Popular Tags