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.ByteArrayInputStream;
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.Reader;
023    import java.io.StringReader;
024    
025    import javax.xml.transform.Source;
026    import javax.xml.transform.TransformerException;
027    import javax.xml.transform.stream.StreamSource;
028    
029    import org.apache.camel.Converter;
030    import org.apache.camel.converter.IOConverter;
031    import org.apache.camel.converter.jaxp.StringSource;
032    import org.apache.camel.converter.jaxp.XmlConverter;
033    import org.apache.camel.processor.interceptor.Debugger;
034    import org.apache.commons.logging.Log;
035    import org.apache.commons.logging.LogFactory;
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
042    public class StreamCacheConverter {
043        private static final transient Log LOG = LogFactory.getLog(StreamCacheConverter.class);
044    
045        private XmlConverter converter = new XmlConverter();
046    
047        @Converter
048        public StreamCache convertToStreamCache(Source source) throws TransformerException {
049            return new StreamSourceCache(converter.toString(source));
050        }
051    
052        @Converter
053        public StreamCache convertToStreamCache(InputStream stream) throws IOException {
054            return new InputStreamCache(IOConverter.toBytes(stream));
055        }
056    
057        @Converter
058        public StreamCache convertToStreamCache(Reader reader) throws IOException {
059            return new ReaderCache(IOConverter.toString(reader));
060        }
061    
062        private class StreamSourceCache extends StringSource implements StreamCache {
063    
064            private static final long serialVersionUID = 4147248494104812945L;
065    
066            public StreamSourceCache(String text) {
067                super(text);
068            }
069    
070            public void reset() {
071                // do nothing here
072            }
073    
074        }
075    
076        private class InputStreamCache extends ByteArrayInputStream implements StreamCache {
077    
078            public InputStreamCache(byte[] data) {
079                super(data);
080            }
081    
082        }
083    
084        private class ReaderCache extends StringReader implements StreamCache {
085    
086            public ReaderCache(String s) {
087                super(s);
088            }
089    
090            public void reset() {
091                try {
092                    super.reset();
093                } catch (IOException e) {
094                    LOG.warn("Exception is thrown when resets the ReaderCache", e);
095                }
096            }
097    
098            public void close() {
099                // Do not release the string for caching
100            }
101    
102        }
103    
104    
105    }