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 */
017package org.apache.camel.converter.stream;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.Reader;
024import java.io.Serializable;
025import javax.xml.transform.TransformerException;
026import javax.xml.transform.sax.SAXSource;
027import javax.xml.transform.stream.StreamSource;
028
029import org.apache.camel.BytesSource;
030import org.apache.camel.Converter;
031import org.apache.camel.Exchange;
032import org.apache.camel.StreamCache;
033import org.apache.camel.StringSource;
034import 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
041public 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(CachedOutputStream cos, Exchange exchange) throws IOException {
088        return cos.newStreamCache();
089    }
090
091    @Converter
092    public static StreamCache convertToStreamCache(Reader reader, Exchange exchange) throws IOException {
093        String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, reader);
094        return new ReaderCache(data);
095    }
096
097    @Converter
098    public static Serializable convertToSerializable(StreamCache cache, Exchange exchange) throws IOException {
099        byte[] data = convertToByteArray(cache, exchange);
100        return new BytesSource(data);
101    }
102
103    @Converter
104    public static byte[] convertToByteArray(StreamCache cache, Exchange exchange) throws IOException {
105        // lets serialize it as a byte array
106        ByteArrayOutputStream os = new ByteArrayOutputStream();
107        cache.writeTo(os);
108        return os.toByteArray();
109    }
110
111}