Author: markt
Date: Fri Aug  7 19:15:54 2009
New Revision: 802146

URL: http://svn.apache.org/viewvc?rev=802146&view=rev
Log:
Complete implementation of SessionCookieConfig

Added:
    
tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java  
 (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/connector/Request.java
    tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
    tomcat/trunk/java/org/apache/catalina/ha/session/JvmRouteBinderValve.java
    tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings_es.properties

Modified: tomcat/trunk/java/org/apache/catalina/connector/Request.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/Request.java?rev=802146&r1=802145&r2=802146&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/Request.java (original)
+++ tomcat/trunk/java/org/apache/catalina/connector/Request.java Fri Aug  7 
19:15:54 2009
@@ -48,7 +48,6 @@
 import javax.servlet.ServletRequestAttributeEvent;
 import javax.servlet.ServletRequestAttributeListener;
 import javax.servlet.ServletResponse;
-import javax.servlet.SessionCookieConfig;
 import javax.servlet.SessionTrackingMode;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
@@ -63,6 +62,7 @@
 import org.apache.catalina.Realm;
 import org.apache.catalina.Session;
 import org.apache.catalina.Wrapper;
+import org.apache.catalina.core.ApplicationSessionCookieConfig;
 import org.apache.catalina.core.AsyncContextImpl;
 import org.apache.catalina.realm.GenericPrincipal;
 import org.apache.catalina.util.Enumerator;
@@ -2384,9 +2384,15 @@
                && getContext().getServletContext().
                        getEffectiveSessionTrackingModes().contains(
                                SessionTrackingMode.COOKIE)) {
-            Cookie cookie = new Cookie(Globals.SESSION_COOKIE_NAME,
-                                       session.getIdInternal());
-            configureSessionCookie(cookie);
+            Cookie cookie =
+                ApplicationSessionCookieConfig.createSessionCookie(
+                        context.getServletContext().getSessionCookieConfig(),
+                        session.getIdInternal(),
+                        isSecure(),
+                        context.getUseHttpOnly(),
+                        connector.getEmptySessionPath(),
+                        context.getEncodedPath());
+            
             response.addCookieInternal(cookie);
         }
 
@@ -2399,50 +2405,6 @@
 
     }
 
-    /**
-     * Configures the given JSESSIONID cookie.
-     *
-     * @param cookie The JSESSIONID cookie to be configured
-     */
-    protected void configureSessionCookie(Cookie cookie) {
-        SessionCookieConfig scc =
-            context.getServletContext().getSessionCookieConfig();
-
-        cookie.setMaxAge(-1);
-
-        if (scc != null) {
-            cookie.setComment(scc.getComment());
-        }
-
-        if (scc != null) {
-            cookie.setDomain(scc.getDomain());
-        }
-
-        if ((scc != null && scc.isSecure()) || isSecure()) {
-            cookie.setSecure(true);
-        }
-
-        if ((scc != null && scc.isHttpOnly()) ||
-                context.getUseHttpOnly()) {
-            cookie.setHttpOnly(true);
-        }
-        
-        if (!connector.getEmptySessionPath() &&
-                scc != null && scc.getPath() != null) {
-            cookie.setPath(scc.getPath());
-        } else {
-            String contextPath = null;
-            if (!connector.getEmptySessionPath() && (getContext() != null)) {
-                contextPath = getContext().getEncodedPath();
-            }
-            if ((contextPath != null) && (contextPath.length() > 0)) {
-                cookie.setPath(contextPath);
-            } else {
-                cookie.setPath("/");
-            }
-        }
-    }
-    
     protected String unescape(String s) {
         if (s==null) return null;
         if (s.indexOf('\\') == -1) return s;

Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java?rev=802146&r1=802145&r2=802146&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Fri Aug  
7 19:15:54 2009
@@ -171,7 +171,8 @@
     /**
      * Session Cookie config
      */
-    private SessionCookieConfig sessionCookieConfig;
+    private SessionCookieConfig sessionCookieConfig =
+        new ApplicationSessionCookieConfig();
     
     /**
      * Session tracking modes

Added: 
tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java?rev=802146&view=auto
==============================================================================
--- 
tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java 
(added)
+++ 
tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java 
Fri Aug  7 19:15:54 2009
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.catalina.core;
+
+import javax.servlet.SessionCookieConfig;
+import javax.servlet.http.Cookie;
+
+import org.apache.catalina.Globals;
+
+public class ApplicationSessionCookieConfig implements SessionCookieConfig {
+
+    private boolean httpOnly;
+    private boolean secure;
+    private int maxAge = -1;
+    private String comment;
+    private String domain;
+    private String name;
+    private String path;
+    
+    @Override
+    public String getComment() {
+        return comment;
+    }
+
+    @Override
+    public String getDomain() {
+        return domain;
+    }
+
+    @Override
+    public int getMaxAge() {
+        return maxAge;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public String getPath() {
+        return path;
+    }
+
+    @Override
+    public boolean isHttpOnly() {
+        return httpOnly;
+    }
+
+    @Override
+    public boolean isSecure() {
+        return secure;
+    }
+
+    @Override
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
+
+    @Override
+    public void setDomain(String domain) {
+        this.domain = domain;
+    }
+
+    @Override
+    public void setHttpOnly(boolean httpOnly) {
+        this.httpOnly = httpOnly;
+    }
+
+    @Override
+    public void setMaxAge(int maxAge) {
+        this.maxAge = maxAge;
+    }
+
+    @Override
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    @Override
+    public void setSecure(boolean secure) {
+        this.secure = secure;
+    }
+
+    /**
+     * Creates a new session cookie for the given session ID
+     *
+     * @param scc         The default session cookie configuration
+     * @param sessionId   The ID of the session for which the cookie will be
+     *                    created
+     * @param secure      Should session cookie be configured as secure
+     * @param httpOnly    Should session cookie be configured as httpOnly
+     * @param emptyPath   Should session cookie be configured with empty path
+     * @param contextPath Context path to use if required       
+     */
+    public static Cookie createSessionCookie(SessionCookieConfig scc,
+            String sessionId, boolean secure, boolean httpOnly,
+            boolean emptyPath, String contextPath) {
+
+       // Session config can over-ride default name  
+       String cookieName = scc.getName();
+       if (cookieName == null) {
+           cookieName = Globals.SESSION_COOKIE_NAME;
+       }
+       Cookie cookie = new Cookie(cookieName, sessionId);
+       
+       // Just apply the defaults.
+       cookie.setMaxAge(scc.getMaxAge());
+       cookie.setComment(scc.getComment());
+       // Avoid possible NPE
+       if (scc.getDomain() != null) {
+           cookie.setDomain(scc.getDomain());
+       }
+
+       // Always set secure if the request is secure
+       if (scc.isSecure() || secure) {
+           cookie.setSecure(true);
+       }
+
+       // Always set httpOnly if the context is configured for that
+       if (scc.isHttpOnly() || httpOnly) {
+           cookie.setHttpOnly(true);
+       }
+       
+       // Don't set the path if the connector is configured to over-ride
+       if (!emptyPath && scc.getPath() != null) {
+           cookie.setPath(scc.getPath());
+       } else {
+           if (!emptyPath && contextPath != null && (contextPath.length() > 
0)) {
+               cookie.setPath(contextPath);
+           } else {
+               cookie.setPath("/");
+           }
+       }
+       return cookie;
+   }
+   
+ 
+}

Propchange: 
tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision

Modified: 
tomcat/trunk/java/org/apache/catalina/ha/session/JvmRouteBinderValve.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/session/JvmRouteBinderValve.java?rev=802146&r1=802145&r2=802146&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/session/JvmRouteBinderValve.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/ha/session/JvmRouteBinderValve.java 
Fri Aug  7 19:15:54 2009
@@ -25,7 +25,6 @@
 import org.apache.catalina.Container;
 import org.apache.catalina.Context;
 import org.apache.catalina.Engine;
-import org.apache.catalina.Globals;
 import org.apache.catalina.Host;
 import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleException;
@@ -39,6 +38,7 @@
 import org.apache.catalina.ha.session.DeltaSession;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
+import org.apache.catalina.core.ApplicationSessionCookieConfig;
 import org.apache.catalina.session.ManagerBase;
 import org.apache.catalina.session.PersistentManager;
 import org.apache.catalina.util.LifecycleSupport;
@@ -442,8 +442,7 @@
     }
 
     /**
-     * Sets a new cookie for the given session id and response and see
-     * {...@link 
org.apache.catalina.connector.Request#configureSessionCookie(javax.servlet.http.Cookie)}
+     * Sets a new cookie for the given session id and response
      * 
      * @param request current request
      * @param response Tomcat Response
@@ -456,27 +455,20 @@
             if (context.getServletContext().getEffectiveSessionTrackingModes()
                     .contains(SessionTrackingMode.COOKIE)) {
                 // set a new session cookie
-                Cookie newCookie = new Cookie(Globals.SESSION_COOKIE_NAME,
-                        sessionId);
-                newCookie.setMaxAge(-1);
-                String contextPath = null;
-                if (!response.getConnector().getEmptySessionPath()
-                        && (context != null)) {
-                    contextPath = context.getEncodedPath();
-                }
-                if ((contextPath != null) && (contextPath.length() > 0)) {
-                    newCookie.setPath(contextPath);
-                } else {
-                    newCookie.setPath("/");
-                }
-                if (request.isSecure()) {
-                    newCookie.setSecure(true);
-                }
+                Cookie newCookie =
+                    ApplicationSessionCookieConfig.createSessionCookie(
+                            
context.getServletContext().getSessionCookieConfig(),
+                            sessionId,
+                            request.isSecure(),
+                            context.getUseHttpOnly(),
+                            response.getConnector().getEmptySessionPath(),
+                            context.getEncodedPath()); 
+                    
                 if (log.isDebugEnabled()) {
                     log.debug(sm.getString("jvmRoute.newSessionCookie",
-                            sessionId, Globals.SESSION_COOKIE_NAME, newCookie
-                                    .getPath(), new Boolean(newCookie
-                                    .getSecure())));
+                            sessionId, newCookie.getName(), 
newCookie.getPath(),
+                            Boolean.valueOf(newCookie.getSecure()),
+                            Boolean.valueOf(newCookie.isHttpOnly())));
                 }
                 response.addCookie(newCookie);
             }

Modified: 
tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings.properties?rev=802146&r1=802145&r2=802146&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings.properties 
Fri Aug  7 19:15:54 2009
@@ -78,7 +78,7 @@
 jvmRoute.listener.stopped=SessionID Binder Listener stopped
 jvmRoute.lostSession=Lost Session [{0}] at path [{1}]
 jvmRoute.missingJvmRouteAttribute=No engine jvmRoute attribute configured!
-jvmRoute.newSessionCookie=Setting cookie with session id [{0}] name: [{1}] 
path: [{2}] secure: [{3}]
+jvmRoute.newSessionCookie=Setting cookie with session id [{0}] name: [{1}] 
path: [{2}] secure: [{3}] httpOnly: [{4}]
 jvmRoute.noCluster=The JvmRouterBinderValve is configured, but clustering is 
not being used. Fail over will still work, providing a PersistentManager is 
used.
 jvmRoute.notFoundManager=Not found Cluster DeltaManager {0} at {1}
 jvmRoute.receiveMessage.sessionIDChanged=Cluster 
JvmRouteSessionIDBinderListener received orginal session ID [{0}] set to new id 
[{1}] for context path [{2}]

Modified: 
tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings_es.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings_es.properties?rev=802146&r1=802145&r2=802146&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings_es.properties 
(original)
+++ tomcat/trunk/java/org/apache/catalina/ha/session/LocalStrings_es.properties 
Fri Aug  7 19:15:54 2009
@@ -78,7 +78,7 @@
 jvmRoute.listener.stopped = Parado Oyente Ligador de SessionID
 jvmRoute.lostSession = Perdida Sesi\u00F3n [{0}] en ruta [{1}]
 jvmRoute.missingJvmRouteAttribute = \u00A1No se ha configurado atributo de 
motor jvmRoute\!
-jvmRoute.newSessionCookie = Poniendo cookie con id de sesi\u00F3n [{0}] 
nombre\: [{1}] ruta\: [{2}] seguro\: [{3}]
+jvmRoute.newSessionCookie = Poniendo cookie con id de sesi\u00F3n [{0}] 
nombre\: [{1}] ruta\: [{2}] seguro\: [{3}] httpOnly\: [{4}]
 jvmRoute.notFoundManager = No hallado Cl\u00FAster DeltaManager {0} en {1}
 jvmRoute.receiveMessage.sessionIDChanged = Cl\u00FAster 
JvmRouteSessionIDBinderListener recibi\u00F3 ID original de sesi\u00F3n [{0}] 
puesto a nuevo id [{1}] para la ruta de contexto [{2}]
 jvmRoute.run.already = receptor jvmRoute SessionID ya ejecutado



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to