Return-Path: Delivered-To: apmail-activemq-camel-commits-archive@locus.apache.org Received: (qmail 26934 invoked from network); 1 Dec 2008 09:14:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 1 Dec 2008 09:14:50 -0000 Received: (qmail 28177 invoked by uid 500); 1 Dec 2008 09:15:02 -0000 Delivered-To: apmail-activemq-camel-commits-archive@activemq.apache.org Received: (qmail 28132 invoked by uid 500); 1 Dec 2008 09:15:02 -0000 Mailing-List: contact camel-commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: camel-dev@activemq.apache.org Delivered-To: mailing list camel-commits@activemq.apache.org Received: (qmail 28123 invoked by uid 99); 1 Dec 2008 09:15:02 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Dec 2008 01:15:02 -0800 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.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Dec 2008 09:13:42 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5294E23888EB; Mon, 1 Dec 2008 01:14:29 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Mon, 01 Dec 2008 09:14:29 -0000 To: camel-commits@activemq.apache.org From: ningjiang@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081201091429.5294E23888EB@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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