Author: romkal Date: Thu Mar 19 15:09:54 2009 New Revision: 756039 URL: http://svn.apache.org/viewvc?rev=756039&view=rev Log: CAMEL-1320: Created gzip data format
Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/GzipDataFormat.java (with props) camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/GzipDataFormat.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/impl/GzipDataFormatTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java?rev=756039&r1=756038&r2=756039&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java Thu Mar 19 15:09:54 2009 @@ -18,13 +18,12 @@ import java.util.zip.Deflater; -import org.w3c.dom.Node; - import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.dataformat.ArtixDSContentType; import org.apache.camel.model.dataformat.ArtixDSDataFormat; import org.apache.camel.model.dataformat.CsvDataFormat; import org.apache.camel.model.dataformat.DataFormatDefinition; +import org.apache.camel.model.dataformat.GzipDataFormat; import org.apache.camel.model.dataformat.HL7DataFormat; import org.apache.camel.model.dataformat.JaxbDataFormat; import org.apache.camel.model.dataformat.JsonDataFormat; @@ -36,6 +35,7 @@ import org.apache.camel.model.dataformat.XMLSecurityDataFormat; import org.apache.camel.model.dataformat.XStreamDataFormat; import org.apache.camel.model.dataformat.ZipDataFormat; +import org.w3c.dom.Node; /** * An expression for constructing the different possible {...@link org.apache.camel.spi.DataFormat} @@ -242,6 +242,14 @@ ZipDataFormat zdf = new ZipDataFormat(compressionLevel); return dataFormat(zdf); } + + /** + * Uses the GZIP deflater data format + */ + public T gzip() { + GzipDataFormat gzdf = new GzipDataFormat(); + return dataFormat(gzdf); + } @SuppressWarnings("unchecked") private T dataFormat(DataFormatDefinition dataFormatType) { Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/GzipDataFormat.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/GzipDataFormat.java?rev=756039&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/GzipDataFormat.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/GzipDataFormat.java Thu Mar 19 15:09:54 2009 @@ -0,0 +1,59 @@ +/** + * 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.OutputStream; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import org.apache.camel.Exchange; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.util.ExchangeHelper; +import org.apache.camel.util.IOHelper; + +public class GzipDataFormat implements DataFormat { + + public void marshal(Exchange exchange, Object graph, OutputStream stream) + throws Exception { + InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, graph); + if (is == null) { + throw new IllegalArgumentException("Cannot get the inputstream for GzipDataFormat mashalling"); + } + + GZIPOutputStream zipOutput = new GZIPOutputStream(stream); + try { + IOHelper.copy(is, zipOutput); + } finally { + zipOutput.close(); + } + + } + + public Object unmarshal(Exchange exchange, InputStream stream) + throws Exception { + InputStream is = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class); + GZIPInputStream unzipInput = new GZIPInputStream(is); + + // Create an expandable byte array to hold the inflated data + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + IOHelper.copy(unzipInput, bos); + return bos.toByteArray(); + } + +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/GzipDataFormat.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/GzipDataFormat.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/GzipDataFormat.java?rev=756039&view=auto ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/GzipDataFormat.java (added) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/GzipDataFormat.java Thu Mar 19 15:09:54 2009 @@ -0,0 +1,35 @@ +/** + * 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 javax.xml.bind.annotation.XmlRootElement; + +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.RouteContext; + +/** + * Represents the GZip {...@link DataFormat} + * + * @version $Revision: 750806 $ + */ +...@xmlrootelement(name = "gzip") +public class GzipDataFormat extends DataFormatDefinition { + @Override + protected DataFormat createDataFormat(RouteContext routeContext) { + return new org.apache.camel.impl.GzipDataFormat(); + } +} Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/GzipDataFormat.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/GzipDataFormatTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/GzipDataFormatTest.java?rev=756039&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/GzipDataFormatTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/GzipDataFormatTest.java Thu Mar 19 15:09:54 2009 @@ -0,0 +1,75 @@ +/** + * 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.ByteArrayInputStream; +import java.util.zip.GZIPInputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.converter.IOConverter; + +/** + * Unit test of the gzip data format. + */ +public class GzipDataFormatTest extends ContextTestSupport { + private static final String TEXT = "Hamlet by William Shakespeare\n" + + "To be, or not to be: that is the question:\n" + + "Whether 'tis nobler in the mind to suffer\n" + + "The slings and arrows of outrageous fortune,\n" + + "Or to take arms against a sea of troubles,\n" + + "And by opposing end them? To die: to sleep;";; + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + private byte[] sendText() throws Exception { + return (byte[]) template.sendBody("direct:start", TEXT.getBytes("UTF-8")); + } + + public void testMarshalTextToGZip() throws Exception { + context.addRoutes(new RouteBuilder() { + public void configure() { + from("direct:start").marshal().gzip(); + } + }); + context.start(); + + byte[] output = sendText(); + + GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(output)); + String result = IOConverter.toString(stream); + assertEquals("Uncompressed something different than compressed", TEXT, result); + } + + public void testUnMarshalTextToGzip() throws Exception { + context.addRoutes(new RouteBuilder() { + public void configure() { + from("direct:start").marshal().gzip().unmarshal().gzip().to("mock:result"); + } + }); + context.start(); + + MockEndpoint result = (MockEndpoint)context.getEndpoint("mock:result"); + result.expectedBodiesReceived(TEXT.getBytes("UTF-8")); + sendText(); + result.assertIsSatisfied(); + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/GzipDataFormatTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain