📄 portletrequestimpl.java
字号:
/* * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a> * @version $Id: PortletRequestImpl.java 5032 2006-08-17 18:15:06Z novotny $ */package org.gridsphere.portlet.impl;import org.gridsphere.portletcontainer.PortletPreferencesManager;import org.gridsphere.services.core.user.User;import org.gridsphere.services.core.user.UserPrincipal;import javax.portlet.*;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpSession;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.*;/** * The <CODE>PortletRequest</CODE> defines the base interface to provide client * request information to a portlet. The portlet container uses two specialized * versions of this interface when invoking a portlet, <CODE>ActionRequest</CODE> * and <CODE>RenderRequest</CODE>. The portlet container creates these objects and * passes them as arguments to the portlet's <CODE>processAction</CODE> and * <CODE>render</CODE> methods. * * @see ActionRequest * @see RenderRequest */public abstract class PortletRequestImpl extends HttpServletRequestWrapper implements PortletRequest { protected PortletContext portletContext = null; protected String contextPath = "/"; protected boolean hasReader = false; protected boolean included = false; private PortletSession portletSession = null; protected GridSphereParameters portalParameters = null; /** * Constructor creates a proxy for a HttpServletRequest * All PortletRequest objects come from request or session attributes * * @param req the HttpServletRequest * @param portletContext the portlet context */ public PortletRequestImpl(HttpServletRequest req, PortletContext portletContext) { super(req); Map<String, String[]> origParams = new HashMap<String, String[]>(); this.portletContext = portletContext; contextPath = this.portletContext.getRealPath(""); int l = contextPath.lastIndexOf(File.separator); contextPath = contextPath.substring(l); // handle windows file.separator "\" to "/" if (contextPath.indexOf("\\") != -1) { contextPath = contextPath.replace('\\', '/'); } Map<String, List> map = (Map<String, List>) getHttpServletRequest().getAttribute(SportletProperties.PORTAL_PROPERTIES); if (map == null) { map = new HashMap<String, List>(); getHttpServletRequest().setAttribute(SportletProperties.PORTAL_PROPERTIES, new HashMap()); } Enumeration e = getHttpServletRequest().getHeaderNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); Enumeration headersEnum = getHttpServletRequest().getHeaders(name); List<String> vals = new ArrayList<String>(); while (headersEnum.hasMoreElements()) { String val = (String) headersEnum.nextElement(); vals.add(val); } map.put(name, vals); } getHttpServletRequest().setAttribute(SportletProperties.PORTAL_PROPERTIES, map); for (Enumeration parameters = super.getParameterNames(); parameters.hasMoreElements();) { String paramName = (String) parameters.nextElement(); String[] paramValues = (String[]) super.getParameterValues(paramName); origParams.put(paramName, paramValues); } portalParameters = new GridSphereParameters(origParams); /* System.err.println("============================= PortletRequestImpl ====================================="); if (getAttribute(SportletProperties.PORTLET_ACTION_METHOD) != null) { System.err.println("in action"); } else { System.err.println("in render"); } System.err.println("query string=" + super.getQueryString()); System.err.println("Actual HTTP parameters"); Iterator it = origParams.keySet().iterator(); while (it.hasNext()) { String paramName = (String)it.next(); String[] paramValues = (String[])origParams.get(paramName); System.err.println("\nname=" + paramName + "\nvalues="); for (int i = 0; i < paramValues.length; i++) { System.err.print(" " + paramValues[i]); } } System.err.println("\n\nPortlet parameters for portlet " ); for (Enumeration parameters = getParameterNames(); parameters.hasMoreElements();) { String paramName = (String)parameters.nextElement(); String[] paramValues = (String[])getParameterValues(paramName); System.err.println("\nname=" + paramName + "\nvalues="); for (int i = 0; i < paramValues.length; i++) { System.err.print(" " + paramValues[i]); } } System.err.println("\n==================================================================="); */ } public void setIncluded(boolean included) { this.included = included; } public boolean isIncluded() { return included; } public void addRenderParams(Map renderParams) { portalParameters.addRenderParams(renderParams); } /** * Is this attribute name a reserved name (by the J2EE spec)?. * Reserved names begin with "java." or "javax.". * * @param name the attribute name to test * @return true if the supplied name is reserved */ private boolean isNameReserved(String name) { return name.startsWith("java.") || name.startsWith("javax."); } /** * Returns true, if the given window state is valid * to be set for this portlet in the context * of the current request. * * @param state window state to checked * @return true, if it is valid for this portlet * in this request to change to the * given window state */ public boolean isWindowStateAllowed(WindowState state) { PortalContext context = (PortalContext) getAttribute(SportletProperties.PORTAL_CONTEXT); Enumeration statesEnum = context.getSupportedWindowStates(); while (statesEnum.hasMoreElements()) { WindowState s = (WindowState) statesEnum.nextElement(); if (s.equals(state)) return true; } return false; } /** * Returns true, if the given portlet mode is a valid * one to set for this portlet in the context * of the current request. * * @param mode portlet mode to check * @return true, if it is valid for this portlet * in this request to change to the * given portlet mode */ public boolean isPortletModeAllowed(PortletMode mode) { Set modesAllowed = (Set) this.getHttpServletRequest().getAttribute(SportletProperties.ALLOWED_MODES); if (modesAllowed.contains(mode.toString())) return true; return false; } /** * Returns the current portlet mode of the portlet. * * @return the portlet mode */ public PortletMode getPortletMode() { return (PortletMode) getAttribute(SportletProperties.PORTLET_MODE); } /** * Returns the current window state of the portlet. * * @return the window state */ public WindowState getWindowState() { return (WindowState) getAttribute(SportletProperties.PORTLET_WINDOW); } /** * Returns the preferences object associated with the portlet. * * @return the portlet preferences */ public PortletPreferences getPreferences() { PortletPreferencesManager prefsManager = (PortletPreferencesManager) getAttribute(SportletProperties.PORTLET_PREFERENCES_MANAGER); return prefsManager.getPortletPreferences(); } /** * Returns the current portlet session or, if there is no current session, * creates one and returns the new session. * <p/> * Creating a new portlet session will result in creating * a new <code>HttpSession</code> on which the portlet session is based on. * * @return the portlet session */ public PortletSession getPortletSession() { return getPortletSession(true); } /** * Returns the current portlet session or, if there is no current session * and the given flag is <CODE>true</CODE>, creates one and returns * the new session. * <p/> * If the given flag is <CODE>false</CODE> and there is no current * portlet session, this method returns <CODE>null</CODE>. * <p/> * Creating a new portlet session will result in creating * a new <code>HttpSession</code> on which the portlet session is based on. * * @param create <CODE>true</CODE> to create a new session, <BR> * <CODE>false</CODE> to return <CODE>null</CODE> if there * is no current session * @return the portlet session */ public PortletSession getPortletSession(boolean create) { // check if the session was invalidated HttpSession httpSession = this.getHttpServletRequest().getSession(false); if ((portletSession != null) && (httpSession == null)) { portletSession = null; } else if (httpSession != null) { create = true; } if (create && (portletSession == null)) { httpSession = this.getHttpServletRequest().getSession(create); if (httpSession != null) { portletSession = new PortletSessionImpl(this.getHttpServletRequest(), httpSession, portletContext); } } return portletSession; } /** * Returns the value of the specified request property * as a <code>String</code>. If the request did not include a property * of the specified name, this method returns <code>null</code>. * <p/> * A portlet can access portal/portlet-container specific properties * through this method and, if available, the * headers of the HTTP client request. * <p/> * This method should only be used if the * property has only one value. If the property might have * more than one value, use {@link #getProperties}. * <p/> * If this method is used with a multivalued * parameter, the value returned is equal to the first value * in the Enumeration returned by <code>getProperties</code>. * * @param name a <code>String</code> specifying the * property name * @return a <code>String</code> containing the * value of the requested * property, or <code>null</code> * if the request does not * have a property of that name. * @throws IllegalArgumentException if name is <code>null</code>. */ public String getProperty(String name) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -