HTTPCLIENT-916: UsernamePasswordCredentials, NTUserPrincipal, BasicClientCookie, BasicClientCookie2 and BasicCookieStore made Serializable

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@916658 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-02-26 12:19:06 +00:00
parent bac075735d
commit 1628039b85
12 changed files with 394 additions and 8 deletions

View File

@ -1,6 +1,10 @@
Changes since 4.1 ALPHA1
-------------------
* [HTTPCLIENT-916] UsernamePasswordCredentials, NTUserPrincipal,
BasicClientCookie, BasicClientCookie2 and BasicCookieStore made Serializable.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-914] Upgraded Commons Codec dependency to version 1.4
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<Cookie> cookies;

View File

@ -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.

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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<Cookie> 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<Cookie> 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<Cookie> expected = orig.getCookies();
List<Cookie> 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());
}
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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"));
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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]);
}
}
}