You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2008/12/01 10:14:29 UTC

svn commit: r722008 - in /activemq/camel/branches/camel-1.x/camel-core/src: main/java/org/apache/camel/impl/ZipDataFormat.java main/java/org/apache/camel/model/dataformat/ZipDataFormat.java test/java/org/apache/camel/impl/ZipDataFormatTest.java

Author: ningjiang
Date: Mon Dec  1 01:14:28 2008
New Revision: 722008

URL: http://svn.apache.org/viewvc?rev=722008&view=rev
Log:
Added the files which I missed in the last commit

Added:
    activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java   (with props)
    activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java   (with props)
    activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java   (with props)

Added: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java?rev=722008&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java (added)
+++ activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java Mon Dec  1 01:14:28 2008
@@ -0,0 +1,90 @@
+/**
+ * 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.camel.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+
+import javax.xml.bind.annotation.XmlAttribute;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.spi.DataFormat;
+
+public class ZipDataFormat implements DataFormat {
+
+    private static final int INITIALBYTEARRAYSIZE = 1048576;
+    private int compressionLevel;
+
+    public ZipDataFormat() {
+        this.compressionLevel = Deflater.BEST_SPEED;
+    }
+
+    public ZipDataFormat(int compressionLevel) {
+        this.compressionLevel = compressionLevel;
+    }
+
+    public void marshal(Exchange exchange, Object graph, OutputStream stream)
+        throws Exception {
+        
+        // Retrieve the message body as byte array 
+        byte[] input = (byte[]) exchange.getIn().getBody(byte[].class);
+        
+        // Create a Message Deflater
+        Deflater deflater = new Deflater(compressionLevel);
+        deflater.setInput(input);
+        deflater.finish();        
+        
+        // Compress the data
+        byte[] output = new byte[INITIALBYTEARRAYSIZE];
+        while (!deflater.finished()) {
+            int count = deflater.deflate(output);
+            stream.write(output, 0, count);
+        }
+
+    }
+
+    public Object unmarshal(Exchange exchange, InputStream stream)
+        throws Exception {
+
+        // Retrieve the message body as byte array 
+        byte[] input = (byte[]) exchange.getIn().getBody(byte[].class);
+        
+        // Create a Message Inflater        
+        Inflater inflater = new Inflater();
+        inflater.setInput(input);
+
+        // Create an expandable byte array to hold the inflated data
+        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
+        
+        // Inflate the compressed data
+        byte[] buf = new byte[INITIALBYTEARRAYSIZE];
+        while (!inflater.finished()) {
+            int count = inflater.inflate(buf);
+            bos.write(buf, 0, count);
+        }    
+
+        // Return the inflated data
+        return bos.toByteArray();
+    }
+
+}

Propchange: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java?rev=722008&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java (added)
+++ activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java Mon Dec  1 01:14:28 2008
@@ -0,0 +1,58 @@
+/**
+ * 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.camel.model.dataformat;
+
+
+import java.util.zip.Deflater;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.RouteContext;
+
+@XmlRootElement(name = "zip")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class ZipDataFormat extends DataFormatType {
+
+    @XmlAttribute(required = false)
+    private int compressionLevel = Deflater.BEST_SPEED;
+    
+    public ZipDataFormat() {
+        
+    }
+
+    public ZipDataFormat(int compressionLevel) {
+        this.compressionLevel = compressionLevel;
+    }
+
+    @Override
+    protected DataFormat createDataFormat(RouteContext routeContext) {
+        return new org.apache.camel.impl.ZipDataFormat(compressionLevel);
+    }
+
+    public int getCompressionLevel() {
+        return compressionLevel;
+    }
+
+    public void setCompressionLevel(int compressionLevel) {
+        this.compressionLevel = compressionLevel;
+    }
+}

Propchange: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/model/dataformat/ZipDataFormat.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java?rev=722008&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java (added)
+++ activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java Mon Dec  1 01:14:28 2008
@@ -0,0 +1,156 @@
+/**
+ * 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.camel.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.TestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+
+
+/**
+ * Unit test of the zip data format.
+ */
+public class ZipDataFormatTest extends TestSupport {
+    private static final String TEXT = "The Cow in Apple Time \n" 
+        + "by: Robert Frost \n\n" 
+        + "Something inspires the only cow of late\n" 
+        + "To make no more of a wall than an open gate,\n" 
+        + "And think no more of wall-builders than fools.\n" 
+        + "Her face is flecked with pomace and she drools\n" 
+        + "A cider syrup. Having tasted fruit,\n" 
+        + "She scorns a pasture withering to the root.\n" 
+        + "She runs from tree to tree where lie and sweeten.\n" 
+        + "The windfalls spiked with stubble and worm-eaten.\n" 
+        + "She leaves them bitten when she has to fly.\n" 
+        + "She bellows on a knoll against the sky.\n" 
+        + "Her udder shrivels and the milk goes dry.";
+
+    private CamelContext context;
+    private ProducerTemplate template;
+
+    
+    
+    protected void setUp() throws Exception {
+        context = new DefaultCamelContext();
+        template = context.createProducerTemplate();
+        template.start();
+    }
+
+    protected void tearDown() throws Exception {
+        template.stop();
+        context.stop();
+    }
+    
+    private void sendText() throws Exception {
+        
+        template.send("direct:start", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                // Set the property of the charset encoding
+                exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
+                Message in = exchange.getIn();
+                in.setBody(TEXT);
+            }
+            
+        });       
+    }
+
+    public void testMarshalTextToZipBestCompression() throws Exception {
+       
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").marshal().zip(Deflater.BEST_COMPRESSION).process(new ZippedMessageProcessor());
+            }
+        });
+        context.start();
+
+        sendText();
+    }
+    
+    public void testMarshalTextToZipBestSpeed() throws Exception {
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").marshal().zip(Deflater.BEST_SPEED).process(new ZippedMessageProcessor());
+            }
+        });
+        context.start();
+
+        sendText();
+
+    }
+
+    public void testMarshalTextToZipDefaultCompression() throws Exception {
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").marshal().zip(Deflater.DEFAULT_COMPRESSION).process(new ZippedMessageProcessor());
+            }
+        });
+        context.start();
+
+        sendText();
+    }
+    
+    public void testUnMarshalTextToZip() throws Exception {
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("direct:start").marshal().zip().unmarshal().zip().to("mock:result");
+            }
+        });
+        context.start();
+        MockEndpoint result = (MockEndpoint)context.getEndpoint("mock:result");
+        result.expectedBodiesReceived(TEXT);
+        sendText();
+        result.assertIsSatisfied();
+    }    
+    
+    private class ZippedMessageProcessor implements Processor {
+
+        public void process(Exchange exchange) throws Exception {
+            byte[] body = (byte[]) exchange.getIn().getBody(byte[].class);
+            
+            Inflater inflater = new Inflater();
+            inflater.setInput(body);
+            
+            // Create an expandable byte array to hold the inflated data
+            ByteArrayOutputStream bos = new ByteArrayOutputStream(body.length);
+            
+            // Inflate the compressed data
+            byte[] buf = new byte[1024];
+            while (!inflater.finished()) {
+                int count = inflater.inflate(buf);
+                bos.write(buf, 0, count);
+            }
+
+            String result = new String(bos.toByteArray(), "UTF-8");
+            
+            // does the testing
+            assertEquals(TEXT, result);
+        }
+    }    
+  
+}

Propchange: activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/camel-core/src/test/java/org/apache/camel/impl/ZipDataFormatTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date