1 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 ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 17 public class ActionRequest { 18 19 protected final WebApplication webapp; 20 protected final ActionConfig config; 21 protected HttpServletRequest request; 22 protected HttpServletResponse response; 23 24 protected final int totalInterceptors; 25 protected int interceptorIndex; 26 protected final Object action; 27 28 protected boolean executed = false; 29 protected boolean rendered = false; 30 31 32 34 public HttpServletRequest getHttpServletRequest() { 35 return request; 36 } 37 38 public void setHttpServletRequest(HttpServletRequest request) { 39 this.request = request; 40 } 41 42 public HttpServletResponse getHttpServletResponse() { 43 return response; 44 } 45 46 public void setHttpServletResponse(HttpServletResponse response) { 47 this.response = response; 48 } 49 50 public ActionConfig getActionConfig() { 51 return config; 52 } 53 54 public Object getAction() { 55 return action; 56 } 57 58 public String 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 76 79 protected ActionRequest(WebApplication webapp, ActionConfig config, HttpServletRequest request, HttpServletResponse 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 93 98 public String invoke() throws Exception { 99 if (executed == true) { 100 throw new MadvocException("Action '" + config.actionPath + "' has already been executed."); 101 } 102 if (interceptorIndex < totalInterceptors) { 104 ActionInterceptor interceptor = config.interceptors[interceptorIndex]; 105 interceptorIndex++; 106 return interceptor.intercept(this); 107 } 108 config.usage++; 110 Object actionInvocationResult = config.actionMethod.invoke(action); 111 executed = true; 112 return (actionInvocationResult != null ? actionInvocationResult.toString() : null); 113 } 114 115 117 protected String resultName; 118 119 protected String resultValue; 120 121 124 public String getResultName() { 125 return resultName; 126 } 127 128 131 public String getResultValue() { 132 return resultValue; 133 } 134 135 139 public void render(String value) throws Exception { 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 181 protected String buildResultTarget(String actionPath, String 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 target = new StringBuilder (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 |