/*
 * $Header: /home/cvs/struts/xdocs/struts1.2/documentation/ja/src/share/org/apache/struts/action/Action.java,v 1.9 2005/05/09 13:23:57 tatakaha Exp $
 * $Revision: 1.9 $
 * $Date: 2005/05/09 13:23:57 $
 *
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.struts.action;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.Globals;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.TokenProcessor;

/**
 * <p><strong>Action</strong>は、受信した
 * HTTPリクエストの内容とそのリクエストを処理するために実行されるべきビジネスロジックを適合させます。
 * コントローラ(RequestProcessor)は、
 * それぞれのリクエストに適切なActionを選択して(必要に応じて)インスタンスを生成し、
 * <code>execute</code>メソッドを呼び出します。</p>
 * {@primary <p>An <strong>Action</strong> is an adapter between the contents of an incoming
 * HTTP request and the corresponding business logic that should be executed to
 * process this request. The controller (RequestProcessor) will select an
 * appropriate Action for each request, create an instance (if necessary),
 * and call the <code>execute</code> method.</p>}
 *
 * <p>コントローラは同時に要求された複数のリクエストに対して同一のインスタンスを共有するため、
 * Actionはスレッドセーフとなるようにプログラムしなくてはいけません。
 * すなわち、次の項目に注意して設計すべきです。
 * </p>
 * {@primary <p>Actions must be programmed in a thread-safe manner, because the
 * controller will share the same instance for multiple simultaneous
 * requests. This means you should design with the following items in mind:
 * </p>}
 * <ul>
 * <li>
 * 特定のリクエストの状態に関連する情報を保持するために、
 * インスタンス変数やスタティック変数を使用してはいけません。
 * 同一のアクションに関してリクエストをまたがってグローバルなリソースを共有するために使用するのはかまいません。
 * </li>
 * {@primary <li>Instance and static variables MUST NOT be used to store information
 *     related to the state of a particular request. They MAY be used to
 *     share global resources across requests for the same action.</li>}
 * <li>
 * 他のリソース(JavaBeans、セッション変数、等)へのアクセスは、
 * それらの保護が必要な場合は同期化しなくてはいけません。
 * (しかしながら、
 * 一般的にリソースクラスは必要に応じて自分自身を保護するように設計すべきです。)
 * </li>
 * {@primary <li>Access to other resources (JavaBeans, session variables, etc.) MUST
 *     be synchronized if those resources require protection. (Generally,
 *     however, resource classes should be designed to provide their own
 *     protection where necessary.</li>}
 * </ul>
 *
 * <p><code>Action</code>インスタンスが最初に生成される時、
 * コントローラはそのActionが関連付けられたサーブレットインスタンスを識別するため、
 * nullでない引数で<code>setServlet</code>メソッドを呼び出します
 * サーブレットをシャットダウン(または再起動)する時、
 * Actionに割り当てられている使用中のリソースをすべてクリーンアップするため、
 * <code>null</code>を引数に<code>setServlet</code>メソッドを呼び出します。</p>
 * {@primary <p>When an <code>Action</code> instance is first created, the controller
 * will call <code>setServlet</code> with a non-null argument to
 * identify the servlet instance to which this Action is attached.
 * When the servlet is to be shut down (or restarted), the
 * <code>setServlet</code> method will be called with a <code>null</code>
 * argument, which can be used to clean up any allocated resources in use
 * by this Action.</p>}
 *
 * @version $Revision: 1.9 $ $Date: 2005/05/09 13:23:57 $
 * @translator 横田 健彦
 * @translator 棚澤 昌幸
 * @editor 高橋 達男
 * @update 2005/04/30
 * @status admindraft
 * @memo ブロックA
 */
public class Action {

    /**
     * <p>トークン機能で使用される<code>TokenProcessor</code>のインスタンス。</p>
     * {@primary <p>An instance of <code>TokenProcessor</code> to use for token functionality.</p>}
     */
    private static TokenProcessor token = TokenProcessor.getInstance();
    // :TODO: We can make this variable protected and remove Action's token methods
    // or leave it private and allow the token methods to delegate their calls.


    // ----------------------------------------------------- Instance Variables


    /**
     * <p>システムのデフォルトLocale。</p>
     * {@primary <p>The system default Locale.</p>}
     *
     * @deprecated Locale.getDefaultを直接使ってください。
     * Struts 1.2.より後で削除されます。
     * {@primary @deprecated Use Locale.getDefault directly.  This will be removed after
     * Struts 1.2.}
     */
    protected static Locale defaultLocale = Locale.getDefault();
    // :TODO: Remove after Struts 1.2


    /**
     * <p>関連付けられたサーブレット。</p>
     * {@primary <p>The servlet to which we are attached.</p>}
     */
    protected ActionServlet servlet = null;


    // ------------------------------------------------------------- Properties


    /**
     * <p>関連付けられたサーブレットのインスタンスを返します。</p>
     * {@primary <p>Return the servlet instance to which we are attached.</p>}
     */
    public ActionServlet getServlet() {

        return (this.servlet);

    }


    /**
     * <p>(<code>servlet</code>がnullでない場合)関連付けられたサーブレットのインスタンスを設定します。
     * (<code>servlet</code>がnullの場合)関連付けられたサーブレットのインスタンスを解放します。</p>
     * {@primary <p>Set the servlet instance to which we are attached (if
     * <code>servlet</code> is non-null), or release any allocated resources
     * (if <code>servlet</code> is null).</p>}
     *
     * @param servlet もしあれば、新しいコントローラサーブレット
     * {@primary @param servlet The new controller servlet, if any}
     */
    public void setServlet(ActionServlet servlet) {

        this.servlet = servlet;
        // :FIXME: Is this suppose to release resources?


    }


    // --------------------------------------------------------- Public Methods


    /**
     * <p>指定された非HTTPリクエストを処理して、
     * 対応する非HTTPレスポンスを生成します。
     * (またはレスポンスを生成する他のWebコンポーネントにリクエストをフォワードします。)
     * 併せてビジネスロジックがスローする例外のハンドリングも提供します。
     * コントロールをどこに/どうやってフォワードすべきかが記述された{@link ActionForward}インスタンスもしくは、
     * レスポンスが既に完了している場合は<code>null</code>を返します。</p>
     * {@primary <p>Process the specified non-HTTP request, and create the
     * corresponding non-HTTP response (or forward to another web
     * component that will create it), with provision for handling
     * exceptions thrown by the business logic.
     * Return an {@link ActionForward} instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.</p>}
     *
     * <p>デフォルトの実装では、
     * このメソッドのHTTPバージョンにフォワードしようとします。</p>
     * {@primary <p>The default implementation attempts to forward to the HTTP
     * version of this method.</p>}
     *
     * @param mapping このインスタンスを選択するために使用するActionMapping
     * {@primary @param mapping The ActionMapping used to select this instance}
     * @param form このリクエストのためのActionForm Bean(もしあれば)
     * {@primary @param form The optional ActionForm bean for this request (if any)}
     * @param request 処理中の非HTTPリクエスト
     * {@primary @param request The non-HTTP request we are processing}
     * @param response 生成する非HTTPレスポンス
     * {@primary @param response The non-HTTP response we are creating}
     *
     * @exception Exception アプリケーションのビジネスロジックが例外をスローした場合
     * {@primary @exception Exception if the application business logic throws
     *  an exception.}
     * @since Struts 1.1
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        ServletRequest request,
        ServletResponse response)
        throws Exception {

        try {
            return execute(
                mapping,
                form,
                (HttpServletRequest) request,
                (HttpServletResponse) response);

        } catch (ClassCastException e) {
            return null;
        }

    }


    /**
     * <p>指定されたHTTPリクエストを処理して、
     * 対応するHTTPレスポンスを生成します。
     * (またはレスポンスを生成する他のWebコンポーネントにリクエストをフォワードします。)
     * 併せてビジネスロジックがスローする例外のハンドリングも提供します。
     * コントロールをどこに/どうやってフォワードすべきかが記述された{@link ActionForward}インスタンスもしくは、
     * レスポンスが既に完了している場合は<code>null</code>を返します。</p>
     * {@primary <p>Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it),
     * with provision for handling exceptions thrown by the business logic.
     * Return an {@link ActionForward} instance describing where and how
     * control should be forwarded, or <code>null</code> if the response
     * has already been completed.</p>}
     *
     * @param mapping このインスタンスを選択するために使用するActionMapping
     * {@primary @param mapping The ActionMapping used to select this instance}
     * @param form このリクエストのためのActionForm Bean(もしあれば)
     * {@primary @param form The optional ActionForm bean for this request (if any)}
     * @param request 処理中のHTTPリクエスト
     * {@primary @param request The HTTP request we are processing}
     * @param response 生成するHTTPレスポンス
     * {@primary @param response The HTTP response we are creating}
     *
     * @exception Exception アプリケーションのビジネスロジックが例外をスローした場合
     * {@primary @exception Exception if the application business logic throws
     *  an exception}
     * @since Struts 1.1
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {

        return null;

    }


    // ---------------------------------------------------- Protected Methods


    /**
     * <p>(messages="true"が設定され)メッセージが必要な場合は、
     * &lt;html:messages&gt;タグで使用できるように適切なリクエスト属性に指定されたメッセージキーを追加します。
     * まだ属性が存在しない場合は、属性を初期化します。
     * それ以外の場合は、このリクエスト属性は設定されません。</p>
     * {@primary Adds the specified messages keys into the appropriate request
     * attribute for use by the &lt;html:messages&gt; tag (if
     * messages="true" is set), if any messages are required.
     * Initialize the attribute if it has not already been.
     * Otherwise, ensure that the request attribute is not set.}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request   The servlet request we are processing}
     * @param messages  メッセージオブジェクト
     * {@primary @param messages  Messages object}
     * @since Struts 1.2.1
     */
    protected void addMessages(
        HttpServletRequest request,
        ActionMessages messages) {

        if (messages == null){
            //	bad programmer! *slap*
            return;
        }

        // get any existing messages from the request, or make a new one
        ActionMessages requestMessages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
        if (requestMessages == null){
            requestMessages = new ActionMessages();
        }
        // add incoming messages
        requestMessages.add(messages);

        // if still empty, just wipe it out from the request
        if (requestMessages.isEmpty()) {
            request.removeAttribute(Globals.MESSAGE_KEY);
            return;
        }

        // Save the messages
        request.setAttribute(Globals.MESSAGE_KEY, requestMessages);
    }


    /**
     * <p>メッセージが必要な場合は、
     * &lt;html:messages&gt;タグが使用できるように適切なリクエスト属性に指定されたエラーキーを追加します。
     * まだ属性が存在しない場合は、属性を初期化します。
     * それ以外の場合は、このリクエスト属性は設定されません。</p>
     * {@note "attribute for use by the for use by the" は 
     * "attribute for use by the"が正しいです。 Bug#33876}
     * {@primary Adds the specified errors keys into the appropriate request
     * attribute for use by the for use by the &lt;html:errors&gt; tag,
     * if any messages are required.
     * Initialize the attribute if it has not already been.
     * Otherwise, ensure that the request attribute is not set.}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request   The servlet request we are processing}
     * @param errors エラーオブジェクト
     * {@primary @param errors  Errors object}
     * @since Struts 1.2.1
     */
    protected void addErrors(
        HttpServletRequest request,
        ActionMessages errors) {

        if (errors == null){
            //	bad programmer! *slap*
            return;
        }

        // get any existing errors from the request, or make a new one
        ActionMessages requestErrors = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
        if (requestErrors == null){
            requestErrors = new ActionMessages();
        }
        // add incoming errors
        requestErrors.add(errors);

        // if still empty, just wipe it out from the request
        if (requestErrors.isEmpty()) {
            request.removeAttribute(Globals.ERROR_KEY);
            return;
        }

        // Save the errors
        request.setAttribute(Globals.ERROR_KEY, requestErrors);
    }


    /**
     * <p>各トランザクションに対してリクエストを1回だけにするために使用する、
     * 新しいトランザクショントークンを生成します。</p>
     * {@primary <p>Generate a new transaction token, to be used for enforcing a single
     * request for a particular transaction.</p>}
     *
     * @param request 処理中のリクエスト
     * {@primary @param request The request we are processing}
     */
    protected String generateToken(HttpServletRequest request) {
        return token.generateToken(request);
    }


    /**
     * <p>現在のモジュールのデフォルトのデータソースを返します。</p>
     * {@primary <p>Return the default data source for the current module.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     *
     * @since Struts 1.1
     */
    protected DataSource getDataSource(HttpServletRequest request) {

        return (getDataSource(request, Globals.DATA_SOURCE_KEY));

    }



    /**
     * <p>現在のモジュールの指定されたデータソースを返します。</p>
     * {@primary <p>Return the specified data source for the current module.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @param key <code>&lt;data-sources&gt;</code>要素内でデータソースを特定するためのキー
     * {@note "<code>&lt;message-resources&gt;</code>" は 
     * "<code>&lt;data-sources&gt;</code>"が正しいと思われます(Bug#33876)が、
     * 最新版ではこのメソッドそのものが削除されてしまいました。}
     * {@note 原文が誤っているっぽいので大幅に意訳しました。(高橋)}
     * {@primary @param key The key specified in the
     *  <code>&lt;message-resources&gt;</code> element for the
     *  requested bundle}
     *
     * @since Struts 1.1
     */
    protected DataSource getDataSource(HttpServletRequest request, String key) {

        // Identify the current module
        ServletContext context = getServlet().getServletContext();
        ModuleConfig moduleConfig =
            ModuleUtils.getInstance().getModuleConfig(request, context);

        return (DataSource) context.getAttribute(key + moduleConfig.getPrefix());
    }


    /**
     * <p>直前のアクションでリクエストに置かれた既存のエラーを全て取り出します。
     * <code>Action</code>の開始時に<code>new ActionMessages()</code>という形で生成する代わりに、
     * このメソッドを呼ぶことができます。
     * これにより、saveErrors()メソッドで全ての既存のエラーを消すのを防げます。</p>
     * {@primary Retrieves any existing errors placed in the request by previous actions.  This method could be called instead
     * of creating a <code>new ActionMessages()</code> at the beginning of an <code>Action</code>
     * This will prevent saveErrors() from wiping out any existing Errors}
     *
     * @return リクエストに既に存在するエラー、
     * エラーがない場合は新たなActionMessagesオブジェクト
     * {@primary @return the Errors that already exist in the request, or a new ActionMessages object if empty.}
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @since Struts 1.2.1
     */
    protected ActionMessages getErrors(HttpServletRequest request) {
        ActionMessages errors =
            (ActionMessages) request.getAttribute(Globals.ERROR_KEY);
        if (errors == null) {
            errors = new ActionMessages();
        }
        return errors;
    }


    /**
     * <p>ユーザが現在選択しているLocaleを返します。</p>
     * {@primary <p>Return the user's currently selected Locale.</p>}
     *
     * @param request 処理中のリクエスト
     * {@primary @param request The request we are processing}
     */
    protected Locale getLocale(HttpServletRequest request) {

        return RequestUtils.getUserLocale(request, null);

    }


    /**
     * <p>直前のアクションでリクエストに置かれた既存のメッセージを全て取り出します。
     * <code>Action</code>の開始時に<code>new ActionMessages()</code>という形で生成する代わりに、
     * このメソッドを呼ぶことができます。
     * これにより、saveMessages()メソッドで全ての既存のメッセージを消すことを防ぎます。</p>
     * {@primary Retrieves any existing messages placed in the request by previous actions.  This method could be called instead
     * of creating a <code>new ActionMessages()</code> at the beginning of an <code>Action</code>
     * This will prevent saveMessages() from wiping out any existing Messages}
     *
     * @return リクエストに既に存在するメッセージ、
     * メッセージがない場合新たなActionMessages
     * {@primary @return the Messages that already exist in the request, or a new ActionMessages object if empty.}
     * @param request 処理中のリクエスト
     * {@primary @param request The servlet request we are processing}
     * @since Struts 1.2.1
     */
    protected ActionMessages getMessages(HttpServletRequest request) {
        ActionMessages messages =
            (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
        if (messages == null) {
            messages = new ActionMessages();
        }
        return messages;
    }


    /**
     * <p>現在のモジュールのデフォルトのメッセージリソースを返します。</p>
     * {@primary <p>Return the default message resources for the current module.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @since Struts 1.1
     */
    protected MessageResources getResources(HttpServletRequest request) {

        return ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY));

    }


    /**
     * <p>現在のモジュールの指定されたメッセージリソースを返します。</p>
     * {@primary <p>Return the specified message resources for the current module.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @param key 要求されているメッセージのまとまりに対応する、
     * <code>&lt;message-resources&gt;</code>要素内でメッセージリソースを特定するためのキー
     * {@primary @param key The key specified in the
     *  <code>&lt;message-resources&gt;</code> element for the
     *  requested bundle}
     *
     * @since Struts 1.1
     */
    protected MessageResources getResources(
        HttpServletRequest request,
        String key) {

        // Identify the current module
        ServletContext context = getServlet().getServletContext();
        ModuleConfig moduleConfig =
            ModuleUtils.getInstance().getModuleConfig(request, context);

        // Return the requested message resources instance
        return (MessageResources) context.getAttribute(
            key + moduleConfig.getPrefix());

    }


    /**
     * <p>現在のフォームのキャンセルボタンが押された場合に<code>true</code>を返します。
     * このメソッドは、
     * リクエスト属性<code>Globals.CANCEL_KEY</code>が設定されているかどうかをチェックします。
     * この属性が通常設定されるのは、
     * 現在のリクエストにおいて<strong>CancelTag</strong>が生成したキャンセルボタンをユーザが押した時です。
     * <code>true</code>の場合、
     * <strong>ActionForm</strong>の<code>validate()</code>メソッドによるバリデーションを、
     * コントローラサーブレットはスキップします。</p>
     * {@primary <p>Returns <code>true</code> if the current form's cancel button was
     * pressed.  This method will check if the <code>Globals.CANCEL_KEY</code>
     * request attribute has been set, which normally occurs if the cancel
     * button generated by <strong>CancelTag</strong> was pressed by the user
     * in the current request.  If <code>true</code>, validation performed
     * by an <strong>ActionForm</strong>'s <code>validate()</code> method
     * will have been skipped by the controller servlet.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @see org.apache.struts.taglib.html.CancelTag
     */
    protected boolean isCancelled(HttpServletRequest request) {

        return (request.getAttribute(Globals.CANCEL_KEY) != null);

    }


    /**
     * <p>ユーザの現在のセッションにトランザクショントークンが保持され、
     * かつ、このアクションのリクエストパラメータとして送信された値がそのトークンとマッチする場合は、
     * <code>true</code>を返します。
     * 次のいずれかの場合は<code>false</code>を返します。</p>
     * {@primary <p>Return <code>true</code> if there is a transaction token stored in
     * the user's current session, and the value submitted as a request
     * parameter with this action matches it. Returns <code>false</code>
     * under any of the following circumstances:</p>}
     * <ul>
     * <li>このリクエストに関連付けられたセッションがない場合</li>
     * {@primary <li>No session associated with this request</li>}
     * <li>セッションに保持されたトランザクショントークンがない場合</li>
     * {@primary <li>No transaction token saved in the session</li>}
     * <li>リクエストパラメータの中にトランザクショントークンがない場合</li>
     * {@primary <li>No transaction token included as a request parameter</li>}
     * <li>リクエストの中のトランザクショントークンの値が、
     * ユーザのセッションにあるトランザクショントークンの値と一致しない場合</li>
     * {@primary <li>The included transaction token value does not match the
     *     transaction token in the user's session</li>}
     * </ul>
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     */
    protected boolean isTokenValid(HttpServletRequest request) {

        return token.isTokenValid(request, false);

    }


    /**
     * <p>ユーザの現在のセッションにトランザクショントークンが保持され、
     * かつ、このアクションのリクエストパラメータとして送信された値がそのトークンとマッチする場合は
     * <code>true</code>を返します。
     * 次のいずれかの場合は<code>false</code>を返します。</p>
     * {@note "Returns <code>false</code>."は、
     * "Returns <code>false</code> under any of the following circumstances:"として訳しています。 Bug#33876}
     * {@primary <p>Return <code>true</code> if there is a transaction token stored in
     * the user's current session, and the value submitted as a request
     * parameter with this action matches it. Returns <code>false</code>.</p>}
     * <ul>
     * <li>このリクエストに関連付けられたセッションがない場合</li>
     * {@primary <li>No session associated with this request</li>}
     * <li>セッションに保持されたトランザクショントークンがない場合</li>
     * {@primary <li>No transaction token saved in the session</li>}
     * <li>リクエストパラメータの中にトランザクショントークンがない場合</li>
     * {@primary <li>No transaction token included as a request parameter</li>}
     * <li>リクエストの中のトランザクショントークンの値が、
     * ユーザのセッションにあるトランザクショントークンの値と一致しない場合</li>
     * {@primary <li>The included transaction token value does not match the
     *     transaction token in the user's session</li>}
     * </ul>
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @param reset チェック後にトークンをリセットするか否か?
     * {@primary @param reset Should we reset the token after checking it?}
     */
    protected boolean isTokenValid(HttpServletRequest request, boolean reset) {

        return token.isTokenValid(request, reset);

    }


    /**
     * <p>ユーザのセッションに保持されているトランザクショントークンをリセットします。
     * これは、
     * 次に送信されるリクエストではトランザクショントークンをチェックする必要がないことを示します。</p>
     * {@primary <p>Reset the saved transaction token in the user's session. This
     * indicates that transactional token checking will not be needed
     * on the next request that is submitted.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     */
    protected void resetToken(HttpServletRequest request) {

        token.resetToken(request);

    }


    /**
     * <p>何らかのメッセージが必要な場合、
     * &lt;html:errors&gt;タグが利用できるように、
     * 適切なリクエスト属性に指定されたエラーメッセージキーを保存します。
     * それ以外の場合、
     * リクエスト属性が生成されないようにします。</p>
     * {@primary <p>Save the specified error messages keys into the appropriate request
     * attribute for use by the &lt;html:errors&gt; tag, if any messages
     * are required. Otherwise, ensure that the request attribute is not
     * created.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @param errors エラーメッセージオブジェクト
     * {@primary @param errors Error messages object}
     * @deprecated 代わりにsaveErrors(HttpServletRequest, ActionMessages)を使用して下さい。
     * このメソッドはStruts 1.2.より後で削除されます。
     * {@primary @deprecated Use saveErrors(HttpServletRequest, ActionMessages) instead.
     * This will be removed after Struts 1.2.}
     */
    protected void saveErrors(HttpServletRequest request, ActionErrors errors) {

        this.saveErrors(request,(ActionMessages)errors);
        // :TODO: Remove after Struts 1.2.

    }


    /**
     * <p>何らかのメッセージが必要な場合、
     * &lt;html:errors&gt;タグが利用できるように、
     * 適切なリクエスト属性に指定されたエラーメッセージキーを保存します。
     * それ以外の場合、
     * リクエスト属性が生成されないようにします。</p>
     * {@primary <p>Save the specified error messages keys into the appropriate request
     * attribute for use by the &lt;html:errors&gt; tag, if any messages
     * are required. Otherwise, ensure that the request attribute is not
     * created.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     * @param errors エラーメッセージオブジェクト
     * {@primary @param errors Error messages object}
     * @since Struts 1.2
     */
    protected void saveErrors(HttpServletRequest request, ActionMessages errors) {

        // Remove any error messages attribute if none are required
        if ((errors == null) || errors.isEmpty()) {
            request.removeAttribute(Globals.ERROR_KEY);
            return;
        }

        // Save the error messages we need
        request.setAttribute(Globals.ERROR_KEY, errors);

    }


    /**
     * <p>(messages="true"が設定され)メッセージが必要な場合、
     * &lt;html:messages&gt;タグが使用できるように、
     * 適切なリクエスト属性の中に指定されたメッセージキーを保存します。
     * それ以外の場合、リクエスト属性は設定されません。</p>
     * {@primary <p>Save the specified messages keys into the appropriate request
     * attribute for use by the &lt;html:messages&gt; tag (if
     * messages="true" is set), if any messages are required. Otherwise,
     * ensure that the request attribute is not created.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing.}
     * @param messages 保存するメッセージ
     * <code>null</code>または空のメッセージの場合、
     * リクエスト中に存在する全てのActionMessagesを削除します。
     * {@primary @param messages The messages to save. <code>null</code> or empty
     * messages removes any existing ActionMessages in the request.}
     *
     * @since Struts 1.1
     */
    protected void saveMessages(
        HttpServletRequest request,
        ActionMessages messages) {

        // Remove any messages attribute if none are required
        if ((messages == null) || messages.isEmpty()) {
            request.removeAttribute(Globals.MESSAGE_KEY);
            return;
        }

        // Save the messages we need
        request.setAttribute(Globals.MESSAGE_KEY, messages);
    }


    /**
     * <p>(messages="true"が設定され)メッセージが必要な場合、
     * &lt;html:messages&gt;タグが使用できるように、
     * 適切なリクエスト属性の中に指定されたメッセージキーを保存します。
     * それ以外の場合、セッション属性は生成されません。</p>
     * {@primary <p>Save the specified messages keys into the appropriate session
     * attribute for use by the &lt;html:messages&gt; tag (if
     * messages="true" is set), if any messages are required. Otherwise,
     * ensure that the session attribute is not created.</p>}
     *
     * @param session メッセージを保存するセッション
     * {@primary @param session The session to save the messages in.}
     * @param messages 保存するメッセージ
     * <code>null</code>または空のメッセージの場合、
     * リクエスト中に存在する全てのActionMessagesを削除します。
     * {@primary @param messages The messages to save. <code>null</code> or empty
     * messages removes any existing ActionMessages in the session.}
     *
     * @since Struts 1.2
     */
    protected void saveMessages(
        HttpSession session,
        ActionMessages messages) {

        // Remove any messages attribute if none are required
        if ((messages == null) || messages.isEmpty()) {
            session.removeAttribute(Globals.MESSAGE_KEY);
            return;
        }

        // Save the messages we need
        session.setAttribute(Globals.MESSAGE_KEY, messages);
    }


    /**
     * <p>必要に応じて新たなセッションを生成し、
     * ユーザの現在のセッションに新しいトランザクショントークンを保存します。</p>
     * {@primary <p>Save a new transaction token in the user's current session, creating
     * a new session if necessary.</p>}
     *
     * @param request 処理中のサーブレットリクエスト
     * {@primary @param request The servlet request we are processing}
     */
    protected void saveToken(HttpServletRequest request) {
        token.saveToken(request);
    }


    /**
     * <p><code>HttpSession</code>にユーザが現在選択した<code>Locale</code>を設定します。</p>
     * {@primary <p>Set the user's currently selected <code>Locale</code> into their
     * <code>HttpSession</code>.</p>}
     *
     * @param request 処理中のリクエスト
     * {@primary @param request The request we are processing}
     * @param locale 設定するユーザが選択したLocale、
     * またはサーバーのデフォルトLocaleを選択する場合はnull
     * {@primary @param locale The user's selected Locale to be set, or null
     *  to select the server's default Locale}
     */
    protected void setLocale(HttpServletRequest request, Locale locale) {

        HttpSession session = request.getSession();
        if (locale == null) {
            locale = Locale.getDefault();
        }
        session.setAttribute(Globals.LOCALE_KEY, locale);

    }

}
