⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getapplicationaction.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  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.extensions.actions;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

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

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;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;

import com.sslexplorer.core.CoreAttributeConstants;
import com.sslexplorer.core.CoreEvent;
import com.sslexplorer.core.CoreEventConstants;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.core.actions.XMLOutputAction;
import com.sslexplorer.extensions.ApplicationShortcut;
import com.sslexplorer.extensions.ExtensionDescriptor;
import com.sslexplorer.extensions.store.ExtensionStore;
import com.sslexplorer.policyframework.Policy;
import com.sslexplorer.policyframework.ResourceAccessEvent;
import com.sslexplorer.security.Constants;
import com.sslexplorer.security.InvalidTicketException;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.security.User;
import com.sslexplorer.security.VPNSession;

public class GetApplicationAction extends XMLOutputAction {

    final public static Log log = LogFactory.getLog(GetApplicationAction.class);

    public GetApplicationAction() {
    }

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

        request.setAttribute(Constants.REQ_ATTR_COMPRESS, Boolean.FALSE);
        String application = request.getParameter("name");
        String ticket = request.getParameter("ticket");
        ApplicationShortcut shortcut = null;
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            shortcut = CoreServlet.getServlet().getSystemDatabase().getShortcut(id);
        }

        Properties properties = new Properties();
        int type = VPNSession.UNKNOWN_VPN_CLIENT;
        for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            String val = request.getParameter(name);
            if (name.equals("ticket") || name.equals("id") || name.equals("name")) {
                continue;
            } else {
                properties.put(name, val);
            }
        }
        processApplicationRequest(application, ticket, request, response, shortcut, properties);

        return null;
    }

    protected void processApplicationRequest(String application, String ticket, HttpServletRequest request,
                    HttpServletResponse response, ApplicationShortcut shortcut, Properties properties) throws JDOMException, Exception {

        VPNSession vpnSession = null;
        try {
            
            /**
             * The user must have a valid VPN session to obtain the
             * application descriptor
             */
            vpnSession = CoreServlet.getServlet().getLogonController().getPendingVPNSession(ticket);
            if(vpnSession == null) {
                vpnSession = CoreServlet.getServlet().getLogonController().getVPNSessionByTicket(ticket);
            }     
            User user = null;
            if(vpnSession == null) {
                SessionInfo info = CoreServlet.getServlet().getLogonController().getAuthorizationTicket(ticket);
                if(info==null)
                    throw new Exception("The ticket provided was invalid");
                user = info.getUser();
            }
            else {
                user = vpnSession.getSessionInfo().getUser();
            }  
            if(application.equals("VPN Client")) {
                    sendAgentDescriptor(request, response, properties, user);
            } else {
                ExtensionDescriptor des = ExtensionStore.getInstance().getExtensionDescriptor(application);
                try {
                    if (ExtensionStore.getInstance().isExtensionLoaded(application)) {
                        des = ExtensionStore.getInstance().getExtensionDescriptor(application);
                        if(des == null) {
                            throw new Exception("No application named "  + des);
                        }
                        sendApplicationDescriptor(des, request, response, shortcut, properties);
                    }
                } catch (Exception e) {
                    throw e;
                }
            }
        } catch (Throwable t) {
            log.error("Failed to process request for application.", t);
            sendError(t.getMessage() == null ? "<null>" : t.getMessage(), response);
        }
    }

    private void sendApplicationDescriptor(ExtensionDescriptor app, HttpServletRequest request, HttpServletResponse response,
                    ApplicationShortcut shortcut, Properties properties) throws JDOMException, IOException, UndefinedParameterException {
        response.setHeader("Content-type", "text/xml");
        byte[] xml = processParameters(request, app.getDescriptorElement(), app, shortcut).getBytes();
        response.setContentLength(xml.length);
        response.getOutputStream().write(xml);
        response.getOutputStream().close();
    }

    private void sendAgentDescriptor(HttpServletRequest request, HttpServletResponse response, Properties properties, User user) throws JDOMException, IOException, UndefinedParameterException, InvalidTicketException {
        
        if(log.isInfoEnabled())
            log.info("Sending agent descriptor");
        
        /**
         * We do this a bit differently because we may need to dynamically change the agent descriptor
         * to support other agent extensions. To do this we get the XML element seperately from the 
         * application. This may contain an <agents> element that has been dynamically created for the
         * user.
         */
        ExtensionDescriptor app = ExtensionStore.getInstance().getAgentApplication();

        Element root = ExtensionStore.getInstance().getAgentDescriptor(user, properties );
        response.setHeader("Content-type", "text/xml");
        byte[] xml = processParameters(request, root, app, null).getBytes();
        response.setContentLength(xml.length);
        response.getOutputStream().write(xml);
        response.getOutputStream().close();
    }
    
    private String processParameters(HttpServletRequest request, Element element, ExtensionDescriptor app, final ApplicationShortcut shortcut)
                    throws UndefinedParameterException, JDOMException, IOException {

        SessionInfo sessionInfo;
        if (CoreServlet.getServlet().getLogonController().verifyPendingVPNAuthorization(request)) {
            sessionInfo = CoreServlet.getServlet().getLogonController().getPendingVPNSession(request).getSessionInfo();
        } else {
            VPNSession session = CoreServlet.getServlet().getLogonController().getVPNSession(request);
            if(session!=null)
                sessionInfo = session.getSessionInfo();
            else {
                sessionInfo = CoreServlet.getServlet().getLogonController().getAuthorizationTicket(request.getParameter("ticket"));
                
                if(sessionInfo==null)
                    throw new IOException("Invalid request!");
            }

        }

        List params = element.getChildren("parameter");

        for (Iterator it = params.iterator(); it.hasNext();) {
            Element p = (Element) it.next();

            String name = p.getAttribute("name").getValue();
            String value = request.getParameter(name);

            if (value == null && shortcut != null) {
                value = (String) shortcut.getParameters().get(name);
            }

            if (value == null) {
                value = app.getParameterDefinition(name).getDefaultValue();
                if (value == null || value.equals(ExtensionDescriptor.UNDEFINED_PARAMETER)) {
                    throw new UndefinedParameterException("Parameter " + name + " is undefined");
                }
            }

            value = CoreUtil.doStandardReplacements(app, null, value);
            value = CoreUtil.doStandardReplacements(sessionInfo, value);
            value = CoreUtil.doStandardReplacements(request, value);

            p.setAttribute("value", value);
        }

        XMLOutputter output = new XMLOutputter();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        output.output(element, out);

        String xml = new String(out.toByteArray());

        params = element.getChildren("parameter");

        //
        String vpnTicket = request.getParameter("ticket");

        Map parameters = new HashMap();
        for (Iterator it = params.iterator(); it.hasNext();) {
            Element p = (Element) it.next();
            parameters.put(p.getAttributeValue("name"), p.getAttributeValue("value"));
        }

        String processed = CoreUtil.doStandardReplacements(app, parameters, xml);
        processed = CoreUtil.doStandardReplacements(sessionInfo, processed);
        processed = CoreUtil.doStandardReplacements(request, processed);
        if (log.isDebugEnabled())
            log.debug("Returning '" + processed + "'");
        return processed;
    }

}

⌨️ 快捷键说明

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