001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.converter.stream; 018 019 import java.io.ByteArrayInputStream; 020 import java.io.ByteArrayOutputStream; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.io.Reader; 024 import java.io.Serializable; 025 import javax.xml.transform.TransformerException; 026 import javax.xml.transform.sax.SAXSource; 027 import javax.xml.transform.stream.StreamSource; 028 029 import org.apache.camel.BytesSource; 030 import org.apache.camel.Converter; 031 import org.apache.camel.Exchange; 032 import org.apache.camel.StreamCache; 033 import org.apache.camel.StringSource; 034 import org.apache.camel.util.IOHelper; 035 036 /** 037 * A set of {@link Converter} methods for wrapping stream-based messages in a {@link StreamCache} 038 * implementation to ensure message re-readability (eg multicasting, retrying) 039 */ 040 @Converter 041 public final class StreamCacheConverter { 042 043 /** 044 * Utility classes should not have a public constructor. 045 */ 046 private StreamCacheConverter() { 047 } 048 049 @Converter 050 public static StreamCache convertToStreamCache(StreamSource source, Exchange exchange) throws IOException { 051 return new StreamSourceCache(source, exchange); 052 } 053 054 @Converter 055 public static StreamCache convertToStreamCache(StringSource source) { 056 //no need to do stream caching for a StringSource 057 return null; 058 } 059 060 @Converter 061 public static StreamCache convertToStreamCache(BytesSource source) { 062 //no need to do stream caching for a BytesSource 063 return null; 064 } 065 066 @Converter 067 public static StreamCache convertToStreamCache(SAXSource source, Exchange exchange) throws TransformerException { 068 String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, source); 069 return new SourceCache(data); 070 } 071 072 @Converter 073 public static StreamCache convertToStreamCache(ByteArrayInputStream stream, Exchange exchange) throws IOException { 074 return new ByteArrayInputStreamCache(stream); 075 } 076 077 @Converter 078 public static StreamCache convertToStreamCache(InputStream stream, Exchange exchange) throws IOException { 079 // transfer the input stream to a cached output stream, and then creates a new stream cache view 080 // of the data, which ensures the input stream is cached and re-readable. 081 CachedOutputStream cos = new CachedOutputStream(exchange); 082 IOHelper.copyAndCloseInput(stream, cos); 083 return cos.newStreamCache(); 084 } 085 086 @Converter 087 public static StreamCache convertToStreamCache(Reader reader, Exchange exchange) throws IOException { 088 String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, reader); 089 return new ReaderCache(data); 090 } 091 092 @Converter 093 public static Serializable convertToSerializable(StreamCache cache, Exchange exchange) throws IOException { 094 byte[] data = convertToByteArray(cache, exchange); 095 return new BytesSource(data); 096 } 097 098 @Converter 099 public static byte[] convertToByteArray(StreamCache cache, Exchange exchange) throws IOException { 100 // lets serialize it as a byte array 101 ByteArrayOutputStream os = new ByteArrayOutputStream(); 102 cache.writeTo(os); 103 return os.toByteArray(); 104 } 105 106 }