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.ByteArrayOutputStream; 020 import java.io.IOException; 021 import java.io.InputStream; 022 import java.io.Reader; 023 import java.io.Serializable; 024 import javax.xml.transform.TransformerException; 025 import javax.xml.transform.sax.SAXSource; 026 import javax.xml.transform.stream.StreamSource; 027 028 import org.apache.camel.BytesSource; 029 import org.apache.camel.Converter; 030 import org.apache.camel.Exchange; 031 import org.apache.camel.StreamCache; 032 import org.apache.camel.StringSource; 033 import org.apache.camel.util.IOHelper; 034 035 /** 036 * A set of {@link Converter} methods for wrapping stream-based messages in a {@link StreamCache} 037 * implementation to ensure message re-readability (eg multicasting, retrying) 038 */ 039 @Converter 040 public final class StreamCacheConverter { 041 042 /** 043 * Utility classes should not have a public constructor. 044 */ 045 private StreamCacheConverter() { 046 } 047 048 @Converter 049 public static StreamCache convertToStreamCache(StreamSource source, Exchange exchange) throws IOException { 050 return new StreamSourceCache(source, exchange); 051 } 052 053 @Converter 054 public static StreamCache convertToStreamCache(StringSource source) { 055 //no need to do stream caching for a StringSource 056 return null; 057 } 058 059 @Converter 060 public static StreamCache convertToStreamCache(BytesSource source) { 061 //no need to do stream caching for a BytesSource 062 return null; 063 } 064 065 @Converter 066 public static StreamCache convertToStreamCache(SAXSource source, Exchange exchange) throws TransformerException { 067 String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, source); 068 return new SourceCache(data); 069 } 070 071 @Converter 072 public static StreamCache convertToStreamCache(InputStream stream, Exchange exchange) throws IOException { 073 CachedOutputStream cos = new CachedOutputStream(exchange); 074 IOHelper.copyAndCloseInput(stream, cos); 075 return cos.getStreamCache(); 076 } 077 078 @Converter 079 public static StreamCache convertToStreamCache(Reader reader, Exchange exchange) throws IOException { 080 String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, reader); 081 return new ReaderCache(data); 082 } 083 084 @Converter 085 public static Serializable convertToSerializable(StreamCache cache, Exchange exchange) throws IOException { 086 byte[] data = convertToByteArray(cache, exchange); 087 return new BytesSource(data); 088 } 089 090 @Converter 091 public static byte[] convertToByteArray(StreamCache cache, Exchange exchange) throws IOException { 092 // lets serialize it as a byte array 093 ByteArrayOutputStream os = new ByteArrayOutputStream(); 094 cache.writeTo(os); 095 return os.toByteArray(); 096 } 097 098 }