1 23 24 package org.mdarad.framework.util.struts; 25 26 27 import org.apache.commons.beanutils.ConvertUtils; 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.Action; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessages; 34 import org.apache.struts.action.ActionServlet; 35 import org.apache.struts.config.ForwardConfig; 36 import org.apache.struts.config.ModuleConfig; 37 import org.apache.struts.tiles.TilesRequestProcessor; 38 import org.mdarad.framework.exception.SystemException; 39 40 import javax.servlet.ServletException ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.io.IOException ; 44 import java.io.UnsupportedEncodingException ; 45 import java.net.URI ; 46 import java.util.Currency ; 47 import java.util.Locale ; 48 49 public abstract class AbstractRequestProcessor extends org.apache.struts.action.SecureTilesRequestProcessor { 50 private final static String EXCEPTION_KEY = "org.mdarad.framework.util.struts.AbstractRequestProcessor.EXCEPTION_KEY"; 51 private final static String ACTION_KEY = "org.mdarad.framework.util.struts.AbstractRequestProcessor.ACTION_KEY"; 52 53 56 protected ActionForward processActionPerform(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Action action, ActionForm actionForm, ActionMapping actionMapping) throws IOException , ServletException { 57 StrutsChainedException exception = (StrutsChainedException) httpServletRequest.getAttribute(EXCEPTION_KEY); 58 if (exception != null) { 59 return processException(httpServletRequest, httpServletResponse, (Exception ) exception.getThrowable(0), actionForm, actionMapping); 60 } 61 return super.processActionPerform(httpServletRequest, httpServletResponse, action, actionForm, actionMapping); 62 } 63 64 protected ActionForward processException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception e, ActionForm actionForm, ActionMapping actionMapping) throws IOException , ServletException { 65 e.printStackTrace(System.err); 66 return super.processException(httpServletRequest, httpServletResponse, e, actionForm, actionMapping); } 68 69 70 public void init(ActionServlet servlet, ModuleConfig moduleConfig) throws ServletException { 71 StrutsConverter strutsConverter = new StrutsConverter(); 72 ConvertUtils.register(strutsConverter, Locale .class); 73 ConvertUtils.register(strutsConverter, Currency .class); 74 ConvertUtils.register(strutsConverter, URI .class); 75 ConvertUtils.register(strutsConverter, Integer .class); 76 super.init(servlet, moduleConfig); 77 } 78 79 static public void chainException(HttpServletRequest httpServletRequest, Throwable throwable) { 80 StrutsChainedException exception = (StrutsChainedException) httpServletRequest.getAttribute(EXCEPTION_KEY); 81 if (exception == null) exception = new StrutsChainedException(); 82 exception.chain(throwable); 83 httpServletRequest.setAttribute(EXCEPTION_KEY, exception); 84 } 85 86 protected Action processActionCreate(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ActionMapping actionMapping) throws IOException { 87 Action action = (Action) httpServletRequest.getAttribute(ACTION_KEY); 88 if (action == null) action = super.processActionCreate(httpServletRequest, httpServletResponse, actionMapping); 89 return action; 90 } 91 92 protected void processLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { 93 try { 94 httpServletRequest.setCharacterEncoding("UTF-8"); 95 } catch (UnsupportedEncodingException e) { 96 throw new RuntimeException (e); 97 } 98 super.processLocale(httpServletRequest, httpServletResponse); 99 } 100 101 protected boolean processRoles(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ActionMapping actionMapping) throws IOException , ServletException { 102 return true; 103 } 104 105 protected abstract boolean processAuthenticationRoles(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ActionForm actionForm, ActionMapping actionMapping) throws IOException , ServletException ; 106 107 protected boolean processLocalization(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ActionForm actionForm, ActionMapping actionMapping) throws IOException , ServletException { 108 try { 109 StrutsLocalizationUtils.processLocale(httpServletRequest, "org.mdarad.framework.util.struts.AbstractAction.webLocalizationContext"); 110 } catch (Exception e) { 111 processForwardConfig(httpServletRequest, httpServletResponse, processException(httpServletRequest, httpServletResponse, e, actionForm, actionMapping)); 112 return false; 113 } 114 return true; 115 } 116 117 protected boolean processInvalidForm(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ActionForm actionForm, ActionMapping actionMapping) throws IOException , ServletException { 118 Action action = super.processActionCreate(httpServletRequest, httpServletResponse, actionMapping); 119 httpServletRequest.setAttribute(ACTION_KEY, action); 120 121 if (action instanceof InvalidFormRequestPopulator) { 122 InvalidFormRequestPopulator requestPopulatorAction = (InvalidFormRequestPopulator) action; 123 try { 124 requestPopulatorAction.populateRequestOnInvalidForm(httpServletRequest, actionForm); 125 } catch (SystemException e) { 126 processForwardConfig(httpServletRequest, httpServletResponse, processException(httpServletRequest, httpServletResponse, e, actionForm, actionMapping)); 127 return false; 128 } 129 } 130 return true; 131 } 132 133 protected boolean processValidate(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping) throws IOException , ServletException { 134 if (!processLocalization(request, response, form, mapping)) { 135 return false; 136 } 137 if (!processAuthenticationRoles(request, response, form, mapping)) { 138 return false; 139 } 140 141 if (form == null) { 142 return (true); 143 } 144 145 if (request.getAttribute(Globals.CANCEL_KEY) != null) { 147 return (true); 148 } 149 150 if (!mapping.getValidate()) { 152 return (true); 153 } 154 155 ActionMessages errors = form.validate(mapping, request); 157 if ((errors == null) || errors.isEmpty()) { 158 return (true); 159 } else { 160 if (!processInvalidForm(request, response, form, mapping)) { 161 return false; 162 } 163 } 164 165 if (form.getMultipartRequestHandler() != null) { 167 form.getMultipartRequestHandler().rollback(); 168 } 169 170 String input = mapping.getInput(); 172 if (input == null) { 173 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 174 getInternal().getMessage("noInput", 175 mapping.getPath())); 176 return (false); 177 } 178 179 request.setAttribute(Globals.ERROR_KEY, errors); 181 182 if (moduleConfig.getControllerConfig().getInputForward()) { 183 ForwardConfig forward = mapping.findForward(input); 184 processForwardConfig(request, response, forward); 185 } else { 186 internalModuleRelativeForward(input, request, response); 187 } 188 189 return (false); 190 } 191 } | Popular Tags |