jp.terasoluna.fw.web.struts.action.RequestProcessorEx.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.struts.action.RequestProcessorEx.java

Source

/*
 * 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.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.terasoluna.fw.util.ExceptionUtil;
import jp.terasoluna.fw.web.RequestUtil;
import jp.terasoluna.fw.web.struts.form.ActionFormUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.DelegatingRequestProcessor;

/**
 * DelegatingRequestProcessorgNX?B
 * <p>
 *  NCAgNGXg?AURIANVN?s?B
 *  org.springframework.web.struts.DelegatingRequestProcessorp??A
 *  @\?B
 * </p>
 *
 * <ol>
 * <li>&quot;_&quot;n
 *  ANVtH?[ZbVB??</li>
 * <p>
 *  ZbVXR?[vANVtH?[_?
 *  &quot;_&quot;n???A
 *  CX^X???AZbVi[
 *  ANVtH?[_??A&quot;_&quot;
 *  n???B
 *  ?AZbV?1ANVtH?[
 *  ????AZbVXR?[v
 *  ANVtH?[????L?q?A
 *  p??gp?B
 * </p>
 * <h5>struts-config.xmlANVtH?[w</h5>
 * <code><pre>
 * &lt;struts-config&gt;
 * &lt;form-beans&gt;
 * &lt;!-- tH?[?"_"  --&gt;
 * &lt;form-bean name="_sampleForm"
 * type="jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx"
 * &gt;
 * &lt;form-property name="companyId" type="java.lang.String" /&gt;
 * ?@?@?@?@?@?E?E?E
 * &lt;/struts-config&gt;
 * </pre></code>
 *
 *
 * <li>tH?[f}~@\</li>
 * <p>
 *  rWlX?WbN?sfANVtH?[
 *  tH?[????AprocessPopulate()XLbv?B
 * </p>
 *
 * <li>ANVtH?[G?[?</li>
 * <p>
 *  `FbNG?[???A|bvAbvG?[
 *  \?AANVtH?[ANVG?[?B
 *  G?[??AANVG?[ANV
 *  tH?[??B
 * </p>
 *
 * <li>O??O?o@\</li>
 * <p>
 *  RequestProcessor#process()OX??[?A
 *  OX^bNg?[X?O?o?B
 *  ?O?o?A?OServletExceptionbv?AX??[?B
 * </p>
 * </ol>
 *
 * <p>
 *  RequestProcessorExp?Astruts-config.xml
 *  &lt;controller&gt;vfprocessorClass??Kv?B
 * <h5>struts-config.xml?</h5>
 * <code><pre>
 * &lt;struts-config&gt;
 *   ?E?E?E
 *   &lt;controller processorClass=
 *       "jp.terasoluna.fw.web.struts.RequestProcessorEx"/&gt;
 *   ?E?E?E
 * &lt;/struts-config&gt;
 * </pre></code>
 * tH?[}~@\???AActionExQ??B
 * </p>
 *
 * @see jp.terasoluna.fw.web.struts.actions.ActionEx
 * @see jp.terasoluna.fw.web.struts.form.FormEx
 *
 */
public class RequestProcessorEx extends DelegatingRequestProcessor {

    /**
     * ?ONX?B
     */
    @SuppressWarnings("hiding")
    private static Log log = LogFactory.getLog(RequestProcessorEx.class);

    /**
     * processPopulate()XLbvtONGXg?L?[?B
     */
    public static final String SKIP_POPULATE = "SKIP_POPULATE";

    /**
     * StrutsHTTPNGXg??g?B
     *
     * <p>StrutsActionServletprocess()?\bhX??[O
     * SystemException?\bhLb`?B
     * ?AOeZbVIDnbVl?O?o?A
     * ServletExceptionbvX??[?B</p>
     *
     * @param req HTTPNGXg
     * @param res HTTPX|X
     * @throws IOException IOO
     * @throws ServletException T?[ubgO
     */
    @Override
    public void process(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        if (log.isDebugEnabled()) {
            log.debug("process() called.");
        }

        try {
            // X?[p?[NX process() ?s
            super.process(req, res);
        } catch (IOException e) {
            String sessionHash = RequestUtil.getSessionHash(req);
            log.error("sessionHash = " + sessionHash);
            log.error(ExceptionUtil.getStackTrace(e));
            throw e;
        } catch (ServletException e) {
            String sessionHash = RequestUtil.getSessionHash(req);
            log.error("sessionHash = " + sessionHash);
            log.error(ExceptionUtil.getStackTrace(e));
            throw e;
        } catch (Exception e) {
            String sessionHash = RequestUtil.getSessionHash(req);
            log.error("sessionHash = " + sessionHash);
            log.error(ExceptionUtil.getStackTrace(e));
            throw new ServletException(e);
        }

        if (log.isDebugEnabled()) {
            log.debug("process() finished.");
        }
    }

    /**
     * StrutsANVtH?[??g?B
     *
     * <p>ANV}bsOwANVtH?[?A
     * "_"nANVtH?[???A
     * processActionFormEx()????B</p>
     *
     * @param req HTTPNGXg
     * @param res HTTPX|X
     * @param mapping ANV}bsO
     * @return ActionForm ANVtH?[
     */
    @Override
    protected ActionForm processActionForm(HttpServletRequest req, HttpServletResponse res, ActionMapping mapping) {
        if (log.isDebugEnabled()) {
            log.debug("processActionForm() called. path = " + mapping.getPath());
            log.debug("mapping.name = " + mapping.getName());
            log.debug("mapping.scope = " + mapping.getScope());
        }

        if (mapping instanceof ActionMappingEx && mapping.getScope() != null && "session".equals(mapping.getScope())
                && mapping.getName() != null && mapping.getName().startsWith("_")) {

            // XR?[v"session"?AANVtH?[_?"_"
            // n???AprocessActionFormEx() ?s
            return processActionFormEx(req, res, (ActionMappingEx) mapping);
        }
        // X?[p?[NX processActionForm() ?s
        return super.processActionForm(req, res, mapping);
    }

    /**
     * StrutsANVtH?[??g?B
     * processActionForm()?B
     *
     * <p>ANV}bsOwANVtH?[?A
     * "_"nANVtH?[???B</p>
     *
     * <p>ANVtH?[???A
     * RequestProcessorprocessPopulate()?\bhN?B</p>
     *
     * @param req HTTPNGXg
     * @param res HTTPX|X
     * @param mapping ANV}bsO
     * @return ANVtH?[
     */
    protected ActionForm processActionFormEx(HttpServletRequest req, HttpServletResponse res,
            ActionMappingEx mapping) {
        if (log.isDebugEnabled()) {
            log.debug("processActionFormEx() called. path = " + mapping.getPath());
        }

        boolean clearForm = false;
        // ANV}bsOclearFormv?peBQ??B
        clearForm = mapping.getClearForm();
        if (log.isDebugEnabled()) {
            log.debug("clearForm = " + clearForm);
        }

        if (clearForm) {
            // clearFormv?peBtrue?A
            // "_"wANVtH?[?A
            // ZbV???B
            HttpSession session = req.getSession();
            ActionFormUtil.clearActionForm(session);
        } else {
            // clearFormv?peBfalse?A
            // "_"i[ANVtH?[?A
            // ZbV???B
            // ?AwANVtH?[??B
            HttpSession session = req.getSession();
            ActionFormUtil.clearActionForm(session, mapping.getName());
        }

        // X?[p?[NX processActionForm() ?s
        return super.processActionForm(req, res, mapping);
    }

    /**
     * StrutsANVtH?[NGXgp??[^f??g?B
     *
     * <p>rWlX?WbN?sfANVtH?[
     * ????A
     * RequestProcessorprocessPopulate()?\bhN?B</p>
     * <p>struts-config.xmlANV}bsO??A
     * cancelPopulatetrue ???A
     * processPopulate LZ?B</p>
     *
     * @param req HTTPNGXg
     * @param res HTTPX|X
     * @param form ANVtH?[
     * @param mapping ANV}bsO
     * @throws ServletException T?[ubgO
     */
    @Override
    protected void processPopulate(HttpServletRequest req, HttpServletResponse res, ActionForm form,
            ActionMapping mapping) throws ServletException {

        if (log.isDebugEnabled()) {
            log.debug("processPopulate() called. path = " + mapping.getPath());
        }

        boolean cancelPopulateflg = false;
        cancelPopulateflg = ((ActionMappingEx) mapping).getCancelPopulate();
        if (cancelPopulateflg) {
            if (log.isDebugEnabled()) {
                log.debug("processPopulate() canceled.");
            }
            // ?~?s
            return;
        }

        String skipPopulate = (String) req.getAttribute(SKIP_POPULATE);
        if (skipPopulate != null) {
            if (skipPopulate.equals(mapping.getName())) {

                if (log.isDebugEnabled()) {
                    log.debug("processPopulate() skipped.");
                }
                // ?~?s
                return;
            }
            req.removeAttribute(SKIP_POPULATE);
        }
        // X?[p?[NX processPopulate() ?s
        super.processPopulate(req, res, form, mapping);

    }
}