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 java.nio.ByteBuffer;
026
027import javax.xml.transform.TransformerException;
028import javax.xml.transform.sax.SAXSource;
029import javax.xml.transform.stream.StreamSource;
030
031import org.apache.camel.BytesSource;
032import org.apache.camel.Converter;
033import org.apache.camel.Exchange;
034import org.apache.camel.StreamCache;
035import org.apache.camel.StringSource;
036import org.apache.camel.util.IOHelper;
037
038/**
039 * A set of {@link Converter} methods for wrapping stream-based messages in a {@link StreamCache}
040 * implementation to ensure message re-readability (eg multicasting, retrying)
041 */
042@Converter
043public final class StreamCacheConverter {
044
045    /**
046     * Utility classes should not have a public constructor.
047     */
048    private StreamCacheConverter() {
049    }
050
051    @Converter
052    public static StreamCache convertToStreamCache(StreamSource source, Exchange exchange) throws IOException {
053        return new StreamSourceCache(source, exchange);
054    }
055
056    @Converter
057    public static StreamCache convertToStreamCache(StringSource source) {
058        //no need to do stream caching for a StringSource
059        return null;
060    }
061
062    @Converter
063    public static StreamCache convertToStreamCache(BytesSource source) {
064        //no need to do stream caching for a BytesSource
065        return null;
066    }
067
068    @Converter
069    public static StreamCache convertToStreamCache(SAXSource source, Exchange exchange) throws TransformerException {
070        String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, source);
071        return new SourceCache(data);
072    }
073
074    @Converter
075    public static StreamCache convertToStreamCache(ByteArrayInputStream stream, Exchange exchange) throws IOException {
076        return new ByteArrayInputStreamCache(stream);
077    }
078
079    @Converter
080    public static StreamCache convertToStreamCache(InputStream stream, Exchange exchange) throws IOException {
081        // transfer the input stream to a cached output stream, and then creates a new stream cache view
082        // of the data, which ensures the input stream is cached and re-readable.
083        CachedOutputStream cos = new CachedOutputStream(exchange);
084        IOHelper.copyAndCloseInput(stream, cos);
085        return cos.newStreamCache();
086    }
087
088    @Converter
089    public static StreamCache convertToStreamCache(CachedOutputStream cos, Exchange exchange) throws IOException {
090        return cos.newStreamCache();
091    }
092
093    @Converter
094    public static StreamCache convertToStreamCache(Reader reader, Exchange exchange) throws IOException {
095        String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, reader);
096        return new ReaderCache(data);
097    }
098
099    @Converter
100    public static Serializable convertToSerializable(StreamCache cache, Exchange exchange) throws IOException {
101        byte[] data = convertToByteArray(cache, exchange);
102        return new BytesSource(data);
103    }
104
105    @Converter
106    public static byte[] convertToByteArray(StreamCache cache, Exchange exchange) throws IOException {
107        // lets serialize it as a byte array
108        ByteArrayOutputStream os = new ByteArrayOutputStream();
109        cache.writeTo(os);
110        return os.toByteArray();
111    }
112
113    @Converter
114    public static ByteBuffer convertToByteBuffer(StreamCache cache, Exchange exchange) throws IOException {
115        byte[] array = convertToByteArray(cache, exchange);
116        return ByteBuffer.wrap(array);
117    }
118
119}