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

📄 corerequestprocessor.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.core;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.apache.struts.tiles.TilesRequestProcessor;

import com.maverick.http.HttpClient;
import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.HostService;
import com.sslexplorer.boot.Util;
import com.sslexplorer.navigation.MenuTree;
import com.sslexplorer.navigation.NavigationManager;
import com.sslexplorer.security.Constants;

/**
 * Extension of {@link org.apache.struts.tiles.TilesRequestProcessor} that
 * <strong>all</strong> requests to the struts application pass throught.
 * <p>
 * Here a map of all active session is maintained and a check is made to
 * see if the navigation menus have been constructed.
 * 
 * @author Brett Smith <a href="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.31 $
 */
public class CoreRequestProcessor extends TilesRequestProcessor {

    final static HashMap sessions = new HashMap();
    static CoreRequestProcessor requestProcessor;
    
    /**
     * Constructor.
     *
     */
    public CoreRequestProcessor() {
        super();
        requestProcessor = this;
    }
    
    /**
     * Get a static instance of the request processor.
     * 
     * @return static instance of request processor
     */
    public static CoreRequestProcessor getRequestProcessor() {
        return requestProcessor;
    }

    /**
     * Get a map of all sessions
     * 
     * @return map of all sessions
     */
    public static Map getSessions() {
        return sessions;
    }

    /* (non-Javadoc)
     * @see org.apache.struts.action.RequestProcessor#process(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        if (session.getAttribute(Constants.SESSION_HOOK) == null) {

            // Redirect to a valid host if not in setup mode and the feature is
            // in use
            if (!ContextHolder.getContext().isSetupMode()) {
                String validExternalHosts;
                String action;
                try {
                    action = CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null,
                        "webServer.invalidHostnameAction");
                    validExternalHosts = CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null,
                                    "webServer.validExternalHostnames");
                } catch (Exception e) {
                    log.error("Could not determine valid hosts.", e);
                    throw new ServletException("Could not determine valid hosts.", e);
                }
                if (!validExternalHosts.equals("")) {
                    StringTokenizer t = new StringTokenizer(validExternalHosts, "\n");
                    String host = request.getHeader("Host");
                    HostService hostService = host == null ? null : new HostService(host);
                    boolean hostOk = false;
                    String firstHost = t.nextToken();
                    if (hostService != null && !hostService.getHost().equals("")) {
                        if (hostService.getHost().startsWith("activeproxy")) {
                            int idx = hostService.getHost().indexOf(".");
                            hostService.setHost(hostService.getHost().substring(idx + 1));
                        }
                        String thisHost = firstHost;
                        do {
                            if (hostService.getHost().equals(thisHost)) {
                                hostOk = true;
                            } else {
                                if (t.hasMoreTokens()) {
                                    thisHost = t.nextToken();
                                }
                            }
                        } while (!hostOk && t.hasMoreTokens());
                    }
                    if (!hostOk) {
                        if(action.equals("redirect")) {
                            String path = (request.isSecure() ? "https" : "http") + "://" + firstHost;
                            if (ContextHolder.getContext().getPort() != 443) {
                                path += ":" + ContextHolder.getContext().getPort();
                            }
                            path += Util.getOriginalRequest(request);
                            session.invalidate();
                            response.sendRedirect(path);
                            return;
                        }
                        else if(action.equals("error")) {
                            response.sendError(HttpServletResponse.SC_NOT_FOUND);
                            return;
                        }
                        else if(action.equals("disconnect")) {
                            response.getOutputStream().close();
                            return;
                        }
                    }
                }
            }
            else {
                // We should never timeout during setup / installation
                session.setMaxInactiveInterval(Integer.MAX_VALUE);
            }

            CoreServlet.getServlet().fireCoreEvent(new NewHTTPSessionEvent(this, request, response));
            sessions.put(session.getId(), session);
            session.setAttribute(Constants.SESSION_HOOK, new HttpSessionBindingListener() {

                public void valueBound(HttpSessionBindingEvent arg0) {
                }

                public void valueUnbound(HttpSessionBindingEvent arg0) {
                    sessions.remove(arg0.getSession().getId());
                }

            });
            
            
        }
        
        // Get any page tasks for this page
        String servletPath = request.getServletPath();
        if (servletPath.startsWith("/") && servletPath.endsWith(".do")) {
            servletPath = servletPath.substring(1, servletPath.length() - 3);
            MenuTree pageTaskMenuTree = NavigationManager.getMenuTree(PageTaskMenuTree.PAGE_TASK_MENU_TREE);
            MenuItem pageTasks = pageTaskMenuTree.getMenuItem(servletPath);
            if (pageTasks != null) {
                session.setAttribute(Constants.PAGE_TASKS, pageTaskMenuTree.rebuildMenus(pageTasks, request));
            } else {
                session.removeAttribute(Constants.PAGE_TASKS);
            }
        } else {
            session.removeAttribute(Constants.PAGE_TASKS);
        }

        try {
            super.process(request, response);
        } catch (ServletException se) {
            /*
             * TODO This hack is so we can redirect to the logon page if the
             * user tries to commit a form after their session has timed out.
             * Find a better way
             */
            if (se.getMessage() != null && se.getMessage().indexOf("BeanUtils.populate") != -1) {
                log.error("User probably commited a form after their session had timed out.", se);
                log.error("Cause.", se.getRootCause());
                request.getSession().getServletContext().getRequestDispatcher("/showHome.do").forward(request, response);
            } else {
                throw se;
            }
        }
    }
}

⌨️ 快捷键说明

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