|
What this is
Other links
The source code
/*
* Copyright 1999-2004 The Apache Sofware 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.modules.session;
import java.io.File;
import org.apache.tomcat.core.BaseInterceptor;
import org.apache.tomcat.core.Container;
import org.apache.tomcat.core.Context;
import org.apache.tomcat.core.Request;
import org.apache.tomcat.core.Response;
import org.apache.tomcat.core.ServerSession;
import org.apache.tomcat.util.http.ServerCookie;
/**
* Extract the session ID from the request using cookies and
* session rewriting.
*
* Will process the request and determine the session Id, and set it
* in the Request. It doesn't marks the session as accessed, and it
* doesn't retrieve or set the HttpSession - the storage and management
* of sessions is implemented in a separate module.
*
* This interceptor doesn't deal with any of the Session internals -
* it just works with the sessionID. A pluggable session manager
* ( or user-space manager !) will deal with marking the session
* as accessed or setting the session implementation and maintaining
* lifecycles.
*
* This implementation only handles Cookies and URL rewriting sessions,
* please extend or add new interceptors for other methods.
*
* You can set this interceptor to not use cookies, but only rewriting.
*
* @author costin@eng.sun.com
* @author Shai Fultheim [shai@brm.com]
*/
public class SessionId extends BaseInterceptor
{
// GS, separates the session id from the jvm route
static final char SESSIONID_ROUTE_SEP = '.';
boolean noCookies=false;
boolean cookiesFirst=true;
boolean checkSSLSessionId=false;
boolean ignoreCase=false;
boolean secureCookie=true;
public SessionId() {
ignoreCase= (File.separatorChar == '\\');
}
public void setCookiesFirst( boolean b ) {
cookiesFirst=b;
}
public void setNoCookies(boolean noCookies) {
this.noCookies = noCookies;
}
public void setCheckSSLSessionId(boolean checkSSLSessionId) {
this.checkSSLSessionId = checkSSLSessionId;
}
/** Is the path case-insenitive.
*/
public void setIgnoreCase(boolean ic) {
ignoreCase = ic;
}
public boolean getIgnoreCase() {
return ignoreCase;
}
/** Use secure cookies for SSL connections.
*/
public void setSecureCookie(boolean sc) {
secureCookie = sc;
}
public boolean getSecureCookie() {
return secureCookie;
}
/** Extract the session id from the request.
* SessionInterceptor will have to be called _before_ mapper,
* to avoid coding session stuff inside the mapper.
*
* When we fix the interceptors we'll have to specify something
* similar with the priority in apache hooks, right now it's just
* a config issue.
*/
public int postReadRequest(Request request ) {
if( request.getRequestedSessionId() != null ) {
// probably Apache already did that for us
return 0;
}
// quick test: if no extra path, no url rewriting.
if( request.requestURI().indexOf( ';' ) < 0 )
return 0;
// In case URI rewriting is used, extract the uri and fix
// the request.
String sig=";jsessionid=";
int foundAt=-1;
String sessionId;
if ((foundAt=request.requestURI().indexOf(sig))!=-1){
String uri=request.requestURI().toString();
sessionId=uri.substring(foundAt+sig.length());
// rewrite URL, do I need to do anything more?
request.requestURI().setString(uri.substring(0, foundAt));
// remove from unparsedURI too, if necessary
if( !request.unparsedURI().isNull() ) {
foundAt = request.unparsedURI().indexOf(sig);
if (foundAt!=-1) {
uri=request.unparsedURI().toString();
request.unparsedURI().setString(uri.substring(0, foundAt));
}
}
// No validate now - we just note that this is what the user
// requested.
request.setSessionIdSource( Request.SESSIONID_FROM_URL);
request.setRequestedSessionId( sessionId );
}
return 0;
}
/**
* Extract and set the session id and ServerSession.
* We know the Context - and all local interceptors can be used
* ( like session managers that are set per context ).
*
* This module knows about URI and cookies. It will validate the
* session id ( and set it only if valid ), and "touch" the
* session.
*/
public int requestMap(Request request ) {
String sessionId = null;
Context ctx=request.getContext();
if( ctx==null ) {
log( "Configuration error in StandardSessionInterceptor " +
" - no context " + request );
return 0;
}
int count=request.getCookies().getCookieCount();
ServerSession sess=null;
if( ! cookiesFirst ) {
// try the information from URL rewriting
sessionId= request.getRequestedSessionId();
sess=processSession( request, sessionId,
request.getSessionIdSource() );
if( debug>0 ) log("CookiesFirst==false, use url rewriting " + sess );
if( sess!=null )
return 0;
}
// Give priority to cookies. I don't know if that's part
// of the spec - XXX
for( int i=0; i
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 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.