alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *  Copyright 1999-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.tomcat.facade;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.core.Context;
import org.apache.tomcat.core.Request;
import org.apache.tomcat.core.ServerSession;
import org.apache.tomcat.core.TomcatException;
import org.apache.tomcat.util.buf.UEncoder;
import org.apache.tomcat.util.http.AcceptLanguage;
import org.apache.tomcat.util.http.Cookies;
import org.apache.tomcat.util.http.MimeHeaders;
import org.apache.tomcat.util.http.ServerCookie;
import org.apache.tomcat.util.io.FileUtil;
import org.apache.tomcat.util.res.StringManager;

/**
 * The facade to the request that a servlet will see.
 *
 * @author James Duncan Davidson [duncan@eng.sun.com]
 * @author Jason Hunter [jch@eng.sun.com]
 * @author James Todd [gonzo@eng.sun.com]
 * @author Harish Prabandham
 * @author Costin Manolache
 * @author Ignacio J. Ortega
 *
 */
public final class HttpServletRequestFacade implements HttpServletRequest {
    private static StringManager sm =
	StringManager.getManager("org.apache.tomcat.resources");

    private Request request;

    HttpSessionFacade sessionFacade;
    ServletInputStreamFacade isFacade=new ServletInputStreamFacade();
    boolean isFacadeInitialized=false;
    BufferedReader reader=null;
    UEncoder uencoder;

    private boolean usingStream = false;
    private boolean usingReader = false;

    private boolean parametersProcessed=false;
    
    /** Not public 
     */
    HttpServletRequestFacade(Request request) {
        this.request = request;
	isFacade.setRequest( request );
	try {
	    // we may create facades more often than requests
	    if( uencoder==null ) {
		uencoder=new UEncoder();
		uencoder.addSafeCharacter(';');
		uencoder.addSafeCharacter('/');
		request.setNote( "req.uencoder", uencoder );
	    } 
	} catch( TomcatException ex ) {
	    ex.printStackTrace();
	}

    }

    /** Not public - is called only from FacadeManager on behalf of Request
     */
    void recycle() {
	usingReader=false;
	usingStream=false;
	parametersProcessed=false;
	sessionFacade=null;
	if( isFacade != null ) isFacade.recycle();
	isFacadeInitialized=false;
        reader=null;
    }

    /** Not public - is called only from FacadeManager
     */
    Request getRealRequest() {
	return request;
    }

    /** Not public - is called only from FacadeManager
     */
    void setRequest( Request req ) {
	request=req;
    }
    
    // -------------------- Public facade methods --------------------
    public Object getAttribute(String name) {
	return request.getAttribute(name);
    }

    public Enumeration getAttributeNames() {
	return request.getAttributeNames();
    }

    public void setAttribute(String name, Object value) {
        request.setAttribute(name, value);
    }

    public void removeAttribute(String name) {
	request.removeAttribute(name);
    }
    
    public String getCharacterEncoding() {
	String enc=request.getCharacterEncoding();

	// Source is set if we are sure about the encoding,
	// we've got it from header or session or some
	// well-defined mechanism. No source means the default
	// is used.
	try {
	    Object o=request.getNote( "req.encodingSource" );
	    if( o==null ) return null;
	} catch( TomcatException ex ) {
	    ex.printStackTrace();
	}
	return enc;
    }

    public int getContentLength() {
        return request.getContentLength();
    }

    public String getContentType() {
	return request.getContentType();
    }

    public Cookie[] getCookies() {
	Cookies cookies=request.getCookies();
	int count=cookies.getCookieCount();
	Cookie[] cookieArray = new Cookie[ count ];

	// Convert from ServerCookie to Cookie.
	// The price is payed _only_ by servlets that call getCookie().
	// ( if you don't call it no allocation happens for cookies )
	// ( well, it happens, the code to reuse have to be written )
	for (int i = 0; i < count; i ++) {
	    ServerCookie sC=cookies.getCookie(i);
	    cookieArray[i] = new CookieFacade(sC);
	}

	return cookieArray;
    }

    /** Tomcat Request doesn't deal with header to date conversion.
     *  We delegate this to RequestUtil. ( adapter function )
     */
    public long getDateHeader(String name) {
	return request.getDateHeader( name );
    }

    public String getHeader(String name) {
        return request.getHeader(name);
    }

    public Enumeration getHeaders(String name) {
        return request.getHeaders(name);
    }

    public Enumeration getHeaderNames() {
        return request.getHeaderNames();
    }

    /** Adapter: Tomcat Request allows both stream and writer access.
     */
    public ServletInputStream getInputStream() throws IOException {
	if (usingReader) {
	    String msg = sm.getString("reqfac.getinstream.ise");
	    throw new IllegalStateException(msg);
	}
	usingStream = true;

	if( ! isFacadeInitialized ) {
	    isFacade.prepare();
	    isFacadeInitialized=true;
	}
	return isFacade;
    }

    /** Adapter: Tomcat Request doesn't deal with header to int conversion.
     */
    public int getIntHeader(String name)
	throws  NumberFormatException
    {
	String value=request.getHeader( name );
	if( value==null) return -1;
	int valueInt=Integer.parseInt(value);
	return valueInt;
    }
    
    public String getMethod() {
        return request.method().toString();
    }

    /** Adapter: Request doesn't deal with this servlet convention
     */
    public String getParameter(String name) {
	if( ! parametersProcessed ) {
	    request.handleQueryParameters();
	    if( request.method().equals("POST")) {
		request.handlePostParameters();
	    }
	    parametersProcessed=true;
	}
        return request.parameters().getParameter( name );
    }

    public String[] getParameterValues(String name) {
	if( ! parametersProcessed ) {
	    request.handleQueryParameters();
	    if( request.method().equals("POST")) {
		request.handlePostParameters();
	    }
	    parametersProcessed=true;
	}
        return request.parameters().getParameterValues(name);
    }

    public Enumeration getParameterNames() {
	if( ! parametersProcessed ) {
	    request.handleQueryParameters();
	    if( request.method().equals("POST")) {
		request.handlePostParameters();
	    }
	    parametersProcessed=true;
	}
        return request.parameters().getParameterNames();
    }
    
    public String getPathInfo() {
	// DECODED
        return request.pathInfo().toString();
    }

    public String getPathTranslated() {
	// DECODED
	// Servlet 2.2 spec differs from what Apache and
	// all other web servers consider to be PATH_TRANSLATED.
	// It's important not to use CGI PATH_TRANSLATED - this
	// code is specific to servlet 2.2 ( or more )
	String path=getPathInfo();
	if(path==null || "".equals( path ) ) return null;
	String pathTranslated=
	    FileUtil.safePath( request.getContext().getAbsolutePath(),
			       path);
	return pathTranslated;
    }
    
    public String getProtocol() {
        return request.protocol().toString();
    }

    public String getQueryString() {
	// ENCODED. We don't decode the original query string,
	// we we can return the same thing
	String qS=request.queryString().toString();
	if( "".equals(qS) )
	    return null;
	return qS;
    }

    public String getRemoteUser() {
	return request.getRemoteUser();
    }

    public String getScheme() {
        return request.scheme().toString();
    }

    public String getServerName() {
	return request.serverName().toString();
    }

    public int getServerPort() {
        return request.getServerPort();
    }

    /** Adapter: Tomcat Request allows both stream and writer access.
     */
    public BufferedReader getReader() throws IOException {
	if (usingStream) {
	    String msg = sm.getString("reqfac.getreader.ise");
	    throw new IllegalStateException(msg);
	}
	usingReader = true;

	if( reader != null ) return reader; // method already called 

	if( ! isFacadeInitialized ) {
	    isFacade.prepare();
	    isFacadeInitialized=true;
	}

	// XXX  provide recycleable objects
	String encoding = request.getCharacterEncoding();
	
	InputStreamReader r =
            new InputStreamReader(isFacade, encoding);
	reader= new BufferedReader(r);
	return reader;
    }
    
    public String getRemoteAddr() {
        return request.remoteAddr().toString();
    }

    public String getRemoteHost() {
	String remoteHost = request.remoteHost().toString();

        // AJP12 defaults to empty string, AJP13 defaults to null
        if(remoteHost != null && remoteHost.length() != 0)
            return remoteHost;

        try{
            remoteHost = InetAddress.getByName(request.remoteAddr().toString()).getHostName();
        }catch(Exception e){
            // If anything went wrong then fall back to using the remote hosts IP address
            remoteHost = request.remoteAddr().toString();
        }

        return remoteHost;
    }

    public String getRequestURI() {
	// ENCODED
	if( request.unparsedURI().isNull() ) {
	    // unparsed URI is used as a cache.
	    // request.unparsedURI().duplicate( request.requestURI() );
	    String decoded=request.requestURI().toString();
	    // XXX - I'm not sure what encoding should we use - maybe output ?,
	    // since this will probably be used for the output
	    uencoder.setEncoding(request.getCharacterEncoding());
	    String encoded= uencoder.encodeURL( decoded );
	    
	    request.unparsedURI().setString( encoded );
	}
        return request.unparsedURI().toString();
    }

    
    /** Facade: we delegate to the right object ( the context )
     */
    public RequestDispatcher getRequestDispatcher(String path) {
        if (path == null)
	    return null;

	if (! path.startsWith("/")) {
	    // The original implementation returned that RD relative
	    // to lookupPath, which is RequestPath + PathInfo
	    String pI= request.pathInfo().toString();
	    if( pI == null ) 
		path= FileUtil.catPath( request.servletPath().toString(),
					path );
	    else
		path= FileUtil.catPath( request.servletPath().toString() + pI,
					path);
	    if( path==null) return null;
	}

	Context ctx=request.getContext();
	return ((ServletContext)ctx.getFacade()).getRequestDispatcher(path);
    }

    /** Adapter: first elelment
     */
    public Locale getLocale() {
	return (Locale)getLocales().nextElement();
    }

    /** Delegate to RequestUtil
     */
    public Enumeration getLocales() {
	MimeHeaders headers=request.getMimeHeaders();
        return AcceptLanguage.getLocales(headers.getHeader("Accept-Language"));
    }

    /** Delegate to Context
     */
    public String getContextPath() {
	// Should be ENCODED ( in 2.3 ). tomcat4.0 doesn't seem to do that
	// ( it's in fact returning 404 on any encoded context paths ), and
	// is very likely to result in user errors - if anyone expects it to
	// be decoded, as it allways was in 3.x or if anyone will use it as
	// a key.
        return request.getContext().getPath();
    }

    public String getServletPath() {
	// DECODED
        return request.servletPath().toString();
    }

    /**
     * @deprecated
     */
    public String getRealPath(String name) {
	Context ctx=request.getContext();
        return FileUtil.safePath( ctx.getAbsolutePath(),
				  name);
    }
    
    // -------------------- Security --------------------
    public String getAuthType() {
	return request.getAuthType();
    }
    
    public boolean isSecure() {
	return request.isSecure();
    }

    public boolean isUserInRole(String role) {
        // get the servletWrapper...
        ServletHandler handler=(ServletHandler)request.getHandler();
        String realRole=role;
        if ( handler!= null ) {
            // lookup the alias
            String mappedRole = handler.getServletInfo().getSecurityRole(role);
            if ( mappedRole != null ) {
                // use translated role
                realRole = mappedRole;
            }
        }
        return request.isUserInRole(realRole);
    }

    public Principal getUserPrincipal() {
	return request.getUserPrincipal();
    }
    
    // -------------------- Session --------------------

    public HttpSession getSession() {
	return getSession(true);
    }

    /** Create the Facade for session.
     */
    public HttpSession getSession(boolean create) {
	ServerSession realSession = (ServerSession)request.getSession(create);

	// No real session, return null
	if( realSession == null ) {
	    sessionFacade=null;
	    return null;
	}

	
	sessionFacade=(HttpSessionFacade)realSession.getFacade();
	if( sessionFacade==null ) {
	    sessionFacade=new HttpSessionFacade();
	    sessionFacade.setRealSession( realSession );
	    realSession.setFacade( sessionFacade );
	}
        return sessionFacade;
    }

    public String getRequestedSessionId() {
        return request.getRequestedSessionId();
    }
    
    public boolean isRequestedSessionIdValid() {
        boolean isvalid = false;
        ServerSession session = (ServerSession)request.getSession(false);
        if(session != null && session.getId().equals(getRequestedSessionId()))
            isvalid = true;

        return isvalid;
    }

    /** Adapter - Request uses getSessionIdSource
     */
    public boolean isRequestedSessionIdFromCookie() {
	return Request.SESSIONID_FROM_COOKIE.equals( request.getSessionIdSource() );
    }

    /**
     * @deprecated
     */
    public boolean isRequestedSessionIdFromUrl() {
	return isRequestedSessionIdFromURL();
    }

    public boolean isRequestedSessionIdFromURL() {
	return Request.SESSIONID_FROM_URL.equals( request.getSessionIdSource() );
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.