view gnv/src/main/java/de/intevation/gnv/action/ArtifactDatabaseActionBase.java @ 1022:28a0628b11b0

Added license file and license header. gnv/trunk@1258 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:15:08 +0000
parents 33198e55371c
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.action;

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

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Basicimplemantation for all Actions which should serv request for
 * the GNV.
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public class ArtifactDatabaseActionBase extends Action {

    /**
     * The id of the action which should used if the execution
     * of the action was successful.
     */
    protected final static String SUCCSESS_FORWARD_ID = "success";
    
    /**
     * The id of the action which should be used if an exception has occurred
     * during the execution. 
     */
    protected final static String EXCEPTION_FORWARD_ID = "success";

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger
            .getLogger(ArtifactDatabaseActionBase.class);

    /**
     * Constructor
     */
    public ArtifactDatabaseActionBase() {
        super();
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        log.debug("ArtifactDatabaseActionBase.execute");
        ActionForward forward = mapping.findForward(SUCCSESS_FORWARD_ID);
        return forward;
    }

    /**
     * Returns the Action that should be used if an exception has occurred.
     * @param mapping the mapping which holds all available Actions
     * @return the Action that should be used.
     */
    protected ActionForward getExceptionForward(ActionMapping mapping) {
        log.debug("ArtifactDatabaseActionBase.getExceptionForward");
        ActionForward lForward = mapping.findForward(EXCEPTION_FORWARD_ID);
        return lForward;
    }

    /**
     * Encodes the <code>String</code> to prevent cross-site-scripting
     * @param s the string that should be encoded
     * @return the encoded string
     */
    protected String encode(String s) {
        log.debug("String to encode: " + s);
        s = s.replaceAll("<", "&lt;");
        s = s.replaceAll(">", "&gt;");
        s = s.replaceAll("\"", "&quot;");
        s = s.replaceAll("&", "&amp;");

        log.debug("Encoded string: " + s);
        return s;
    }

    /**
     * Encodes the <code>StringArray</code> to prevent cross-site-scripting
     * @param s the stringarray that should be encoded
     * @return the encoded stringarray
     */
    protected String[] encode(String[] s) {
        if (s == null)
            return null;

        String[] good = new String[s.length];
        for (int i = 0; i < good.length; i++) {
            good[i] = encode(s[i]);
        }

        return good;
    }


    protected boolean isSessionExhausted(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return session.isNew();
    }


    protected ActionForward sessionExhaustedForward(
        ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
        request.setAttribute(
            CommunicationKeys.REQUEST_EXCEPTION_SESSION_ID,
            "SessionTimeout has occured");

        log.warn("Session timed out.");

        return new FetchArtifactFactoriesAction().execute(
            mapping, form, request, response);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org