You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/05/25 17:41:11 UTC
svn commit: r409408 - in /jakarta/httpcomponents/httpclient/trunk/src:
java/org/apache/http/cookie/impl/BasicExpiresHandler.java
test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
Author: olegk
Date: Thu May 25 08:41:10 2006
New Revision: 409408
URL: http://svn.apache.org/viewvc?rev=409408&view=rev
Log:
Added cookie handler impls for 'expires' attribute shared by all common cookie specs
Added:
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java (with props)
Modified:
jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java?rev=409408&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java Thu May 25 08:41:10 2006
@@ -0,0 +1,72 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software 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.
+ * ====================================================================
+ *
+ * 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/>.
+ *
+ */
+package org.apache.http.cookie.impl;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.CookieOrigin;
+import org.apache.http.cookie.MalformedCookieException;
+import org.apache.http.util.DateParseException;
+import org.apache.http.util.DateUtils;
+
+public class BasicExpiresHandler extends AbstractCookieAttributeHandler {
+
+ /** Valid date patterns */
+ private final String[] datepatterns;
+
+ public BasicExpiresHandler(final String[] datepatterns) {
+ if (datepatterns == null) {
+ throw new IllegalArgumentException("Array of date patterns may not be null");
+ }
+ this.datepatterns = (String[]) datepatterns.clone();
+ }
+
+ public void parse(final Cookie cookie, final String value)
+ throws MalformedCookieException {
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
+ }
+ if (value == null) {
+ throw new MalformedCookieException("Missing value for expires attribute");
+ }
+ try {
+ cookie.setExpiryDate(DateUtils.parseDate(value, this.datepatterns));
+ } catch (DateParseException dpe) {
+ throw new MalformedCookieException("Unable to parse expires attribute: "
+ + value);
+ }
+ }
+
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
+ }
+ return !cookie.isExpired();
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java?rev=409408&r1=409407&r2=409408&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java Thu May 25 08:41:10 2006
@@ -28,10 +28,15 @@
package org.apache.http.cookie.impl;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieAttributeHandler;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.MalformedCookieException;
+import org.apache.http.util.DateUtils;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -260,4 +265,70 @@
}
}
+ public void testBasicExpiresParse() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
+
+ DateFormat dateformat = new SimpleDateFormat(DateUtils.PATTERN_RFC1123);
+ dateformat.setTimeZone(DateUtils.GMT);
+
+ Date now = new Date();
+
+ h.parse(cookie, dateformat.format(now));
+ assertNotNull(cookie.getExpiryDate());
+ }
+
+ public void testBasicExpiresParseInvalid() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
+ try {
+ h.parse(cookie, "garbage");
+ fail("MalformedCookieException must have been thrown");
+ } catch (MalformedCookieException ex) {
+ // expected
+ }
+ try {
+ h.parse(cookie, null);
+ fail("MalformedCookieException must have been thrown");
+ } catch (MalformedCookieException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicExpiresMatch() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
+
+ Date past = new Date(System.currentTimeMillis() - 20000000);
+ Date future = new Date(System.currentTimeMillis() + 20000000);
+
+ cookie.setExpiryDate(past);
+ assertFalse(h.match(cookie, origin));
+ cookie.setExpiryDate(future);
+ assertTrue(h.match(cookie, origin));
+ }
+
+ public void testBasicExpiresInvalidInput() throws Exception {
+ try {
+ new BasicExpiresHandler(null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
+ try {
+ h.parse(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ h.match(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
}