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

Java tutorial

Introduction

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

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

import jp.terasoluna.fw.exception.SystemException;
import jp.terasoluna.fw.util.FileUtil;

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;

/**
 * <p>ZbVfBNg??ANV?B</p>
 *
 * <p>
 * T?[oTCh??PDFt@Ci[fBNg
 * ?i?~?AZbVfBNg?j?OI?[U???B<br>
 * <br>
 * @\gVXe?v?peBt@C?isystem.properties?j
 * ZbVfBNgx?[XpXL?qKv?B<br>
 * <br>
 * <h5>VXe?v?peBt@C?isystem.properties?j?</h5>
 * <code><pre><strong>session.dir.base=/tmp/sessions</strong></pre></code>
 * ZbVfBNg??@p\?B<br>
 * <br>
 * ?iP?j{@link MakeSessionDirectoryAction}NXp<br>
 * ?OI?MakeSessionDirectoryActionJ?B<br>
 * struts-config.xmlactionv?peBparameter?w?tH??[h?B<br>
 * struts-config.xmlyBean`t@C?L?B
 * <h5>struts-config.xml?</h5>
 * <code><pre>
 * &lt;action path="<strong>/makeSessionDir</strong>"
 *     scope="session"
 *     parameter="/foo.jsp"&gt;;
 * &lt;/action&gt;
 * </pre></code>
 * <h5>Bean`t@C?</h5>
 * <code><pre>
 * &lt;bean name="<strong>/makeSessionDir</strong>" scope="prototype"
 *   class="<strong>jp.terasoluna.fw.web.struts.actions.MakeSessionDirectoryAction</strong>"&gt;
 * &lt;/bean&gt;
 * </pre></code>
 *
 * ?iQ?jANVNX?ZbVfBNg??<br>
 * MakeSessionDirectoryActionOANV?A?[U?OC
 * {@link javax.servlet.http.HttpSession}\??
 * FileUtil#makeSessionDirectory(String sessionId)?oKv?B<br>
 * <br>
 * <br>
 * ZbVfBNg??@p\?B<br>
 * <br>
 * ?iP?j{@link javax.servlet.http.HttpSessionListener}NXp<br>
 * fv?C?gfBXNv^?iweb.xml?j
 * HttpSessionListenerNXXio^?A
 * ZbVjZbVfBNg????
 * ?iFileUtil#removeSessionDirectory(String sessionId)?j
 * Xi?B<br>
 * web.xml?L?B<br>
 * <h5>fv?C?gfBXNv^?iweb.xml?j?</h5>
 * <code><pre>
 * &lt;web-app&gt;
 *   ?E?E?E
 *   &lt;listener&gt;
 *     &lt;listener-class&gt;
 *       <strong>jp.terasoluna.fw.web.MyHttpSessionListener</strong>
 *     &lt;/listener-class&gt;
 *   &lt;/listener&gt;
 * &lt;/web-app&gt;
 * </pre></code>
 * </p>
 *
 */
public class MakeSessionDirectoryAction extends ActionEx {

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

    /**
     * ZbVsG?[R?[h?B
     */
    private static final String SESSION_NOT_FOUND_ERROR = "error.session.not.found";

    /**
     * G?[y?[W?i404?jJsG?[R?[h?B
     */
    private static final String FORWARD_ERRORPAGE_ERROR = "error.forward.errorpage";

    /**
     * <p>
     *  ZbVfBNg???B<br>
     *  ZbV???AZbVfBNg??
     *  p??[^??J?ANVtH??[hZbg?B<br>
     *  p??[^?????A(404)G?[?B<br>
     *  ZbV???AVXeON?B
     * </p>
     * @param mapping ANV}bsO
     * @param form ANVtH?[
     * @param req HTTPNGXg
     * @param res HTTPX|X
     * @return J?ANVtH??[h?B
     */
    @Override
    public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest req,
            HttpServletResponse res) {

        if (log.isDebugEnabled()) {
            log.debug("doExecute() called.");
        }

        // pZbVON
        HttpSession session = req.getSession(false);
        if (session == null) {
            log.error("HttpSession is not available.");
            throw new SystemException(null, SESSION_NOT_FOUND_ERROR);
        }

        // ZbVID?AZbVfBNg???B
        FileUtil.makeSessionDirectory(session.getId());

        // p??[^??itH??[h??j
        String path = mapping.getParameter();

        if (path == null) {
            // p??[^?????A(404)G?[p
            try {
                res.sendError(HttpServletResponse.SC_NOT_FOUND);
            } catch (IOException e) {
                log.error("Error page(404) forwarding failed.");
                throw new SystemException(e, FORWARD_ERRORPAGE_ERROR);
            }
            return null;
        }

        // J
        ActionForward retVal = new ActionForward(path);
        return retVal;
    }
}