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 2008/04/14 16:37:18 UTC

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/

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 <selva.andrea at gmail.com>
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 <selva.andrea at gmail.com>
+
 * [HTTPCORE-157] ChunkedOutputStream#flush() now behaves consistently with the 
   specification of OutputStream#flush().
   Oleg Kalnichevski <olegk at apache.org> 

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
+ * <http://www.apache.org/>.
+ *
+ */
+
+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
+ * <http://www.apache.org/>.
+ *
+ */
+
+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