1 package jodd.servlet; 2 3 import java.io.IOException; 4 import java.util.HashMap; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import jodd.util.ReflectUtil; 11 import jodd.util.StringUtil; 12 13 29 public class ActionController extends ActionServlet { 30 31 34 private HashMap actionsMap = new HashMap(); 35 38 private ActionData global_forwards = new ActionData(); 39 42 private ActionFilter actionFilter = null; 43 44 46 private String configPath; 47 private String configFile; 48 private String configFilter; 49 50 59 public void init(javax.servlet.ServletConfig config) throws ServletException { 60 super.init(config); 61 62 configPath = config.getServletContext().getRealPath(""); 64 configFile = config.getInitParameter("config"); 65 configFilter = config.getInitParameter("filter"); 66 67 reload(); 68 } 69 70 77 public void reload() throws ServletException { 78 actionsMap = new HashMap(); 79 global_forwards = new ActionData(); 80 ActionControllerUtil.parseFile(actionsMap, global_forwards, configPath, configFile); 81 82 if (configFilter != null) { 83 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 84 if (classLoader == null) { 85 classLoader = ActionController.class.getClassLoader(); 86 } 87 try { 88 Class c = classLoader.loadClass(configFilter); 89 actionFilter = (ActionFilter) c.newInstance(); 90 } catch (Exception ex) { 91 actionFilter = null; 92 } 93 } else { 94 actionFilter = null; 95 } 96 } 97 98 100 101 108 public static String ACTION_DATA = "jodd.servlet.ActionController.actionData"; 109 110 115 public static String ACTION_PATH = "jodd.servlet.ActionController.actionPath"; 116 117 121 public static String ACTION_NOT_FOUND = "jodd.servlet.ActionController.actionNotFound"; 122 123 155 public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 156 String uri = request.getRequestURI(); 157 String ctxPath = request.getContextPath(); 158 int i = uri.indexOf(ctxPath); 159 String actionPath = uri.substring(i + ctxPath.length()); 160 161 ActionData actionData = (ActionData) actionsMap.get(actionPath); 162 if (actionData == null) { 163 String actionNotFoundPath = global_forwards.getForwardPath(ACTION_NOT_FOUND); 164 if (actionNotFoundPath == null) { 165 actionNotFoundPath = ACTION_NOT_FOUND; 166 } 167 forward(request, response, actionNotFoundPath); 168 return; 169 } 170 171 String actionResult = invokeAction(request, response, actionData, actionPath); 173 174 if (actionResult != null) { 179 String actionResultUri = actionResult; 180 String actionResultParams = ""; 181 i = actionResult.indexOf('?'); 182 if (i != -1) { 183 actionResultUri = actionResult.substring(0, i); 184 actionResultParams = actionResult.substring(i + 1); 185 } 186 187 String fwdPath = actionData.getForwardPath(actionResultUri); 188 if (fwdPath == null) { 189 fwdPath = global_forwards.getForwardPath(actionResultUri); 190 } 191 if (fwdPath == null) { 192 fwdPath = actionResultUri; 193 } 194 195 if (actionResultParams.length() > 0) { 197 i = fwdPath.indexOf('?'); 198 if (i != -1) { 199 fwdPath += "&" + actionResultParams; 200 } else { 201 fwdPath += "?" + actionResultParams; 202 } 203 } 204 if (actionData.isForwardRedirect(actionResultUri)) { 205 redirect(request, response, fwdPath); 206 } else { 207 forward(request, response, fwdPath); 208 } 209 } 210 } 211 212 213 215 216 220 public static String INVOKE_ACTION_PARAMS = "jodd.servlet.ActionController.invokeActionParams"; 221 222 239 public String invokeAction(HttpServletRequest request, HttpServletResponse response, String actionPath) throws IOException, ServletException { 240 String actionPathUri = actionPath; 241 int i = actionPath.indexOf('?'); 242 if (i != -1) { 243 actionPathUri = actionPath.substring(0, i); 244 HashMap map = ServletUtil.getUrlParams(actionPath); 245 request.setAttribute(INVOKE_ACTION_PARAMS, map); 246 } 247 248 ActionData actionData = (ActionData) actionsMap.get(actionPathUri); 249 if (actionData == null) { 250 return ACTION_NOT_FOUND; 251 } 252 return invokeAction(request, response, actionData, actionPathUri); 253 } 254 255 267 private String invokeAction(HttpServletRequest request, HttpServletResponse response, ActionData actionData, String actionPath) throws IOException, ServletException { 268 269 ActionServlet action = actionData.getAction(); 270 271 if (action == null) { 273 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 274 if (classLoader == null) { 275 classLoader = ActionController.class.getClassLoader(); 276 } 277 try { 278 Class c = classLoader.loadClass((String) actionData.getType()); 279 action = (ActionServlet) c.newInstance(); 280 } catch (Exception ex) { 281 action = null; 282 throw new ServletException("can't load ActionServlet " + (String)actionData.getType(), ex); 283 } 284 action.controller = this; actionData.setAction(action); 286 actionsMap.put(actionPath, actionData); 287 } 288 289 String actionResult = null; 291 if (action != null) { 292 request.setAttribute(ACTION_DATA, actionData); 293 request.setAttribute(ACTION_PATH, actionPath); 294 String method = actionData.getMethod(); 295 296 String actionFilterResult = null; 298 if (actionFilter != null) { 299 actionFilterResult = actionFilter.onAction(request, response, action); 300 } 301 if (actionFilterResult == null) { 302 if (method == null) { 303 actionResult = action.doAction(request, response); 305 } else { 306 try { 308 actionResult = StringUtil.toString(ReflectUtil.invoke(action, method, new Object[] {request, response}, new Class[] {HttpServletRequest.class, HttpServletResponse.class})); 310 } catch (Exception ex) { 313 } 314 } 315 if (actionFilter != null) { 316 actionFilterResult = actionFilter.onAfterAction(request, response, action, actionResult); 317 if (actionFilterResult != null) { 318 actionResult = actionFilterResult; 319 } 320 } 321 } else { 322 actionResult = actionFilterResult; 323 } 324 } 325 return actionResult; 326 } 327 328 } 329 | Popular Tags |