You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by vi...@apache.org on 2013/05/15 10:31:33 UTC

svn commit: r1482720 - in /tomcat/trunk: java/org/apache/catalina/core/ test/org/apache/catalina/core/

Author: violetagg
Date: Wed May 15 08:31:32 2013
New Revision: 1482720

URL: http://svn.apache.org/r1482720
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54974
SessionCookieConfig.setXXX methods will throw IllegalStateException if the ServletContext from which this SessionCookieConfig was acquired has already been initialized.

Added:
    tomcat/trunk/test/org/apache/catalina/core/TestApplicationSessionCookieConfig.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
    tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java
    tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties

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=1482720&r1=1482719&r2=1482720&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Wed May 15 08:31:32 2013
@@ -117,6 +117,7 @@ public class ApplicationContext
         super();
         this.context = context;
         this.service = ((Engine) context.getParent().getParent()).getService();
+        this.sessionCookieConfig = new ApplicationSessionCookieConfig(context);
 
         // Populate session tracking modes
         populateSessionTrackingModes();
@@ -192,8 +193,7 @@ public class ApplicationContext
     /**
      * Session Cookie config
      */
-    private final SessionCookieConfig sessionCookieConfig =
-        new ApplicationSessionCookieConfig();
+    private SessionCookieConfig sessionCookieConfig;
 
     /**
      * Session tracking modes

Modified: 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=1482720&r1=1482719&r2=1482720&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationSessionCookieConfig.java Wed May 15 08:31:32 2013
@@ -21,10 +21,18 @@ import javax.servlet.SessionCookieConfig
 import javax.servlet.http.Cookie;
 
 import org.apache.catalina.Context;
+import org.apache.catalina.LifecycleState;
 import org.apache.catalina.util.SessionConfig;
+import org.apache.tomcat.util.res.StringManager;
 
 public class ApplicationSessionCookieConfig implements SessionCookieConfig {
 
+    /**
+     * The string manager for this package.
+     */
+    private static final StringManager sm = StringManager
+            .getManager(Constants.Package);
+
     private boolean httpOnly;
     private boolean secure;
     private int maxAge = -1;
@@ -32,6 +40,11 @@ public class ApplicationSessionCookieCon
     private String domain;
     private String name;
     private String path;
+    private StandardContext context;
+
+    public ApplicationSessionCookieConfig(StandardContext context) {
+        this.context = context;
+    }
 
     @Override
     public String getComment() {
@@ -70,36 +83,71 @@ public class ApplicationSessionCookieCon
 
     @Override
     public void setComment(String comment) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "comment",
+                    context.getPath()));
+        }
         this.comment = comment;
     }
 
     @Override
     public void setDomain(String domain) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "domain name",
+                    context.getPath()));
+        }
         this.domain = domain;
     }
 
     @Override
     public void setHttpOnly(boolean httpOnly) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "HttpOnly",
+                    context.getPath()));
+        }
         this.httpOnly = httpOnly;
     }
 
     @Override
     public void setMaxAge(int maxAge) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "max age",
+                    context.getPath()));
+        }
         this.maxAge = maxAge;
     }
 
     @Override
     public void setName(String name) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "name",
+                    context.getPath()));
+        }
         this.name = name;
     }
 
     @Override
     public void setPath(String path) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "path",
+                    context.getPath()));
+        }
         this.path = path;
     }
 
     @Override
     public void setSecure(boolean secure) {
+        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
+            throw new IllegalStateException(sm.getString(
+                    "applicationSessionCookieConfig.ise", "secure",
+                    context.getPath()));
+        }
         this.secure = secure;
     }
 

Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties?rev=1482720&r1=1482719&r2=1482720&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties (original)
+++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Wed May 15 08:31:32 2013
@@ -48,6 +48,7 @@ applicationFilterRegistration.nullInitPa
 applicationFilterRegistration.nullInitParams=Unable to set initialisation parameters for filter due to null name and/or value. Name [{0}], Value [{1}]
 applicationServletRegistration.setServletSecurity.iae=Null constraint specified for servlet [{0}] deployed to context with name [{1}]
 applicationServletRegistration.setServletSecurity.ise=Security constraints can't be added to servlet [{0}] deployed to context with name [{1}] as the context has already been initialised
+applicationSessionCookieConfig.ise=Property {0} can not be added to SessionCookieConfig for context {1} as the context has been initialised
 aprListener.aprInit=The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: {0}
 aprListener.tcnInvalid=An incompatible version {0} of the APR based Apache Tomcat Native library is installed, while Tomcat requires version {1}
 aprListener.tcnVersion=An older version {0} of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of {1}

Added: tomcat/trunk/test/org/apache/catalina/core/TestApplicationSessionCookieConfig.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationSessionCookieConfig.java?rev=1482720&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestApplicationSessionCookieConfig.java (added)
+++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationSessionCookieConfig.java Wed May 15 08:31:32 2013
@@ -0,0 +1,141 @@
+/*
+ * 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 static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.catalina.LifecycleState;
+
+public class TestApplicationSessionCookieConfig {
+    private ApplicationSessionCookieConfig applicationSessionCookieConfig;
+    private final CustomContext context = new CustomContext();
+
+    @Before
+    public void setUp() throws Exception {
+        applicationSessionCookieConfig = new ApplicationSessionCookieConfig(
+                context);
+    }
+
+    @Test
+    public void testSetCommentInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setComment("test");
+        assertTrue(applicationSessionCookieConfig.getComment().equals("test"));
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetCommentNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setComment("test");
+    }
+
+    @Test
+    public void testSetDomainInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setDomain("test");
+        assertTrue(applicationSessionCookieConfig.getDomain().equals("test"));
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetDomainNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setDomain("test");
+    }
+
+    @Test
+    public void testSetHttpOnlyInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setHttpOnly(true);
+        assertTrue(applicationSessionCookieConfig.isHttpOnly());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetHttpOnlyNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setHttpOnly(true);
+    }
+
+    @Test
+    public void testSetMaxAgeInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setMaxAge(1);
+        assertTrue(applicationSessionCookieConfig.getMaxAge() == 1);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetMaxAgeNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setMaxAge(1);
+    }
+
+    @Test
+    public void testSetNameInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setName("test");
+        assertTrue(applicationSessionCookieConfig.getName().equals("test"));
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetNameNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setName("test");
+    }
+
+    @Test
+    public void testSetPathInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setPath("test");
+        assertTrue(applicationSessionCookieConfig.getPath().equals("test"));
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetPathNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setPath("test");
+    }
+
+    @Test
+    public void testSetSecureInitPhase() {
+        context.setState(LifecycleState.STARTING_PREP);
+        applicationSessionCookieConfig.setSecure(true);
+        assertTrue(applicationSessionCookieConfig.isSecure());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testSetSecureNotInitPhase() {
+        context.setState(LifecycleState.STARTED);
+        applicationSessionCookieConfig.setSecure(true);
+    }
+
+    private static class CustomContext extends StandardContext {
+        private LifecycleState state;
+
+        @Override
+        public LifecycleState getState() {
+            return state;
+        }
+
+        @Override
+        public synchronized void setState(LifecycleState state) {
+            this.state = state;
+        }
+    }
+}

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



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