Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.web.struts.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import jp.terasoluna.fw.web.struts.action.ActionMappingEx; import jp.terasoluna.fw.web.struts.action.RequestProcessorEx; import jp.terasoluna.fw.web.struts.form.FormEx; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessages; /** * ANVNX?B * * <p> * StrutsAction?A?O?o@\?EgUNV * g?[N`FbN@\?B<br> * @\p???A{NXp??B<br> * {NXp?AdoExecute()?\bh * I?[oChTuNX???Astruts-config.xml * <action>vftype?NX??B<br> * gUNVg?[N`FbN?ATu~bgQx?A * uEU?u?v{^g?dTu~bgh@\?B<br> * gUNVg?[N`FbN?s?A? * ?sKv?B * <ul> * <li> * ANVNXBean` * <property>vf * "tokenCheck"?A * "true"Kv * </li> * <li> * struts-config.xml * <forward>vf?i<grobal-forwards> * ?j"txtoken-error"O * g?[NG?[pXw * </li> * </ul> * ActionExgp???AsaveToken() * Ig?[N?A????s * Kv?B<br> * <ul> * <li> * ANVNXBean` * <property>vf * "saveToken"?A * "false"Kv * </li> * </ul> * gUNVg?[N`FbNstruts-config.xml * yBean`t@C??AL?B<br> * </p> * <p> * <strong>TuNXLogoffActionBean`t@C?</strong> * <code><pre> * <bean name="/admin/Logout" scope="prototype" * class="jp.terasoluna.fw.web.struts.actions.LogoffAction"> * <property property="tokenCheck"> * <value>true</value> * </property> * <property property="saveToken"> * <value>false</value> * </property> * </bean> * </pre></code> * </p> * <p> * <strong>TuNXLogoffActionstruts-config.xml?</strong> * <code><pre> * <action path="/admin/Logout" * name="logonSampleForm" * scope="session" * parameter="/sub/logout.jsp"> * <forward name="txtoken-error" modeule="/sub" * path="/doubleRegistError.jsp"/> * </action> * </pre></code> * * ?Avfforward?Apath?XbV * "/"t?Amodule????A * W?[pXJ?B<br> * ?A{@\TuNXp????AdoExecute()?\bh * Gg|CgKv?B<br> * ANV}bsO??AActionMappingExQ??B * </p> * * @see jp.terasoluna.fw.web.struts.action.ActionMappingEx * */ public abstract class ActionEx extends Action { /** * ?ONX?B */ private static Log log = LogFactory.getLog(ActionEx.class); /** * gUNVg?[N`FbNG?[_?tH??[h?B */ protected static final String FORWARD_TXTOKEN_ERROR = "txtoken-error"; /** * gUNVg?[N`FbN?s?B * ftHgfalse?i?s?j?B */ private boolean tokenCheck = false; /** * gUNVg?[N?B * ftHgtrue?i?j?B */ private boolean saveToken = true; /** * gUNVg?[N`FbNtO??B * * @param tokenCheck gUNVg?[N`FbNtO */ public void setTokenCheck(boolean tokenCheck) { this.tokenCheck = tokenCheck; } /** * gUNVg?[NtO??B * * @param saveToken gUNVg?[NtO */ public void setSaveToken(boolean saveToken) { this.saveToken = saveToken; } /** * gUNVg?[N`FbNtO?B * * @return gUNVg?[N`FbNtO */ protected boolean isTokenCheck() { return tokenCheck; } /** * gUNVg?[NtO * * @return gUNVg?[NtO */ protected boolean isSaveToken() { return saveToken; } /** * ANV?s?B * <p> * TuNXdoExecute()?\bh?oO?A * ANVtH?[modified?false??B<br> * doExecute()?s?AANVtH?[ * tB?[hl?XNGXg?SKIP_POPULATE * ?ARequestProcessorEx * processPopulate()??}??B * </p> * * @param mapping ANVRANV}bsO * @param form ANVtH?[ * @param request NGXg? * @param response X|X? * @return J?? * @throws Exception ANV?sO */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (log.isDebugEnabled()) { log.debug("execute() called."); } if (!processTokenCheck(mapping, request)) { if (log.isInfoEnabled()) { log.info("Transaction token error."); } if (log.isDebugEnabled()) { log.debug("forward = " + FORWARD_TXTOKEN_ERROR); } return mapping.findForward(FORWARD_TXTOKEN_ERROR); } if (form != null && form instanceof FormEx) { FormEx formEx = (FormEx) form; formEx.setModified(false); } if (log.isDebugEnabled()) { log.debug("doExecute() called."); } ActionForward forward = doExecute(mapping, form, request, response); if (log.isDebugEnabled()) { log.debug("doExecute() finished."); } if (form != null && form instanceof FormEx) { FormEx formEx = (FormEx) form; if (formEx.isModified()) { request.setAttribute(RequestProcessorEx.SKIP_POPULATE, mapping.getName()); } } if (log.isDebugEnabled()) { if (forward != null) { log.debug("forward = " + forward.getName() + "(" + forward.getPath() + ")"); } else { log.debug("forward = null"); } } return forward; } /** * ANV?s??\bh?B * <p> * ?\bhTuNXI?[o?[Ch?A * ANV@\g?B * </p> * * @param mapping ANVRANV}bsO * @param form ANVtH?[ * @param request NGXg? * @param response X|X? * @return J?? * @throws Exception \O */ public abstract ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception; /** * gUNVg?[N`FbN?s?B * * @param mapping ANV}bsO * @param req HTTPNGXg * @return g?[N? <code>true</code> */ protected boolean processTokenCheck(ActionMapping mapping, HttpServletRequest req) { if (log.isDebugEnabled()) { log.debug("processTokenCheck() called."); } // mappingActionMappingEx?A??truep if (!(mapping instanceof ActionMappingEx)) { if (log.isDebugEnabled()) { log.debug("mapping is not instance of ActionMappingEx."); } return true; } boolean result = true; synchronized (req.getSession().getId().intern()) { if (tokenCheck) { if (!isTokenValid(req)) { result = false; } } if (saveToken) { saveToken(req); } } return result; } /** * ZbV?AGlobals.ERROR_KEYL?[ * ActionMessages?A?xi[?s?B * * @param session ZbV * @param errors G?[?bZ?[W? */ protected void addErrors(HttpSession session, ActionMessages errors) { if (session == null) { return; } if (errors == null) { return; } // ZbVG?[?bZ?[W??A?V??? ActionMessages sessionErrors = (ActionMessages) session.getAttribute(Globals.ERROR_KEY); if (sessionErrors == null) { sessionErrors = new ActionMessages(); } // ?G?[ sessionErrors.add(errors); // ?AG?[?bZ?[W????AZbVSG?[?bZ?[W??? if (sessionErrors.isEmpty()) { session.removeAttribute(Globals.ERROR_KEY); return; } // ZbVG?[?bZ?[W session.setAttribute(Globals.ERROR_KEY, sessionErrors); } /** * ZbV?AGlobals.MESSAGE_KEYL?[ * ActionMessages?A?xi[?s?B * * @param session ZbV * @param messages ?bZ?[W? */ protected void addMessages(HttpSession session, ActionMessages messages) { if (session == null) { return; } if (messages == null) { return; } // ZbV?bZ?[W??A?V??? ActionMessages sessionMessages = (ActionMessages) session.getAttribute(Globals.MESSAGE_KEY); if (sessionMessages == null) { sessionMessages = new ActionMessages(); } // ??bZ?[W sessionMessages.add(messages); // ?A?bZ?[W????AZbVS?bZ?[W??? if (sessionMessages.isEmpty()) { session.removeAttribute(Globals.MESSAGE_KEY); return; } // ZbV?bZ?[W session.setAttribute(Globals.MESSAGE_KEY, sessionMessages); } }