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.Converter;
029 import org.apache.camel.Exchange;
030 import org.apache.camel.StreamCache;
031 import org.apache.camel.converter.jaxp.BytesSource;
032 import org.apache.camel.converter.jaxp.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 class StreamCacheConverter {
041
042 @Converter
043 public StreamCache convertToStreamCache(StreamSource source, Exchange exchange) throws IOException {
044 return new StreamSourceCache(source, exchange);
045 }
046
047 @Converter
048 public StreamCache convertToStreamCache(StringSource source) {
049 //no need to do stream caching for a StringSource
050 return null;
051 }
052
053 @Converter
054 public StreamCache convertToStreamCache(BytesSource source) {
055 //no need to do stream caching for a BytesSource
056 return null;
057 }
058
059 @Converter
060 public StreamCache convertToStreamCache(SAXSource source, Exchange exchange) throws TransformerException {
061 String data = exchange.getContext().getTypeConverter().convertTo(String.class, source);
062 return new SourceCache(data);
063 }
064
065 @Converter
066 public StreamCache convertToStreamCache(InputStream stream, Exchange exchange) throws IOException {
067 CachedOutputStream cos = new CachedOutputStream(exchange);
068 IOHelper.copyAndCloseInput(stream, cos);
069 return cos.getStreamCache();
070 }
071
072 @Converter
073 public StreamCache convertToStreamCache(Reader reader, Exchange exchange) throws IOException {
074 String data = exchange.getContext().getTypeConverter().convertTo(String.class, reader);
075 return new ReaderCache(data);
076 }
077
078 @Converter
079 public Serializable convertToSerializable(StreamCache cache, Exchange exchange) throws IOException {
080 byte[] data = convertToByteArray(cache, exchange);
081 return new BytesSource(data);
082 }
083
084 @Converter
085 public byte[] convertToByteArray(StreamCache cache, Exchange exchange) throws IOException {
086 // lets serialize it as a byte array
087 ByteArrayOutputStream os = new ByteArrayOutputStream();
088 cache.writeTo(os);
089 return os.toByteArray();
090 }
091
092 }