Return-Path: Delivered-To: apmail-hc-commits-archive@www.apache.org Received: (qmail 58688 invoked from network); 14 Apr 2008 14:37:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 14 Apr 2008 14:37:49 -0000 Received: (qmail 6642 invoked by uid 500); 14 Apr 2008 14:37:50 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 6620 invoked by uid 500); 14 Apr 2008 14:37:50 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 6611 invoked by uid 99); 14 Apr 2008 14:37:50 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Apr 2008 07:37:50 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Apr 2008 14:37:05 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id D433E1A9832; Mon, 14 Apr 2008 07:37:23 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r647816 - in /httpcomponents/httpcore/trunk: ./ module-main/src/main/java/org/apache/http/entity/ module-main/src/test/java/org/apache/http/entity/ Date: Mon, 14 Apr 2008 14:37:18 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080414143723.D433E1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Mon Apr 14 07:37:13 2008 New Revision: 647816 URL: http://svn.apache.org/viewvc?rev=647816&view=rev Log: HTTPCORE-150: Entity implementation that serializes a Java object Contributed by Andrea Selva Reviewed by Oleg Kalnichevski Added: httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java (with props) httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java (with props) Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestAllEntity.java Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=647816&r1=647815&r2=647816&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original) +++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Mon Apr 14 07:37:13 2008 @@ -1,6 +1,9 @@ Changes since 4.0 Beta 1 ------------------- +* [HTTPCORE-150] Entity implementation that serializes a Java object + Contributed by Andrea Selva + * [HTTPCORE-157] ChunkedOutputStream#flush() now behaves consistently with the specification of OutputStream#flush(). Oleg Kalnichevski Added: httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java?rev=647816&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java (added) +++ httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java Mon Apr 14 07:37:13 2008 @@ -0,0 +1,107 @@ +/* + * $HeadURL:$ + * $Revision:$ + * $Date:$ + * + * ==================================================================== + * 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.entity; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; + +public class SerializableEntity extends AbstractHttpEntity { + + private byte[] objSer; + + private Serializable objRef; + + public SerializableEntity(Serializable ser, boolean bufferize) throws IOException { + super(); + if (ser == null) { + throw new IllegalArgumentException("Source object may not be null"); + } + + if (bufferize) { + createBytes(ser); + } else { + this.objRef = ser; + } + } + + private void createBytes(Serializable ser) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(ser); + out.flush(); + this.objSer = baos.toByteArray(); + } + + public InputStream getContent() throws IOException, IllegalStateException { + if (this.objSer == null) { + createBytes(this.objRef); + } + return new ByteArrayInputStream(this.objSer); + } + + public long getContentLength() { + if (this.objSer == null) { + return -1; + } else { + return this.objSer.length; + } + } + + public boolean isRepeatable() { + return true; + } + + public boolean isStreaming() { + return this.objSer == null; + } + + public void writeTo(OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + + if (this.objSer == null) { + ObjectOutputStream out = new ObjectOutputStream(outstream); + out.writeObject(this.objRef); + out.flush(); + } else { + outstream.write(this.objSer); + outstream.flush(); + } + } + +} Propchange: httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestAllEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestAllEntity.java?rev=647816&r1=647815&r2=647816&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestAllEntity.java (original) +++ httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestAllEntity.java Mon Apr 14 07:37:13 2008 @@ -49,6 +49,7 @@ suite.addTest(TestHttpEntityWrapper.suite()); suite.addTest(TestBufferedHttpEntity.suite()); suite.addTest(TestEntityTemplate.suite()); + suite.addTest(TestSerializableEntity.suite()); return suite; } Added: httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java?rev=647816&view=auto ============================================================================== --- httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java (added) +++ httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java Mon Apr 14 07:37:13 2008 @@ -0,0 +1,153 @@ +/* + * $HeadURL:$ + * $Revision:$ + * $Date:$ + * + * ==================================================================== + * 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.entity; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestSerializableEntity extends TestCase { + + public static class SerializableObject implements Serializable { + + private static final long serialVersionUID = 1833335861188359573L; + + public int intValue = 4; + + public String stringValue = "Hello"; + + public SerializableObject() {} + } + + public TestSerializableEntity(String testName) { + super(testName); + } + + public static void main(String args[]) { + String[] testCaseName = { TestSerializableEntity.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public static Test suite() { + return new TestSuite(TestSerializableEntity.class); + } + + public void testBasicsBuff() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + + Serializable serializableObj = new SerializableObject(); + out.writeObject(serializableObj); + + SerializableEntity httpentity = new SerializableEntity(serializableObj, true); + + assertEquals(baos.toByteArray().length, httpentity.getContentLength()); + assertNotNull(httpentity.getContent()); + assertTrue(httpentity.isRepeatable()); + assertFalse(httpentity.isStreaming()); + } + + public void testBasicsDirect() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + + Serializable serializableObj = new SerializableObject(); + out.writeObject(serializableObj); + + SerializableEntity httpentity = new SerializableEntity(serializableObj, false); + + assertEquals(-1, httpentity.getContentLength()); + assertNotNull(httpentity.getContent()); + assertTrue(httpentity.isRepeatable()); + assertFalse(httpentity.isStreaming()); + } + + public void testIllegalConstructor() throws Exception { + try { + new SerializableEntity(null, false); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testWriteToBuff() throws Exception { + Serializable serializableObj = new SerializableObject(); + SerializableEntity httpentity = new SerializableEntity(serializableObj, true); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + httpentity.writeTo(out); + byte[] bytes = out.toByteArray(); + assertNotNull(bytes); + ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream( + bytes)); + SerializableObject serIn = (SerializableObject) oin.readObject(); + assertEquals(4, serIn.intValue); + assertEquals("Hello", serIn.stringValue); + + try { + httpentity.writeTo(null); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testWriteToDirect() throws Exception { + Serializable serializableObj = new SerializableObject(); + SerializableEntity httpentity = new SerializableEntity(serializableObj, false); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + httpentity.writeTo(out); + byte[] bytes = out.toByteArray(); + assertNotNull(bytes); + ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream( + bytes)); + SerializableObject serIn = (SerializableObject) oin.readObject(); + assertEquals(4, serIn.intValue); + assertEquals("Hello", serIn.stringValue); + + try { + httpentity.writeTo(null); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + +} Propchange: httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/entity/TestSerializableEntity.java ------------------------------------------------------------------------------ svn:mime-type = text/plain