Index: modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java =================================================================== --- modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java (revision 0) +++ modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/CookiePolicyTest.java (revision 0) @@ -0,0 +1,113 @@ +/* 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.harmony.luni.tests.java.net; + +import java.net.CookiePolicy; +import java.net.HttpCookie; +import java.net.URI; +import java.net.URISyntaxException; + +import junit.framework.TestCase; + +public class CookiePolicyTest extends TestCase { + + /** + * @tests java.net.CookiePolicy#shouldAccept(java.net.URI, + * java.net.HttpCookie). + * + * @since 1.6 + */ + public void test_ShouldAccept_LURI_LHttpCookie() throws URISyntaxException { + HttpCookie cookie = new HttpCookie("Harmony_6", "ongoing"); + URI uri = new URI(""); + try { + CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, cookie); + fail("Should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, null); + fail("Should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + try { + CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, null); + fail("Should throw NullPointerException"); + } catch (NullPointerException e) { + // expected + } + + // Policy: ACCEPT_ALL, always returns true + boolean accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, cookie); + assertTrue(accept); + + accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, null); + assertTrue(accept); + + accept = CookiePolicy.ACCEPT_ALL.shouldAccept(uri, null); + assertTrue(accept); + + // Policy: ACCEPT_NONE, always returns false + accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, cookie); + assertFalse(accept); + + accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, null); + assertFalse(accept); + + accept = CookiePolicy.ACCEPT_NONE.shouldAccept(uri, null); + assertFalse(accept); + + // Policy: ACCEPT_ORIGINAL_SERVER + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie); + assertFalse(accept); + + cookie.setDomain(".b.c"); + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI( + "schema://a.b.c"), cookie); + assertTrue(accept); + + cookie.setDomain(".b.c"); + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI( + "s://a.b.c.d"), cookie); + assertFalse(accept); + + cookie.setDomain("b.c"); + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI( + "s://a.b.c.d"), cookie); + assertFalse(accept); + + cookie.setDomain("a.b.c.d"); + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI( + "s://a.b.c.d"), cookie); + assertTrue(accept); + + cookie.setDomain("."); + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI( + "s://a.b.c.d"), cookie); + assertFalse(accept); + + cookie.setDomain(""); + accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI( + "s://a.b.c.d"), cookie); + assertFalse(accept); + } + +} Index: modules/luni/src/main/java/java/net/CookiePolicy.java =================================================================== --- modules/luni/src/main/java/java/net/CookiePolicy.java (revision 0) +++ modules/luni/src/main/java/java/net/CookiePolicy.java (revision 0) @@ -0,0 +1,74 @@ +/* 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 java.net; + +/** + * CookiePolicy has three pre-defined policy. They are ACCEPT_ALL, ACCEPT_NONE + * and ACCEPT_ORIGINAL_SERVER respectively. They are used to decide which cookies + * should be accepted and which should not be. + * + * See RFC 2965 sec. 3.3 & 7 for more detail. + * + * @since 1.6 + */ +public interface CookiePolicy { + + /** + * A pre-defined policy, accepts all cookies. + */ + static final CookiePolicy ACCEPT_ALL = new CookiePolicy() { + + public boolean shouldAccept(URI uri, HttpCookie cookie) { + return true; + } + + }; + + /** + * A pre-defined policy, accepts no cookies at all. + */ + static final CookiePolicy ACCEPT_NONE = new CookiePolicy() { + + public boolean shouldAccept(URI uri, HttpCookie cookie) { + return false; + } + + }; + + /** + * A pre-defined policy ,only accepts cookies from original server. + */ + static final CookiePolicy ACCEPT_ORIGINAL_SERVER = new CookiePolicy() { + + public boolean shouldAccept(URI uri, HttpCookie cookie) { + return HttpCookie.domainMatches(cookie.getDomain(), uri.getHost()); + } + + }; + + /** + * This method is used to determine whether or not the specified cookie + * should be accepted. + * + * @param uri + * the URI to used to determine acceptability + * @param cookie + * the HttpCookie to be determined + * @return true if this cookie should be accepted; false otherwise + */ + boolean shouldAccept(URI uri, HttpCookie cookie); +}