jetspeedwebpageservice.java

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

JAVA
625
字号
        String hostname = request.getServerName();
        String ip = WebPageHelper.getIP(hostname);
        if (null == ip)
            root.append(hostname);
        else
            root.append(ip);

        if ( (port > 0) &&
             ((scheme.equals("http") && port != 80) ||
              (scheme.equals("https") && port != 443)
             )
           )
        {
            root.append(":");
            root.append(port);
        }
        root.append( request.getServletPath() );
        this.host = root.toString();
    }

    /**
     * Given a Site id, maps to base URL for that site and returns the Site object
     * 
     *
     * @param sid the string Site ID
     * @return the Site object.
     */
    public Site getSite(String sid) throws ServletException
    {
        return(Site)sites.get(sid);
    }

    /**
     * Creates the full path the requested resource on the site from a relative path in request.
     *
     * @param url the base url for the site.
     * @param request the Servlet request.
     * @return the full path to the resource.
     */
    public String getResourcePath(String url, HttpServletRequest request)
    {
        String path = request.getParameter(Configuration.getInstance().getPath());           
        if (path == null)
            return "";

        String fullPath = WebPageHelper.concatURLs(url, path);

        return fullPath.replace('@', '&');
    }


    /**
     *  Given a URL, begin a Jetspeed session with that host
     *  It is here for future use.
     *
     * @param url the URL of the host to proxy.
     * @param data the runData
     * @return a new session
     */
    private SiteSession proxyByURL(String url,
                                      ProxyRunData data)
    throws ServletException, IOException
    {
        String newURL = url.replace('@', '&');
        String base = getTargetBase(newURL);

        // get the Session Map for this 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);
        }

        String sessionID = session.getId();
        SessionMap smap = (SessionMap)sessions.get(sessionID);

        SiteSession pxSession = null;
        if (null == smap)
        {
            // create the map 
            smap = new SessionMap(sessionID, "NA"); // username not relevant....

            session.setAttribute(URL_SESSION_MAP, smap);
            sessions.put(sessionID, smap);

            Site site = new SecuredSite(base, base);
            pxSession = new JetspeedSiteSession(site, base, this.host);
            smap.put(base, pxSession);  // map(targetBaseHostName, Session)
        } 
        else
        {
            pxSession = (JetspeedSiteSession)smap.get(base);
            if (null == pxSession)
            {
                Site site = new SecuredSite(base, base);
                pxSession = new JetspeedSiteSession(site, base, this.host);
                smap.put(base, pxSession);  // map(targetBaseHostName, Session)
            }
        }

        if (WebPageCache.isCacheableResource(newURL))
        {

            if (WebPageCache.getResourceFromCache(newURL, -1, base, this.host, data))
            {
                smap.incCacheCount();
                pxSession.incCacheCount();
            }
            else
            {
                smap.incHitCount();
                pxSession.incHitCount();
            }
            return (JetspeedSiteSession)pxSession;
        }

        smap.incHitCount();
        pxSession.incHitCount();
        pxSession.proxy(newURL, data);
        return(JetspeedSiteSession)pxSession;       
    }

    /**
     * Maps a full URL path to a resource to a base path
     *   given: http://localhost:8080/jetspeed/search/index.html
     *   returns: http://localhost:8080/jetspeed/
     *
     * @param url the full URL of the resource.
     * @return the base host application string
     */
    public String getTargetBase(String url) throws ServletException
    {
        try
        {
            URL u = new URL(url);
            StringBuffer base = new StringBuffer();
            String protocol = u.getProtocol();
            base.append(protocol);
            base.append( "://");
            int port = u.getPort();
            base.append(u.getHost());  
            if ( (port > 0) &&
                 ((protocol.equals("http") && port != 80) ||
                  (protocol.equals("https") && port != 443)
                 )
               )
            {
                base.append(":");
                base.append(port);
            }

            // we need to separate the filename from the resource, since
            // URL.getPath() and .getFile() return the same string
            String path = u.getFile();

            if (null != path)
            {

                int dot = path.lastIndexOf('.');
                int slash = path.lastIndexOf('/');
                if (dot > slash && slash != -1)
                { // its a file
                    path = path.substring(0, slash);
                }
                // 

                base.append(path);

                if ('/' != base.charAt(base.length()-1))
                    base.append('/');
            } else
                base.append("/");

            return base.toString();
        } catch (MalformedURLException e)
        {
            throw new ServletException(e.toString());
        }
    }


    /**
     * One time initialization of the proxy service
     *
     * @param config the servlet configuration.     
     * @exception IOException a servlet exception.
     * @exception ServletException a servlet exception.
     */
    public boolean init(ServletConfig config)
    throws ServletException, IOException
    {

        String paramFile = config.getInitParameter(INIT_PROPERTIES_PARAM);
        if (null == paramFile)
        {
            lastError = "Jetspeed HTTP Proxy Init Property Not Found:" + INIT_PROPERTIES_PARAM;
            log.error(lastError);                                
            return false;
        }

        String fullPath = config.getServletContext().getRealPath(paramFile);

        Configuration pc = Configuration.getInitialInstance(fullPath);  
        if (null == pc)
        {
            return false;
        }

        lastError = "";
        init = true;
        return true;
    }

    /**
     * Returns true if the service was initialized successfully.
     *
     * @retun true if the service was initialized successfully.
     */
    public boolean isInit()
    {
        return init;
    }

    /**
     * One time de-initialization of the proxy service
     *
     */
    public void destroy()
    {
        try
        {
            //
            // first logout of all Network Element Sessions
            //
            Iterator it = sessions.values().iterator();
            while (it.hasNext())
            {
                SessionMap map = (SessionMap)it.next();
                Iterator itElements = map.values().iterator();
                while (itElements.hasNext())
                {
                    SiteSession has = (SiteSession)itElements.next();
                    try 
                    {
                        has.logout(null);
                    }
                    catch (Exception e)
                    {
                        // continue logging out even if one fails
                        log.error("Shutdown-Logout of Session: " + e);                                
                    }
                }
            }

        } catch ( Exception ex )
        {
            log.error( ex );
        }
    }

    /**
     * Returns a snapshot collection of all the active and inactive sessions.
     *
     * @return the collection of sessions.
     */
    public Collection getSessions()
    {
        return sessions.values();
    }

    /**
     * Returns a session, give a string id key identifying that session.
     *
     * @param id The ID of the session.
     * @return The corresponding session.
     */
    public SessionMap getSession(String id)
    {
        return (SessionMap)sessions.get(id);
    }

    /**
     * Returns a snapshot collection of all the managed sites in the system.
     *
     * @return the collection of sites.
     */
    public Collection getSites()
    {
        return sites.values();
    }


    /**
     * Returns the error string from failed initialized.
     *
     * @return the error string from last error.
     */
    public String getErrorString()
    {
        return lastError;
    }


}




    

⌨️ 快捷键说明

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