diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index fa1d6d995..cd540add0 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,10 @@ Changes since 4.1 ALPHA1 ------------------- +* [HTTPCLIENT-916] UsernamePasswordCredentials, NTUserPrincipal, + BasicClientCookie, BasicClientCookie2 and BasicCookieStore made Serializable. + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-914] Upgraded Commons Codec dependency to version 1.4 Contributed by Oleg Kalnichevski diff --git a/httpclient/src/main/java/org/apache/http/auth/BasicUserPrincipal.java b/httpclient/src/main/java/org/apache/http/auth/BasicUserPrincipal.java index fdceb934c..4b0b3fb1d 100644 --- a/httpclient/src/main/java/org/apache/http/auth/BasicUserPrincipal.java +++ b/httpclient/src/main/java/org/apache/http/auth/BasicUserPrincipal.java @@ -26,6 +26,7 @@ package org.apache.http.auth; +import java.io.Serializable; import java.security.Principal; import org.apache.http.annotation.Immutable; @@ -34,12 +35,13 @@ import org.apache.http.util.LangUtils; /** * Basic user principal used for HTTP authentication - * * * @since 4.0 */ @Immutable -public final class BasicUserPrincipal implements Principal { +public final class BasicUserPrincipal implements Principal, Serializable { + + private static final long serialVersionUID = -2266305184969850467L; private final String username; diff --git a/httpclient/src/main/java/org/apache/http/auth/NTCredentials.java b/httpclient/src/main/java/org/apache/http/auth/NTCredentials.java index 121905a96..efc372ba9 100644 --- a/httpclient/src/main/java/org/apache/http/auth/NTCredentials.java +++ b/httpclient/src/main/java/org/apache/http/auth/NTCredentials.java @@ -26,6 +26,7 @@ package org.apache.http.auth; +import java.io.Serializable; import java.security.Principal; import java.util.Locale; @@ -40,7 +41,9 @@ import org.apache.http.util.LangUtils; * @since 4.0 */ @Immutable -public class NTCredentials implements Credentials { +public class NTCredentials implements Credentials, Serializable { + + private static final long serialVersionUID = -7385699315228907265L; /** The user principal */ private final NTUserPrincipal principal; diff --git a/httpclient/src/main/java/org/apache/http/auth/NTUserPrincipal.java b/httpclient/src/main/java/org/apache/http/auth/NTUserPrincipal.java index 14104ccd5..d7cddb881 100644 --- a/httpclient/src/main/java/org/apache/http/auth/NTUserPrincipal.java +++ b/httpclient/src/main/java/org/apache/http/auth/NTUserPrincipal.java @@ -26,6 +26,7 @@ package org.apache.http.auth; +import java.io.Serializable; import java.security.Principal; import java.util.Locale; @@ -39,8 +40,10 @@ import org.apache.http.util.LangUtils; * @since 4.0 */ @Immutable -public class NTUserPrincipal implements Principal { +public class NTUserPrincipal implements Principal, Serializable { + private static final long serialVersionUID = -6870169797924406894L; + private final String username; private final String domain; private final String ntname; diff --git a/httpclient/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java b/httpclient/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java index 6535aed04..004368f29 100644 --- a/httpclient/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java +++ b/httpclient/src/main/java/org/apache/http/auth/UsernamePasswordCredentials.java @@ -26,6 +26,7 @@ package org.apache.http.auth; +import java.io.Serializable; import java.security.Principal; import org.apache.http.annotation.Immutable; @@ -39,7 +40,9 @@ import org.apache.http.util.LangUtils; * @since 4.0 */ @Immutable -public class UsernamePasswordCredentials implements Credentials { +public class UsernamePasswordCredentials implements Credentials, Serializable { + + private static final long serialVersionUID = 243343858802739403L; private final BasicUserPrincipal principal; private final String password; diff --git a/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java b/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java index 7c5300f7b..5121b2ba5 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/BasicCookieStore.java @@ -26,6 +26,7 @@ package org.apache.http.impl.client; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -47,7 +48,9 @@ import org.apache.http.cookie.CookieIdentityComparator; * @since 4.0 */ @ThreadSafe -public class BasicCookieStore implements CookieStore { +public class BasicCookieStore implements CookieStore, Serializable { + + private static final long serialVersionUID = -1113466491038527240L; @GuardedBy("this") private final ArrayList cookies; diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie.java b/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie.java index 76b61feb3..88c223b67 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie.java @@ -27,6 +27,7 @@ package org.apache.http.impl.cookie; +import java.io.Serializable; import java.util.Date; import java.util.HashMap; import java.util.Locale; @@ -43,7 +44,9 @@ import org.apache.http.cookie.SetCookie; * @since 4.0 */ @NotThreadSafe -public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable { +public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable { + + private static final long serialVersionUID = -3869795591041535538L; /** * Default Constructor taking a name and a value. The value may be null. diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie2.java b/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie2.java index 85e5b0de5..154f8979c 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie2.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/BasicClientCookie2.java @@ -27,6 +27,7 @@ package org.apache.http.impl.cookie; +import java.io.Serializable; import java.util.Date; import org.apache.http.annotation.NotThreadSafe; @@ -39,8 +40,10 @@ import org.apache.http.cookie.SetCookie2; * @since 4.0 */ @NotThreadSafe -public class BasicClientCookie2 extends BasicClientCookie implements SetCookie2 { +public class BasicClientCookie2 extends BasicClientCookie implements SetCookie2, Serializable { + private static final long serialVersionUID = -7744598295706617057L; + private String commentURL; private int[] ports; private boolean discard; diff --git a/httpclient/src/test/java/org/apache/http/auth/TestCredentials.java b/httpclient/src/test/java/org/apache/http/auth/TestCredentials.java index 6be57a1d4..0d9c6b1ee 100644 --- a/httpclient/src/test/java/org/apache/http/auth/TestCredentials.java +++ b/httpclient/src/test/java/org/apache/http/auth/TestCredentials.java @@ -27,6 +27,11 @@ package org.apache.http.auth; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -205,4 +210,31 @@ public class TestCredentials extends TestCase { assertTrue(creds9.equals(creds7)); } + + public void testUsernamePasswordCredentialsSerialization() throws Exception { + UsernamePasswordCredentials orig = new UsernamePasswordCredentials("name", "pwd"); + ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); + ObjectOutputStream outstream = new ObjectOutputStream(outbuffer); + outstream.writeObject(orig); + outstream.close(); + byte[] raw = outbuffer.toByteArray(); + ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw); + ObjectInputStream instream = new ObjectInputStream(inbuffer); + UsernamePasswordCredentials clone = (UsernamePasswordCredentials) instream.readObject(); + assertEquals(orig, clone); + } + + public void testNTCredentialsSerialization() throws Exception { + NTCredentials orig = new NTCredentials("name", "pwd", "somehost", "domain"); + ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); + ObjectOutputStream outstream = new ObjectOutputStream(outbuffer); + outstream.writeObject(orig); + outstream.close(); + byte[] raw = outbuffer.toByteArray(); + ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw); + ObjectInputStream instream = new ObjectInputStream(inbuffer); + NTCredentials clone = (NTCredentials) instream.readObject(); + assertEquals(orig, clone); + } + } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestBasicCookieStore.java b/httpclient/src/test/java/org/apache/http/impl/client/TestBasicCookieStore.java new file mode 100644 index 000000000..3a721e2db --- /dev/null +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestBasicCookieStore.java @@ -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. + * ==================================================================== + * + * 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 + * . + * + */ + +package org.apache.http.impl.client; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Calendar; +import java.util.List; + +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.cookie.BasicClientCookie; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit tests for {@link BasicCookieStore}. + */ +public class TestBasicCookieStore extends TestCase { + + public TestBasicCookieStore(String testName) { + super(testName); + } + + public static void main(String args[]) { + String[] testCaseName = { TestBasicCookieStore.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public static Test suite() { + return new TestSuite(TestBasicCookieStore.class); + } + + public void testBasics() throws Exception { + BasicCookieStore store = new BasicCookieStore(); + store.addCookie(new BasicClientCookie("name1", "value1")); + store.addCookies(new BasicClientCookie[] {new BasicClientCookie("name2", "value2")}); + List l = store.getCookies(); + assertNotNull(l); + assertEquals(2, l.size()); + assertEquals("name1", l.get(0).getName()); + assertEquals("name2", l.get(1).getName()); + store.clear(); + l = store.getCookies(); + assertNotNull(l); + assertEquals(0, l.size()); + } + + public void testExpiredCookie() throws Exception { + BasicCookieStore store = new BasicCookieStore(); + BasicClientCookie cookie = new BasicClientCookie("name1", "value1"); + + Calendar c = Calendar.getInstance(); + c.add(Calendar.DAY_OF_YEAR, -10); + cookie.setExpiryDate(c.getTime()); + store.addCookie(cookie); + List l = store.getCookies(); + assertNotNull(l); + assertEquals(0, l.size()); + } + + public void testSerialization() throws Exception { + BasicCookieStore orig = new BasicCookieStore(); + orig.addCookie(new BasicClientCookie("name1", "value1")); + orig.addCookie(new BasicClientCookie("name2", "value2")); + ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); + ObjectOutputStream outstream = new ObjectOutputStream(outbuffer); + outstream.writeObject(orig); + outstream.close(); + byte[] raw = outbuffer.toByteArray(); + ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw); + ObjectInputStream instream = new ObjectInputStream(inbuffer); + BasicCookieStore clone = (BasicCookieStore) instream.readObject(); + List expected = orig.getCookies(); + List clones = clone.getCookies(); + assertNotNull(expected); + assertNotNull(clones); + assertEquals(expected.size(), clones.size()); + for (int i = 0; i < expected.size(); i++) { + assertEquals(expected.get(i).getName(), clones.get(i).getName()); + assertEquals(expected.get(i).getValue(), clones.get(i).getValue()); + } + } + +} diff --git a/httpclient/src/test/java/org/apache/http/impl/cookie/TestBasicClientCookie.java b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBasicClientCookie.java new file mode 100644 index 000000000..5841668aa --- /dev/null +++ b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBasicClientCookie.java @@ -0,0 +1,102 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * 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 + * . + * + */ + +package org.apache.http.impl.cookie; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit tests for {@link BasicClientCookie}. + */ +public class TestBasicClientCookie extends TestCase { + + public TestBasicClientCookie(String testName) { + super(testName); + } + + public static void main(String args[]) { + String[] testCaseName = { TestBasicClientCookie.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public static Test suite() { + return new TestSuite(TestBasicClientCookie.class); + } + + public void testConstructor() { + BasicClientCookie cookie = new BasicClientCookie("name", "value"); + assertEquals("name", cookie.getName()); + assertEquals("value", cookie.getValue()); + try { + new BasicClientCookie(null, null); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + //expected + } + } + + public void testCloning() throws Exception { + BasicClientCookie orig = new BasicClientCookie("name", "value"); + orig.setDomain("domain"); + orig.setPath("/"); + orig.setAttribute("attrib", "stuff"); + BasicClientCookie clone = (BasicClientCookie) orig.clone(); + assertEquals(orig.getName(), clone.getName()); + assertEquals(orig.getValue(), clone.getValue()); + assertEquals(orig.getDomain(), clone.getDomain()); + assertEquals(orig.getPath(), clone.getPath()); + assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib")); + } + + public void testSerialization() throws Exception { + BasicClientCookie orig = new BasicClientCookie("name", "value"); + orig.setDomain("domain"); + orig.setPath("/"); + orig.setAttribute("attrib", "stuff"); + ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); + ObjectOutputStream outstream = new ObjectOutputStream(outbuffer); + outstream.writeObject(orig); + outstream.close(); + byte[] raw = outbuffer.toByteArray(); + ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw); + ObjectInputStream instream = new ObjectInputStream(inbuffer); + BasicClientCookie clone = (BasicClientCookie) instream.readObject(); + assertEquals(orig.getName(), clone.getName()); + assertEquals(orig.getValue(), clone.getValue()); + assertEquals(orig.getDomain(), clone.getDomain()); + assertEquals(orig.getPath(), clone.getPath()); + assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib")); + } + +} diff --git a/httpclient/src/test/java/org/apache/http/impl/cookie/TestBasicClientCookie2.java b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBasicClientCookie2.java new file mode 100644 index 000000000..340321dc9 --- /dev/null +++ b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBasicClientCookie2.java @@ -0,0 +1,115 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * 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 + * . + * + */ + +package org.apache.http.impl.cookie; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit tests for {@link BasicClientCookie2}. + */ +public class TestBasicClientCookie2 extends TestCase { + + public TestBasicClientCookie2(String testName) { + super(testName); + } + + public static void main(String args[]) { + String[] testCaseName = { TestBasicClientCookie2.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public static Test suite() { + return new TestSuite(TestBasicClientCookie2.class); + } + + public void testConstructor() { + BasicClientCookie2 cookie = new BasicClientCookie2("name", "value"); + assertEquals("name", cookie.getName()); + assertEquals("value", cookie.getValue()); + try { + new BasicClientCookie2(null, null); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + //expected + } + } + + public void testCloning() throws Exception { + BasicClientCookie2 orig = new BasicClientCookie2("name", "value"); + orig.setDomain("domain"); + orig.setPath("/"); + orig.setAttribute("attrib", "stuff"); + orig.setPorts(new int[] {80, 8080}); + BasicClientCookie2 clone = (BasicClientCookie2) orig.clone(); + assertEquals(orig.getName(), clone.getName()); + assertEquals(orig.getValue(), clone.getValue()); + assertEquals(orig.getDomain(), clone.getDomain()); + assertEquals(orig.getPath(), clone.getPath()); + assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib")); + assertEquals(orig.getPorts().length, clone.getPorts().length); + assertEquals(orig.getPorts()[0], clone.getPorts()[0]); + assertEquals(orig.getPorts()[1], clone.getPorts()[1]); + } + + public void testSerialization() throws Exception { + BasicClientCookie2 orig = new BasicClientCookie2("name", "value"); + orig.setDomain("domain"); + orig.setPath("/"); + orig.setAttribute("attrib", "stuff"); + orig.setPorts(new int[] {80, 8080}); + ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); + ObjectOutputStream outstream = new ObjectOutputStream(outbuffer); + outstream.writeObject(orig); + outstream.close(); + byte[] raw = outbuffer.toByteArray(); + ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw); + ObjectInputStream instream = new ObjectInputStream(inbuffer); + BasicClientCookie2 clone = (BasicClientCookie2) instream.readObject(); + assertEquals(orig.getName(), clone.getName()); + assertEquals(orig.getValue(), clone.getValue()); + assertEquals(orig.getDomain(), clone.getDomain()); + assertEquals(orig.getPath(), clone.getPath()); + assertEquals(orig.getAttribute("attrib"), clone.getAttribute("attrib")); + int[] expected = orig.getPorts(); + int[] clones = clone.getPorts(); + assertNotNull(expected); + assertNotNull(clones); + assertEquals(expected.length, clones.length); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], clones[i]); + } + } + +}