jp.terasoluna.fw.web.struts.actions.DispatchAction.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.struts.actions.DispatchAction.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.actions;

import java.util.Iterator;
import java.util.Map;

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

import jp.terasoluna.fw.web.thin.BlockageControlFilter;
import jp.terasoluna.fw.web.thin.ServerBlockageControlFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
*
* tH??[h??U???s?B
*
* <p>
*  NGXgp??[^p?AJ??B<br>
*  StrutsDispatchAction?ALookupDispatchActiondl?A??B
* </p>
* <p>
*  ?ANGXgp??[^L?[?Bean`t@CKv?s
*  ?iftHgl&quot;event&quot;?j?B
* </p>
* <p>
*  LL`t@C
*  NGXgp??[^L?[custom-event??B
* </p>
* <p>
* <strong>Bean`t@C?</strong>
*  <code><pre>
*  &lt;bean name="/dispatch" scope="prototype"
*    class="jp.terasoluna.fw.web.struts.actions.DispatchAction"&gt;
*    <strong>&lt;property name="event"&gt;
*      &lt;value&gt;custom-event&lt;/value&gt;
*    &lt;/property&gt;</strong>
*  &lt;/bean&gt;
*  </pre></code>
* </p>
* <p>
*  ?AJ?BD???B
*  <ol>
*   <li>?LwNGXgp??[^L?[l?A
*       ?&quot;forward_&quot;?A
*       &quot;forward_&quot;?</li>
*   <li>NGXgp??[^L?[?&quot;forward_&quot;t?A
*       &quot;forward_&quot;?</li>
*   <li>&quot;default&quot;
*   ?ievent=&quot;XXXX&quot;?A&quot;forward_XXXX&quot;
*       ?s?J?w???j</li>
*  </ol>
*
*  ?L?AJ&quot;#input&quot;
*  ?Astruts-config.xmlinput?J??B<br>
*  &quot;#input&quot;?Astruts-config.xmlforwardvfeJ??B
* </p>
* <p>
* <strong>struts-config.xml?</strong>
* <code><pre>
*  &lt;action path="/dispatch"
*    name="_sampleForm"
*    scope="session"
*    input="/prev.jsp"&gt;
*    &lt;forward name="regist" path="/userRegist.do"/&gt;
*    &lt;forward name="search" path="/userSearch.do"/&gt;
*    &lt;forward name="update" path="/userUpdate.do"/&gt;
*    &lt;forward name="decide" path="/prev.do"/&gt;
*    &lt;forward name="default" path="/prev.do"/&gt;
*  &lt;/action&gt;
* </pre></code>
* </p>
* <p>
* <strong>JSPL?q</strong>
*  <code><pre>
*  ?E?E?E
*  &lt;html:radio property="<strong>custom-event</strong>" value="<strong>forward_regist</strong>"/&gt;
*  &lt;html:radio property="<strong>custom-event</strong>" value="<strong>forward_search</strong>"/&gt;
*  &lt;html:radio property="<strong>custom-event</strong>" value="<strong>forward_update</strong>"/&gt;
*  &lt;html:radio property="<strong>custom-event</strong>" value="<strong>forward_#input</strong>"/&gt;
*  &lt;html:submit property="<strong>forward_decide</strong>" value=""/&gt;
*  &lt;html:submit value=""/&gt;
*  ?E?E?E
*  </pre></code>
*  NGXgp??[^l"forward_" + _?tH??[h`?B
* </p>
* <p>
*  e??s??J??B
*  <ol>
*   <li>1WI{^???A
*   J&quot;regist&quot;?AJ?&quot;/userRegist.do&quot;</li>
*   <li>4WI{^???A
*   J&quot;#input&quot;?AJ?&quot;/prev.do&quot;</li>
*   <li>WI{^I?A???A
*   J&quot;decide&quot;?AJ?&quot;/prev.do&quot;</li>
*   <li>???A
*   J&quot;default&quot;?AJ?&quot;/prev.do&quot;</li>
*  </ol>
* </p>
*
*/
public class DispatchAction extends ActionEx {

    /**
     * ?ONX?B
     */
    private static Log log = LogFactory.getLog(DispatchAction.class);

    /**
     * NGXgp??[^?Uw???A
     * ftHgJ?_?tH??[h?B
     */
    private static final String FORWARD_DEFAULT = "default";

    /**
     * ?Uw?A
     * NGXgp??[^L?[vtBbNX?B
     */
    private static final String FORWARD_PREFIX = "forward_";

    /**
     * J?\v?peB?B
     */
    private String event = null;

    /**
     * J?\v?peB??B
     *
     * @param value J?v?peB
     */
    public void setEvent(String value) {
        this.event = value;
    }

    /**
     * tH??[h??U???s?B
     * <p>
     *   tH??[h??U?AT?[o?tO
     *   ???B<br>
     *   ?J??AdoDetamineForward()?B
     * </p>
     *
     * @param mapping ANV}bsO
     * @param form ANVtH?[
     * @param req <code>HTTP</code>NGXg
     * @param res <code>HTTP</code>X|X
     * @return J??
     */
    @Override
    public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest req,
            HttpServletResponse res) {
        if (log.isDebugEnabled()) {
            log.debug("doExecute() called.");
        }
        // NGXgLZtO?mF?A
        // cancelled()?\bh?s?B
        if (isCancelled(req)) {
            ActionForward af = cancelled(mapping, form, req, res);
            if (af != null) {
                return af;
            }
        }

        if (event == null) {
            // eventw?A"event"ftHg??B
            event = "event";
        }

        String forward = doDetermineForward(req.getParameterMap(), event);

        ActionForward actionForward = null;
        if ("#input".equalsIgnoreCase(forward)) {
            actionForward = new ActionForward(mapping.getInput());
        } else {
            actionForward = mapping.findForward(forward);
        }

        // tH??[h????A"default" w
        // ANVtH??[hp?B
        if (actionForward == null) {
            if (log.isWarnEnabled()) {
                log.warn("forward name[" + forward + "] is invalid by user request.");
            }
            actionForward = mapping.findForward(FORWARD_DEFAULT);
        }

        // tH??[h??`FbNL
        // THRU_FILTERtO??
        // T?[o?
        req.removeAttribute(ServerBlockageControlFilter.SERVER_BLOCKAGE_THRU_KEY);
        // ?
        req.removeAttribute(BlockageControlFilter.BLOCKAGE_THRU_KEY);

        if (log.isDebugEnabled()) {
            log.debug("forward = " + forward + " (" + ((actionForward == null) ? "null" : actionForward.getPath())
                    + ")");
        }
        return actionForward;
    }

    /**
     * NGXgp??[^tH??[h?fBXpb`?B
     * paramsnull???AftHgp?B
     *
     * @param params NGXgp??[^?i}bv`?j
     * @param event ANV}bsOwCxg
     * @return ?U?_?tH??[h
     */
    protected String doDetermineForward(Map params, @SuppressWarnings("hiding") String event) {
        if (params != null) {
            if (exists(params, event)) {
                String[] eventValues = (String[]) params.get(event);
                for (int i = 0; i < eventValues.length; i++) {
                    if (eventValues[i].startsWith(FORWARD_PREFIX)) {
                        return eventValues[i].substring(FORWARD_PREFIX.length());
                    }
                }
            }
            Iterator iter = params.keySet().iterator();
            while (iter.hasNext()) {
                String key = (String) iter.next();
                if (key.startsWith(FORWARD_PREFIX)) {
                    String forward = key.substring(FORWARD_PREFIX.length());
                    if (forward.endsWith(".x") || forward.endsWith(".y")) {
                        forward = forward.substring(0, forward.length() - 2);
                    }
                    return forward;
                }
            }
        }
        return FORWARD_DEFAULT;
    }

    /**
     * NGXgp??[^?Anamew?p??[^
     * ??B
     *
     * @param params NGXgp??[^?i}bv`?j
     * @param name NGXgp??[^
     * @return NGXgp??[^? <code>true</code>
     */
    protected boolean exists(Map params, String name) {
        return params.containsKey(name);
    }

    /**
     * NGXgLZtO???J?
     * ?B?_?AActionForwardnullp?A
     * LZJ??A
     * NXp?NXI?[oCh?\bhKv?B
     *
     * @param mapping ANV}bsO
     * @param form ANVtH?[
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return ANVtH??[h
     */
    protected ActionForward cancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        return null;
    }

}