FileDocCategorySizeDatePackage
ApplicationHttpRequest.javaAPI DocApache Tomcat 6.0.1426226Fri Jul 20 04:20:34 BST 2007org.apache.catalina.core

ApplicationHttpRequest

public class ApplicationHttpRequest extends HttpServletRequestWrapper
Wrapper around a javax.servlet.http.HttpServletRequest that transforms an application request object (which might be the original one passed to a servlet, or might be based on the 2.3 javax.servlet.http.HttpServletRequestWrapper class) back into an internal org.apache.catalina.HttpRequest.

WARNING: Due to Java's lack of support for multiple inheritance, all of the logic in ApplicationRequest is duplicated in ApplicationHttpRequest. Make sure that you keep these two classes in synchronization when making changes!

author
Craig R. McClanahan
author
Remy Maucherat
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
protected static final String[]
specials
The set of attribute names that are special for request dispatchers.
protected static org.apache.catalina.util.StringManager
sm
The string manager for this package.
protected org.apache.catalina.Context
context
The context for this request.
protected String
contextPath
The context path for this request.
protected boolean
crossContext
If this request is cross context, since this changes session accesss behavior.
protected Object
dispatcherType
The current dispatcher type.
protected static final String
info
Descriptive information about this implementation.
protected Map
parameters
The request parameters for this request. This is initialized from the wrapped request, but updates are allowed.
private boolean
parsedParams
Have the parameters for this request already been parsed?
protected String
pathInfo
The path information for this request.
private String
queryParamString
The query parameters for the current request.
protected String
queryString
The query string for this request.
protected Object
requestDispatcherPath
The current request dispatcher path.
protected String
requestURI
The request URI for this request.
protected String
servletPath
The servlet path for this request.
protected org.apache.catalina.Session
session
The currently active session for this request.
protected Object[]
specialAttributes
Special attributes.
Constructors Summary
public ApplicationHttpRequest(HttpServletRequest request, org.apache.catalina.Context context, boolean crossContext)
Construct a new wrapped request around the specified servlet request.

param
request The servlet request being wrapped



    // ----------------------------------------------------------- Constructors


                          
        
                                    

        super(request);
        this.context = context;
        this.crossContext = crossContext;
        setRequest(request);

    
Methods Summary
java.util.MapcopyMap(java.util.Map orig)
Perform a shallow copy of the specified Map, and return the result.

param
orig Origin Map to be copied


        if (orig == null)
            return (new HashMap());
        HashMap dest = new HashMap();
        Iterator keys = orig.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            dest.put(key, orig.get(key));
        }
        return (dest);

    
public java.lang.ObjectgetAttribute(java.lang.String name)
Override the getAttribute() method of the wrapped request.

param
name Name of the attribute to retrieve



    // ------------------------------------------------- ServletRequest Methods


                         
        

        if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
            return dispatcherType;
        } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
            if ( requestDispatcherPath != null ){
                return requestDispatcherPath.toString();
            } else {
                return null;   
            }
        }

        int pos = getSpecial(name);
        if (pos == -1) {
            return getRequest().getAttribute(name);
        } else {
            if ((specialAttributes[pos] == null) 
                && (specialAttributes[5] == null) && (pos >= 5)) {
                // If it's a forward special attribute, and null, it means this
                // is an include, so we check the wrapped request since 
                // the request could have been forwarded before the include
                return getRequest().getAttribute(name);
            } else {
                return specialAttributes[pos];
            }
        }

    
public java.util.EnumerationgetAttributeNames()
Override the getAttributeNames() method of the wrapped request.

        return (new AttributeNamesEnumerator());
    
public java.lang.StringgetContextPath()
Override the getContextPath() method of the wrapped request.


        return (this.contextPath);

    
public java.lang.StringgetInfo()
Return descriptive information about this implementation.


        return (info);

    
public java.lang.StringgetParameter(java.lang.String name)
Override the getParameter() method of the wrapped request.

param
name Name of the requested parameter


	parseParameters();

        Object value = parameters.get(name);
        if (value == null)
            return (null);
        else if (value instanceof String[])
            return (((String[]) value)[0]);
        else if (value instanceof String)
            return ((String) value);
        else
            return (value.toString());

    
public java.util.MapgetParameterMap()
Override the getParameterMap() method of the wrapped request.


	parseParameters();
        return (parameters);

    
public java.util.EnumerationgetParameterNames()
Override the getParameterNames() method of the wrapped request.


	parseParameters();
        return (new Enumerator(parameters.keySet()));

    
public java.lang.String[]getParameterValues(java.lang.String name)
Override the getParameterValues() method of the wrapped request.

param
name Name of the requested parameter


	parseParameters();
        Object value = parameters.get(name);
        if (value == null)
            return ((String[]) null);
        else if (value instanceof String[])
            return ((String[]) value);
        else if (value instanceof String) {
            String values[] = new String[1];
            values[0] = (String) value;
            return (values);
        } else {
            String values[] = new String[1];
            values[0] = value.toString();
            return (values);
        }

    
public java.lang.StringgetPathInfo()
Override the getPathInfo() method of the wrapped request.


        return (this.pathInfo);

    
public java.lang.StringgetQueryString()
Override the getQueryString() method of the wrapped request.


        return (this.queryString);

    
public javax.servlet.RequestDispatchergetRequestDispatcher(java.lang.String path)
Return a RequestDispatcher that wraps the resource at the specified path, which may be interpreted as relative to the current request path.

param
path Path of the resource to be wrapped


        if (context == null)
            return (null);

        // If the path is already context-relative, just pass it through
        if (path == null)
            return (null);
        else if (path.startsWith("/"))
            return (context.getServletContext().getRequestDispatcher(path));

        // Convert a request-relative path to a context-relative one
        String servletPath = 
            (String) getAttribute(Globals.INCLUDE_SERVLET_PATH_ATTR);
        if (servletPath == null)
            servletPath = getServletPath();

        // Add the path info, if there is any
        String pathInfo = getPathInfo();
        String requestPath = null;

        if (pathInfo == null) {
            requestPath = servletPath;
        } else {
            requestPath = servletPath + pathInfo;
        }

        int pos = requestPath.lastIndexOf('/");
        String relative = null;
        if (pos >= 0) {
            relative = RequestUtil.normalize
                (requestPath.substring(0, pos + 1) + path);
        } else {
            relative = RequestUtil.normalize(requestPath + path);
        }

        return (context.getServletContext().getRequestDispatcher(relative));

    
public java.lang.StringgetRequestURI()
Override the getRequestURI() method of the wrapped request.


        return (this.requestURI);

    
public java.lang.StringBuffergetRequestURL()
Override the getRequestURL() method of the wrapped request.


        StringBuffer url = new StringBuffer();
        String scheme = getScheme();
        int port = getServerPort();
        if (port < 0)
            port = 80; // Work around java.net.URL bug

        url.append(scheme);
        url.append("://");
        url.append(getServerName());
        if ((scheme.equals("http") && (port != 80))
            || (scheme.equals("https") && (port != 443))) {
            url.append(':");
            url.append(port);
        }
        url.append(getRequestURI());

        return (url);

    
public java.lang.StringgetServletPath()
Override the getServletPath() method of the wrapped request.


        return (this.servletPath);

    
public javax.servlet.http.HttpSessiongetSession()
Return the session associated with this Request, creating one if necessary.

        return (getSession(true));
    
public javax.servlet.http.HttpSessiongetSession(boolean create)
Return the session associated with this Request, creating one if necessary and requested.

param
create Create a new session if one does not exist


        if (crossContext) {
            
            // There cannot be a session if no context has been assigned yet
            if (context == null)
                return (null);

            // Return the current session if it exists and is valid
            if (session != null && session.isValid()) {
                return (session.getSession());
            }

            HttpSession other = super.getSession(false);
            if (create && (other == null)) {
                // First create a session in the first context: the problem is
                // that the top level request is the only one which can 
                // create the cookie safely
                other = super.getSession(true);
            }
            if (other != null) {
                Session localSession = null;
                try {
                    localSession =
                        context.getManager().findSession(other.getId());
                } catch (IOException e) {
                    // Ignore
                }
                if (localSession == null && create) {
                    localSession = 
                        context.getManager().createSession(other.getId());
                }
                if (localSession != null) {
                    localSession.access();
                    session = localSession;
                    return session.getSession();
                }
            }
            return null;

        } else {
            return super.getSession(create);
        }

    
protected intgetSpecial(java.lang.String name)
Get a special attribute.

return
the special attribute pos, or -1 if it is not a special attribute

        for (int i = 0; i < specials.length; i++) {
            if (specials[i].equals(name)) {
                return (i);
            }
        }
        return (-1);
    
public booleanisRequestedSessionIdValid()
Returns true if the request specifies a JSESSIONID that is valid within the context of this ApplicationHttpRequest, false otherwise.

return
true if the request specifies a JSESSIONID that is valid within the context of this ApplicationHttpRequest, false otherwise.


        if (crossContext) {

            String requestedSessionId = getRequestedSessionId();
            if (requestedSessionId == null)
                return (false);
            if (context == null)
                return (false);
            Manager manager = context.getManager();
            if (manager == null)
                return (false);
            Session session = null;
            try {
                session = manager.findSession(requestedSessionId);
            } catch (IOException e) {
                session = null;
            }
            if ((session != null) && session.isValid()) {
                return (true);
            } else {
                return (false);
            }

        } else {
            return super.isRequestedSessionIdValid();
        }
    
protected booleanisSpecial(java.lang.String name)
Is this attribute name one of the special ones that is added only for included servlets?

param
name Attribute name to be tested


        for (int i = 0; i < specials.length; i++) {
            if (specials[i].equals(name))
                return (true);
        }
        return (false);

    
private voidmergeParameters()
Merge the parameters from the saved query parameter string (if any), and the parameters already present on this request (if any), such that the parameter values from the query string show up first if there are duplicate parameter names.


        if ((queryParamString == null) || (queryParamString.length() < 1))
            return;

        HashMap queryParameters = new HashMap();
        String encoding = getCharacterEncoding();
        if (encoding == null)
            encoding = "ISO-8859-1";
        try {
            RequestUtil.parseParameters
                (queryParameters, queryParamString, encoding);
        } catch (Exception e) {
            ;
        }
        Iterator keys = parameters.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            Object value = queryParameters.get(key);
            if (value == null) {
                queryParameters.put(key, parameters.get(key));
                continue;
            }
            queryParameters.put
                (key, mergeValues(value, parameters.get(key)));
        }
        parameters = queryParameters;

    
protected java.lang.String[]mergeValues(java.lang.Object values1, java.lang.Object values2)
Merge the two sets of parameter values into a single String array.

param
values1 First set of values
param
values2 Second set of values


        ArrayList results = new ArrayList();

        if (values1 == null)
            ;
        else if (values1 instanceof String)
            results.add(values1);
        else if (values1 instanceof String[]) {
            String values[] = (String[]) values1;
            for (int i = 0; i < values.length; i++)
                results.add(values[i]);
        } else
            results.add(values1.toString());

        if (values2 == null)
            ;
        else if (values2 instanceof String)
            results.add(values2);
        else if (values2 instanceof String[]) {
            String values[] = (String[]) values2;
            for (int i = 0; i < values.length; i++)
                results.add(values[i]);
        } else
            results.add(values2.toString());

        String values[] = new String[results.size()];
        return ((String[]) results.toArray(values));

    
voidparseParameters()
Parses the parameters of this request. If parameters are present in both the query string and the request content, they are merged.


	if (parsedParams) {
	    return;
	}

        parameters = new HashMap();
        parameters = copyMap(getRequest().getParameterMap());
        mergeParameters();
        parsedParams = true;
    
public voidrecycle()
Recycle this request

        if (session != null) {
            session.endAccess();
        }
    
public voidremoveAttribute(java.lang.String name)
Override the removeAttribute() method of the wrapped request.

param
name Name of the attribute to remove


        if (!removeSpecial(name))
            getRequest().removeAttribute(name);

    
protected booleanremoveSpecial(java.lang.String name)
Remove a special attribute.

return
true if the attribute was a special attribute, false otherwise

        for (int i = 0; i < specials.length; i++) {
            if (specials[i].equals(name)) {
                specialAttributes[i] = null;
                return (true);
            }
        }
        return (false);
    
public voidsetAttribute(java.lang.String name, java.lang.Object value)
Override the setAttribute() method of the wrapped request.

param
name Name of the attribute to set
param
value Value of the attribute to set


        if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
            dispatcherType = value;
            return;
        } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
            requestDispatcherPath = value;
            return;
        }

        if (!setSpecial(name, value)) {
            getRequest().setAttribute(name, value);
        }

    
voidsetContextPath(java.lang.String contextPath)
Set the context path for this request.

param
contextPath The new context path


        this.contextPath = contextPath;

    
voidsetPathInfo(java.lang.String pathInfo)
Set the path information for this request.

param
pathInfo The new path info


        this.pathInfo = pathInfo;

    
voidsetQueryParams(java.lang.String queryString)
Save query parameters for this request.

param
queryString The query string containing parameters for this request

        this.queryParamString = queryString;
    
voidsetQueryString(java.lang.String queryString)
Set the query string for this request.

param
queryString The new query string


        this.queryString = queryString;

    
voidsetRequest(javax.servlet.http.HttpServletRequest request)
Set the request that we are wrapping.

param
request The new wrapped request


        super.setRequest(request);

        // Initialize the attributes for this request
        dispatcherType = request.getAttribute(Globals.DISPATCHER_TYPE_ATTR);
        requestDispatcherPath = 
            request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);

        // Initialize the path elements for this request
        contextPath = request.getContextPath();
        pathInfo = request.getPathInfo();
        queryString = request.getQueryString();
        requestURI = request.getRequestURI();
        servletPath = request.getServletPath();

    
voidsetRequestURI(java.lang.String requestURI)
Set the request URI for this request.

param
requestURI The new request URI


        this.requestURI = requestURI;

    
voidsetServletPath(java.lang.String servletPath)
Set the servlet path for this request.

param
servletPath The new servlet path


        this.servletPath = servletPath;

    
protected booleansetSpecial(java.lang.String name, java.lang.Object value)
Set a special attribute.

return
true if the attribute was a special attribute, false otherwise

        for (int i = 0; i < specials.length; i++) {
            if (specials[i].equals(name)) {
                specialAttributes[i] = value;
                return (true);
            }
        }
        return (false);