jetspeedwebpageservice.java

来自「jetspeed源代码」· Java 代码 · 共 625 行 · 第 1/2 页

JAVA
625
字号
/*
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.jetspeed.services.webpage;

// java.io
import java.io.IOException;

// java.util
import java.util.Collection;
import java.util.Iterator;

// javax.servlet
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// java.net
import java.net.URL;
import java.util.HashMap;
import java.net.MalformedURLException;

import org.apache.log4j.Logger;

/**
 *
 * <p>This is the default implementation of the <code>WebPageService</code> interface.</p>
 *
 * <p>
 * It is a service that provides Web Page facade and delegation
 * services for clients to transparently access resources existing on web
 * pages from requests originating from the portal server.</p>
 *
 * <p>Since the WebPage service is giving the appearance of a single session to 
 * the client, the service needs to manage the synchronization of sessions,
 * including single-sign-on, and security authorization permissions between the
 * the portal server and one or more sites.</p> 
 *
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @version $Id: JetspeedWebPageService.java,v 1.4 2004/02/23 03:46:26 jford Exp $ 
 */

public class JetspeedWebPageService
    implements WebPageService
{
    // the session keys used to store network element proxy sessions
    public final static String SESSION_MAP = "wps.SessionMap";
    public final static String URL_SESSION_MAP = "wps.URLSessionMap";

    public final static String INIT_PROPERTIES_PARAM = "properties";

    /**
     * service state
     *
     */

    // the name of the host that this server is running on
    private String host = null;       

    // cache of sites cached and managed by the Proxy.
    // objects are of type org.apache.jetspeed.services.httpProxy.Site
    private HashMap sites = new HashMap();

    // active sessions that the Proxy is working with
    // the objects are of type org.apache.jetspeed.services.httpProxy.SessionMap
    // this cache is updated on servlet unbound events
    private HashMap sessions = new HashMap();

    // has this service been initialized yet
    private boolean init = false;

    // the log file singleton instance
    static Logger log = Logger.getLogger(JetspeedWebPageService.class);

    // last error
    private String lastError = "Jetspeed WebPage Service has not been initialized.";

    /**
     * The primary method invoked when the a Jetspeed GET is executed.
     *
     * @param servlet the Servlet.     
     * @param request Servlet request.
     * @param response Servlet response.
     * @exception IOException a servlet exception.
     * @exception ServletException a servlet exception.
     */
    public void get(HttpServlet servlet,
                    HttpServletRequest request, 
                    HttpServletResponse response)                         
    throws ServletException, IOException
    {
        ProxyRunData rundata = new ProxyRunData(servlet, request, response, false);
        dispatch(rundata);
    }

    /**
     * The primary method invoked when the a Jetspeed POST is executed.
     *
     * @param servlet the Servlet.          
     * @param request Servlet request.
     * @param response Servlet response.
     * @exception IOException a servlet exception.
     * @exception ServletException a servlet exception.
     */    
    public void post(HttpServlet servlet,
                     HttpServletRequest request, 
                     HttpServletResponse response)
    throws ServletException, IOException
    {        
        ProxyRunData rundata = new ProxyRunData(servlet, request, response, true);
        dispatch(rundata);
    }


    /**
     * The common dispatcher for both GETs and PUTs
     *
     * @param data the request specific state.
     * @exception IOException a servlet exception.
     * @exception ServletException a servlet exception.
     */    
    private void dispatch(ProxyRunData data)
    throws ServletException, IOException
    {        
        // Turn this on for Debugging
        //HttpProxyDebug.snoopParams(data.getRequest(), System.err);
        //HttpProxyDebug.snoopHeaders(data.getRequest(), System.err);        

        // get the proxy host for this server
        getHost(data.getRequest());

        //
        // get the full Network Element IP and Resource parameters from request
        //
        Configuration config = Configuration.getInstance();
        String sid = data.getRequest().getParameter(config.getSID());
        String url = data.getRequest().getParameter(config.getURL());

        if (null == sid)
        {
            if (null == url)
            {
                throw new ServletException("Bad Request. No proxy-parameters passed in request.");
            }
            proxyByURL(url, data);
            return;
        }

        // 
        // found the proxy query parameter denoting proxy-by-network-element-id
        //

        // maps a Site unique id to a Site object
        Site site = getSite(sid);
        if (null == site)
        {
            // logon failed, return error screen here:
            throw new ServletException("The Requested Site ID is currently not configured on this system: " + sid);
        }

        // 
        // check the status of the site
        // if it isn't online, exit out with exception
        //        
        if (site.getStatus() != Configuration.STATUS_ONLINE)
        {
            throw new ServletException("The Requested Site (" 
                                       + site.getURL()
                                       + ") is not available. Status = "
                                       + WebPageHelper.getAvailabilityStatus(site.getStatus()) );            
        }

        // 
        // get the path to the requested resource
        //
        String resource = getResourcePath(site.getURL(), data.getRequest());

        boolean loggedOn = true;


        // get the ession Map for this Portal Session
        // we get the session with 'false' since we don't want to create a new
        // session. The session should be already created 
        HttpSession session = data.getRequest().getSession(false);
        if (null == session)
        {
            session = data.getRequest().getSession(true);
        }

        // 
        // look up the session map from the current servlet session
        //
        String sessionID = session.getId();
        SessionMap smap = (SessionMap)sessions.get(sessionID);
        SiteSession jss = null;
        if (null == smap)
        {
            // get the user from the session
            /*
            User user = (User)session.getAttribute(config.getUserSessionKey());
            String username;
            if (user != null)
            {
                username = user.getUserName();
            }
            */
            // it wasn't found, create a new map 
            String username = "";
            smap = new SessionMap(sessionID, username);


            // add the map to the servlet session for callbacks on unbound
            session.setAttribute(SESSION_MAP, smap);
            // add the map to my collection of sessions
            sessions.put(sessionID, smap);

            // and create the network element session
            jss = new JetspeedSiteSession(site, this.host, username);

            // and then put the session into the network element map
            smap.put(site.getURL(), jss);

            // always logon when creating a new session
            loggedOn = jss.logon(data);

        } else
        {
            // found the session map, lets get the session
            jss = (JetspeedSiteSession)smap.get(site.getURL());
            if (null == jss)
            {
                // get the user from the session
                /*
                User user = (User)session.getAttribute(config.getUserSessionKey());
                String username;
                if (user != null) 
                {
                    username = user.getUserName();
                }
                */

                // no session exists, so create one
                String username = "";
                jss = new JetspeedSiteSession(site, this.host, username);
                smap.put(site.getURL(), jss);        

                // and then always logon when creating a new ne session 
                loggedOn = jss.logon(data);
            }
        }        
        if (loggedOn)
        {
            // debug TODO: remove this eventually
            if (data.getRequest().getParameter("logon-test") != null)
                return;

            if (WebPageCache.isCacheableResource(resource))
            {                
                if (WebPageCache.getResourceFromCache(resource, site.getID(), site.getURL(), this.host, data))
                {
                    smap.incCacheCount();
                    jss.incCacheCount();
                }
                else
                {
                    smap.incHitCount();
                    jss.incHitCount();
                }
                return;
            }

            smap.incHitCount();
            jss.incHitCount();
            jss.proxy(resource, data);        
        }
    }

    /**
     * Builds the proxy url which is used when rewriting other URLs
     * 
     *
     * @param req Servlet request.
     */
    private void getHost(HttpServletRequest request)
    {
        // TODO: try to get this to work!        
        // URL.setURLStreamHandlerFactory(new sun.net.www.protocol.http.handler( );

        if (null != this.host)
            return;

        StringBuffer root = new StringBuffer();
        String scheme = request.getScheme();
        root.append(scheme);
        root.append( "://");
        int port = request.getServerPort();

⌨️ 快捷键说明

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