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