[utilities COMMIT] /java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/net/CookieManager.java

noreply at shibboleth.net noreply at shibboleth.net
Fri Sep 18 10:34:52 EDT 2015


Author: scantor
Date: Fri Sep 18 10:34:51 2015
New Revision: 827

URL: http://svn.shibboleth.net/view/utilities?rev=827&view=rev
Log:
Add cookie read methods to CookieManager.

Modified:
    java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/net/CookieManager.java

Modified: java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/net/CookieManager.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/net/CookieManager.java?rev=827&r1=826&r2=827&view=diff
==============================================================================
--- java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/net/CookieManager.java	(original)
+++ java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/net/CookieManager.java	Fri Sep 18 10:34:51 2015
@@ -187,6 +187,46 @@
         
         httpResponse.addCookie(cookie);
     }
+
+    /**
+     * Check whether a cookie has a certain value.
+     * 
+     * @param name name of cookie
+     * @param expectedValue expected value of cookie
+     * 
+     * @return true iff the cookie exists and has the expected value
+     */
+    public boolean cookieHasValue(@Nonnull @NotEmpty final String name, @Nonnull @NotEmpty final String expectedValue) {
+        
+        final String realValue =  getCookieValue(name, null);
+        if (realValue == null) {
+            return false;
+        }
+        
+        return realValue.equals(expectedValue);
+    }
+    
+    /**
+     * Return the first matching cookie's value.
+     * 
+     * @param name cookie name
+     * @param defValue default value to return if the cookie isn't found
+     * 
+     * @return cookie value
+     */
+    @Nullable public String getCookieValue(@Nonnull @NotEmpty final String name, @Nullable final String defValue) {
+        
+        final Cookie[] cookies = httpRequest.getCookies();
+        if (cookies != null) {
+            for (final Cookie cookie : cookies) {
+                if (cookie.getName().equals(name)) {
+                    return cookie.getValue();
+                }
+            }
+        }
+        
+        return defValue;
+    }
     
     /**
      * Turn the servlet context path into an appropriate cookie path.



More information about the commits mailing list