📄 requestimpl.java
字号:
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.tomcat.core;
import org.apache.tomcat.facade.*;
import org.apache.tomcat.util.*;
import java.io.IOException;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author James Duncan Davidson [duncan@eng.sun.com]
* @author James Todd [gonzo@eng.sun.com]
* @author Jason Hunter [jch@eng.sun.com]
* @author Harish Prabandham
* @author Alex Cruikshank [alex@epitonic.com]
* @author Hans Bergsten [hans@gefionsoftware.com]
*/
public class RequestImpl implements Request {
// GS, used by the load balancing layer in the Web Servers
// jvmRoute == the name of the JVM inside the plugin.
protected String jvmRoute;
// XXX used by forward to override, need a better
// mechanism
protected String requestURI;
protected String queryString;
// RequestAdapterImpl Hints
protected String serverName=null;
protected Vector cookies = new Vector();
protected String contextPath;
protected String lookupPath; // everything after contextPath before ?
protected String servletPath;
protected String pathInfo;
protected String pathTranslated;
// Need to distinguish between null pathTranslated and
// lazy-computed pathTranlsated
protected boolean pathTranslatedIsSet=false;
protected Hashtable parameters = new Hashtable();
protected int contentLength = -1;
protected String contentType = null;
protected String charEncoding = null;
protected String authType;
boolean notAuthenticated=true;
protected String remoteUser;
protected Principal principal;
// active roles for the current user
protected String userRoles[];
protected String reqRoles[];
// Request
protected Response response;
protected HttpServletRequest requestFacade;
protected Context context;
protected ContextManager contextM;
protected Hashtable attributes = new Hashtable();
protected boolean didReadFormData;
protected boolean didParameters;
protected boolean didCookies;
// end "Request" variables
// Session
// set by interceptors - the session id
protected String reqSessionId;
protected String sessionIdSource;
// cache- avoid calling SessionManager for each getSession()
protected HttpSession serverSession;
// LookupResult - used by sub-requests and
// set by interceptors
protected String servletName;
protected ServletWrapper handler = null;
Container container;
protected String mappedPath = null;
protected String scheme;
protected String method;
protected String protocol;
protected MimeHeaders headers;
protected ServletInputStream in;
protected int serverPort;
protected String remoteAddr;
protected String remoteHost;
protected String localHost;
protected ByteBuffer bBuffer;
Request top;
Request parent;
Request child;
protected static StringManager sm =
StringManager.getManager("org.apache.tomcat.core");
public RequestImpl() {
// System.out.println("XXX new ri " );
headers = new MimeHeaders();
initRequest();
}
public void setContext(Context context) {
this.context = context;
}
public void setContextManager( ContextManager cm ) {
contextM=cm;
}
public ContextManager getContextManager() {
return contextM;
}
public String getScheme() {
return scheme;
}
public String getMethod() {
return method;
}
public String getRequestURI() {
if( requestURI!=null) return requestURI;
return requestURI;
}
// XXX used by forward
public String getQueryString() {
if( queryString != null ) return queryString;
return queryString;
}
public String getProtocol() {
return protocol;
}
/** Return the server name. If none was set,
* extract it from the host header.
*
*/
public String getServerName() {
if(serverName!=null) return serverName;
String hostHeader = this.getHeader("host");
if (hostHeader != null) {
int i = hostHeader.indexOf(':');
if (i > -1) {
hostHeader = hostHeader.substring(0,i);
}
serverName=hostHeader;
return serverName;
}
if( localHost != null ) {
serverName = localHost;
return serverName;
}
// default to localhost - and warn
// System.out.println("No server name, defaulting to localhost");
serverName="localhost";
return serverName;
}
/** Virtual host */
public void setServerName(String serverName) {
this.serverName = serverName;
}
public String getLookupPath() {
return lookupPath;
}
public void setLookupPath( String l ) {
lookupPath=l;
}
// XXX optimize for common case ( single params )
public String getParameter(String name ) {
String[] values = getParameterValues(name);
if (values != null) {
return values[0];
} else {
return null;
}
}
public String[] getParameterValues(String name) {
handleParameters();
return (String[])parameters.get(name);
}
public Enumeration getParameterNames() {
handleParameters();
return parameters.keys();
}
public String getAuthType() {
return authType;
}
public String getCharacterEncoding() {
if(charEncoding!=null) return charEncoding;
charEncoding = RequestUtil.getCharsetFromContentType( getContentType());
return charEncoding;
}
public int getContentLength() {
if( contentLength > -1 ) return contentLength;
String value=getHeader( "content-length" );
if( value==null) return -1;
contentLength = Integer.parseInt(value);
return contentLength;
}
public String getContentType() {
if(contentType != null) return contentType;
contentType = getHeader("content-type");
if(contentType != null) return contentType;
// can be null!! -
return contentType;
}
/** All adapters that know the PT needs to call this method,
in order to set pathTranslatedIsSet, otherwise tomcat
will try to compute it again
*/
public void setPathTranslated(String s ) {
pathTranslated=s;
pathTranslatedIsSet=true;
}
/** Not so usefull - it return the path translated for a
URL relative the the context, i.e. different from
what PATH_TRANSLATED does. Avoid using it.
*/
public String getPathTranslated() {
if( pathTranslatedIsSet ) return pathTranslated;
// not set yet - we'll compute it
pathTranslatedIsSet=true;
String path=getPathInfo();
// In CGI spec, PATH_TRANSLATED shouldn't be set if no path
// info is present
pathTranslated=null;
if(path==null || "".equals( path ) ) return null;
pathTranslated=context.getRealPath( path );
return pathTranslated;
}
// XXX XXX Servlet API conflicts with the CGI specs -
// PathInfo should be "" if no path info is requested ( as it is in CGI ).
// We are following the spec, but IMHO it's a bug ( in the spec )
public String getPathInfo() {
return pathInfo;
}
public void setRemoteUser(String s) {
remoteUser=s;
// this is set by an auth module
// context.log("Set user " + s );
notAuthenticated=false;
}
public String getRemoteUser() {
if( notAuthenticated ) {
notAuthenticated=false;
contextM.doAuthenticate(this, response);
// context.log("Auth " + remoteUser );
}
return remoteUser;
}
public boolean isSecure() {
// The adapter is responsible for providing this information
return getScheme().equalsIgnoreCase("HTTPS");
}
public void setUserPrincipal( Principal p ) {
principal=p;
}
/** Return the principal - the adapter will set it
*/
public Principal getUserPrincipal() {
if( getRemoteUser() == null ) return null;
return principal;
}
public void setRequiredRoles( String roles[] ) {
reqRoles=roles;
}
public String[] getRequiredRoles( ) {
return reqRoles;
}
public void setUserRoles( String roles[] ) {
userRoles=roles;
}
public String[] getUserRoles( ) {
return userRoles;
}
public boolean isUserInRole(String role) {
String checkRoles[]=new String[1];
// get the servletWrapper...
if ( handler != null ) {
// lookup the alias
String mappedRole = handler.getSecurityRole(role);
if ( mappedRole != null ) {
// use translated role
checkRoles[0] = mappedRole;
} else {
/* XXX
* no alias found - technically we should return false however
* to maintain backwards compatability with earlier tomcat's
* preserver the existing behavior and do a lookup
* using the actual rolename passed to us
*/
checkRoles[0] = role;
}
} else {
/* XXX servletWrapper is null -
* this shouldn't happen but setup for the lookup anyway
*/
checkRoles[0] = role;
}
int status=contextM.doAuthorize(this, response, checkRoles);
return status==0;
}
public String getServletPath() {
return servletPath;
}
// End hints
// -------------------- Request methods ( high level )
public HttpServletRequest getFacade() {
// some requests are internal, and will never need a
// facade - no need to create a new object unless needed.
if( requestFacade==null ) {
if( context==null ) {
// wrong request
// XXX the will go away after we remove the one-one relation between
// request and facades ( security, etc)
requestFacade = contextM.getContext("" ).getFacadeManager().createHttpServletRequestFacade(this );
return requestFacade;
}
requestFacade = context.getFacadeManager().createHttpServletRequestFacade(this);
}
return requestFacade;
}
public Context getContext() {
return context;
}
public void setResponse(Response response) {
this.response = response;
}
public Response getResponse() {
return response;
}
// -------------------- Session --------------------
// GS - return the jvm load balance route
public String getJvmRoute() {
return jvmRoute;
}
public void setJvmRoute(String jvmRoute) {
this.jvmRoute=jvmRoute;
}
public String getRequestedSessionId() {
return reqSessionId;
}
public void setRequestedSessionId(String reqSessionId) {
this.reqSessionId = reqSessionId;
}
public String getSessionIdSource() {
return sessionIdSource;
}
public void setSessionIdSource(String s) {
sessionIdSource=s;
}
public void setSession(HttpSession serverSession) {
// context.log("RequestImpl: set session ! ");
this.serverSession = serverSession;
}
public HttpSession getSession(boolean create) {
if( serverSession!=null ) {
// if not null, it is validated by the session module
// when initially requested, but an invalidated session
// must be rechecked
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -