confirmaction.java

来自「这是linux下ssl vpn的实现程序」· Java 代码 · 共 202 行

JAVA
202
字号
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.navigation.actions;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.sslexplorer.boot.Util;
import com.sslexplorer.core.actions.DefaultAction;
import com.sslexplorer.navigation.Option;
import com.sslexplorer.navigation.forms.ConfirmForm;
import com.sslexplorer.security.Constants;
import com.sslexplorer.security.SessionInfo;

public class ConfirmAction extends DefaultAction {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {

        //	Get all of the options from the resource bundle
        List options = new ArrayList();
        MessageResources resources = null;

        String bundle = request.getParameter("bundle");
        String propertyPrefix = request.getParameter("propertyPrefix");
        String type = request.getParameter("type");
        if (type == null) {
            type = "message";
        }
        String arg0 = request.getParameter("arg0");
        boolean decorated = true;
        String align = "center";

        //    parameter
        // ="administration,deletePropertyProfile,yes=/updatePropertyProfile.do?action=delete,no="
        if (mapping.getParameter() != null && !mapping.getParameter().equals("")) {
            StringTokenizer t = new StringTokenizer(mapping.getParameter(), ",");
            if (t.hasMoreTokens()) {
                type = t.nextToken();
            }
            
            // TODO this is a hack to get the extension store agreement aligning left, sort it out properly! 
            if(type.startsWith("align=")) {
                int idx = type.indexOf('=');
                align = type.substring(idx + 1);
                type = t.nextToken();
            }
            
            if (t.hasMoreTokens()) {
                decorated = t.nextToken().equalsIgnoreCase("true");
            }
            if (t.hasMoreTokens()) {
                bundle = t.nextToken();
            }
            if (t.hasMoreTokens()) {
                propertyPrefix = t.nextToken();
            }
            resources = getResources(request, bundle);
            if(resources == null) {
                throw new Exception("Could not find resource bundle " + bundle);
            }
            while (t.hasMoreTokens()) {
                String option = t.nextToken();
                options.add(getOption(option, propertyPrefix, request, resources));
            }
        } else {
            resources = getResources(request, bundle);
            if(resources == null) {
                throw new Exception("Could not find resource bundle " + bundle);
            }
            String[] optionValues = request.getParameterValues("option");
            if (options != null) {
                for (int i = 0; i < optionValues.length; i++) {
                    options.add(getOption(optionValues[i], propertyPrefix, request, resources));
                }

            }
        }

        //	Get the title text and the description
        String title = resources.getMessage(propertyPrefix + ".title");
        String subtitle = resources.getMessage(propertyPrefix + ".subtitle");
        String message = resources.getMessage(propertyPrefix + ".message", arg0);

        // Initialise the form
        ConfirmForm confirmForm = (ConfirmForm) form;
        confirmForm.initialize(type, title, subtitle, message, options, decorated, align, arg0);

        // If this confirmation is the result on an exception then build up the
        // exception text
        Throwable exception = type.equals(ConfirmForm.TYPE_EXCEPTION) ? (Throwable) request.getSession().getAttribute(Constants.EXCEPTION) : null;
        request.getSession().removeAttribute(Constants.EXCEPTION);
        if (exception != null) {
            StringBuffer mesgBuf = new StringBuffer();
            StringBuffer traceBuf = new StringBuffer();
            Throwable ex = exception;
            while (ex != null) {
                String mesg = ex.getMessage();
                if (mesg != null) {
                    mesg = mesg.trim();
                    if (!mesg.endsWith(".")) {
                        mesg += ".";
                    }
                    if (mesgBuf.length() == 0) {
                        mesgBuf.append('\n');
                    }
                    mesgBuf.append(mesg);
                }
                StringWriter sw = new StringWriter();
                ex.printStackTrace(new PrintWriter(sw));
                if (traceBuf.length() == 0) {
                    traceBuf.append("\n");
                }
                traceBuf.append(sw.toString());
                ex = ex.getCause();
            }
            confirmForm.setExceptionMessage(mesgBuf.toString());
            confirmForm.setTraceMessage(traceBuf.toString());
        }

        return mapping.findForward("success");
    }

    public Option getOption(String optionText, String propertyPrefix, HttpServletRequest request, MessageResources resources) {
        
        /* If the path for the forward for this option ends with a !, then dont
         * append parameters. This is used in places like the logon screen where
         * you would not want parameters such as password, PIN etc to be passed
         * back after an exception
         */
        boolean includeParameters = true;
        if(optionText.endsWith("!")) {
            optionText = optionText.substring(0, optionText.length() - 1);
            includeParameters = false;
        }

        int idx = optionText.indexOf('=');
        String name = optionText;
        String forward = null;
        if (idx != -1) {
            forward = name.substring(idx + 1);
            name = name.substring(0, idx);
        }

        StringBuffer buf = new StringBuffer(forward);
        if (includeParameters && !forward.equals("")) {
            // Build up the URL to forard to
            for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
                String paramName = (String) e.nextElement();
                String[] paramVals = request.getParameterValues(paramName);
                for (int i = 0; i < paramVals.length; i++) {
                    if (buf.length() == forward.length() && forward.indexOf('?') == -1) {
                        buf.append("?");
                    } else {
                        buf.append("&");
                    }
                    buf.append(Util.urlEncode(paramName));
                    buf.append("=");
                    buf.append(Util.urlEncode(paramVals[i]));
                }
            }
        }

        String label = resources.getMessage(propertyPrefix + ".option." + name);
        return new Option(buf.toString(), label, name);

    }

    public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return SessionInfo.ALL_CONTEXTS;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?