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

📄 portalurlparserimpl.java

📁 portal越来越流行了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.pluto.driver.url.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pluto.driver.url.PortalURL;
import org.apache.pluto.driver.url.PortalURLParameter;
import org.apache.pluto.driver.url.PortalURLParser;
import org.apache.pluto.util.StringUtils;

/**
 * @version 1.0
 * @since Sep 30, 2004
 */
public class PortalURLParserImpl implements PortalURLParser {

	/** Logger. */
    private static final Log LOG = LogFactory.getLog(PortalURLParserImpl.class);

    /** The singleton parser instance. */
    private static final PortalURLParser PARSER = new PortalURLParserImpl();


    // Constants used for Encoding/Decoding ------------------------------------
    
    private static final String PREFIX = "__";
    private static final String DELIM = "_";
    private static final String PORTLET_ID = "pd";
    private static final String ACTION = "ac";
    private static final String RESOURCE = "rs";
    private static final String RENDER_PARAM = "rp";
    private static final String PUBLIC_RENDER_PARAM = "sp";
    private static final String WINDOW_STATE = "ws";
    private static final String PORTLET_MODE = "pm";
    private static final String VALUE_DELIM = "0x0";

    //This is a list of characters that need to be encoded  to be protected
    //The ? is necessary to protect URI's with a query portion that is being passed as a parameter
    private static final String[][] ENCODINGS = new String[][] {
    		new String[] { "_",  "0x1" },
            new String[] { ".",  "0x2" },
            new String[] { "/",  "0x3" },
            new String[] { "\r", "0x4" },
            new String[] { "\n", "0x5" },
            new String[] { "<",  "0x6" },
            new String[] { ">",  "0x7" },
            new String[] { " ",  "0x8" },
            new String[] { "#",  "0x9" },
            new String[] { "?",  "0xa" },
            new String[] { "\\", "0xb" },
            new String[] { "%",  "0xc" },
    };
    
    // Constructor -------------------------------------------------------------
    
    /**
     * Private constructor that prevents external instantiation.
     */
    private PortalURLParserImpl() {
    	// Do nothing.
    }

    /**
     * Returns the singleton parser instance.
     * @return the singleton parser instance.
     */
    public static PortalURLParser getParser() {
    	return PARSER;
    }
    
    
    // Public Methods ----------------------------------------------------------
    
    /**
     * Parse a servlet request to a portal URL.
     * @param request  the servlet request to parse.
     * @return the portal URL.
     */
    public PortalURL parse(HttpServletRequest request) {
        
    	if (LOG.isDebugEnabled()) {
            LOG.debug("Parsing URL: " + request.getRequestURI());
        }

        String contextPath = request.getContextPath();
        String servletName = request.getServletPath();
        
        // Construct portal URL using info retrieved from servlet request.
        PortalURL portalURL =  new RelativePortalURLImpl(contextPath, servletName, this);

        // Support added for filter.  Should we seperate into a different impl?
        String pathInfo = request.getPathInfo();
        if (pathInfo == null) {
            if(servletName.contains(".jsp") && !servletName.endsWith(".jsp")) {
                int idx = servletName.indexOf(".jsp")+".jsp".length();
                pathInfo = servletName.substring(idx);
                servletName = servletName.substring(0, idx);
                portalURL = new RelativePortalURLImpl(contextPath, servletName, this);
            } else {
                return portalURL;
            }
        }
        
        if (LOG.isDebugEnabled()) {
            LOG.debug("Parsing request pathInfo: " + pathInfo);
        }

        StringBuffer renderPath = new StringBuffer();
        StringTokenizer st = new StringTokenizer(pathInfo, "/", false);
        while (st.hasMoreTokens()) {
        	
        	String token = st.nextToken();
        	
        	// Part of the render path: append to renderPath.
        	if (!token.startsWith(PREFIX)) {
//        		renderPath.append(token);
        		//Fix for PLUTO-243
        		renderPath.append('/').append(token);
        	}
//        	 Resource window definition: portalURL.setResourceWindow().
           else if (token.startsWith(PREFIX + RESOURCE)) {
               portalURL.setResourceWindow(decodeControlParameter(token)[0]);
           }
        	// Action window definition: portalURL.setActionWindow().
        	else if (token.startsWith(PREFIX + ACTION)) {
        		portalURL.setActionWindow(decodeControlParameter(token)[0]);
        	}
        	// Window state definition: portalURL.setWindowState().
        	else if (token.startsWith(PREFIX + WINDOW_STATE)) {
        		String[] decoded = decodeControlParameter(token);
        		portalURL.setWindowState(decoded[0], new WindowState(decoded[1]));
        	}
        	// Portlet mode definition: portalURL.setPortletMode().
        	else if (token.startsWith(PREFIX + PORTLET_MODE)) {
        		String[] decoded = decodeControlParameter(token);
        		portalURL.setPortletMode(decoded[0], new PortletMode(decoded[1]));
        	}
        	// Portal URL parameter: portalURL.addParameter().
        	else if(token.startsWith(PREFIX + RENDER_PARAM)) {
        		String value = null;
        		if (st.hasMoreTokens()) {
        			value = st.nextToken();
        		}
        		//set the 
        		PortalURLParameter param = decodeParameter(token, value);
        		portalURL.addParameter(param);
        		
        		
        	}
        	else{ // besser if PREFIX + PUBLIC_PARAM
        		String value = null;
        		if (st.hasMoreTokens()) {
        			value = st.nextToken();
        		}
        		PortalURLParameter param = decodePublicParameter(token, value);
        		if( param != null )
        		{
        			//set public parameter in portalURL
    	    		portalURL.addPublicParameterCurrent(param.getName(), param.getValues());
        		}
	    		
	    		
        	}
        }
        if (renderPath.length() > 0) {
            portalURL.setRenderPath(renderPath.toString());
        }
        
        // Return the portal URL.
        return portalURL;
    }
    
    
    /**
     * Converts a portal URL to a URL string.
     * @param portalURL  the portal URL to convert.
     * @return a URL string representing the portal URL.
     */
    public String toString(PortalURL portalURL) {

    	StringBuffer buffer = new StringBuffer();
    	
        // Append the server URI and the servlet path.
    	buffer.append(portalURL.getServletPath().startsWith("/")?"":"/")
            .append(portalURL.getServletPath());

        // Start the pathInfo with the path to the render URL (page).
        if (portalURL.getRenderPath() != null) {
        	buffer.append("/").append(portalURL.getRenderPath());
        }
        //Append the resource window definition, if it exists.        
        if (portalURL.getResourceWindow() != null){
               buffer.append("/");
               buffer.append(PREFIX).append(RESOURCE)
                               .append(encodeCharacters(portalURL.getResourceWindow()));
        }
        // Append the action window definition, if it exists.
        if (portalURL.getActionWindow() != null) {
        	buffer.append("/");
        	buffer.append(PREFIX).append(ACTION)
        			.append(encodeCharacters(portalURL.getActionWindow()));
        }
        
        // Append portlet mode definitions.
        for (Iterator it = portalURL.getPortletModes().entrySet().iterator();
        		it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            buffer.append("/").append(
            		encodeControlParameter(PORTLET_MODE, entry.getKey().toString(),
                       entry.getValue().toString()));
        }
        
        // Append window state definitions.
        for (Iterator it = portalURL.getWindowStates().entrySet().iterator();
        		it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            buffer.append("/").append(
            		encodeControlParameter(WINDOW_STATE, entry.getKey().toString(),
                       entry.getValue().toString()));

⌨️ 快捷键说明

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