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.TransformerException; 026 import javax.xml.transform.sax.SAXSource; 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.BytesSource; 032 import org.apache.camel.converter.jaxp.StringSource; 033 import org.apache.camel.converter.jaxp.XmlConverter; 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(StreamSource source) throws TransformerException { 049 return new SourceCache(converter.toString(source)); 050 } 051 052 @Converter 053 public StreamCache convertToStreamCache(StringSource source) throws TransformerException { 054 //no need to do stream caching for a StringSource 055 return null; 056 } 057 058 @Converter 059 public StreamCache convertToStreamCache(BytesSource source) throws TransformerException { 060 //no need to do stream caching for a BytesSource 061 return null; 062 } 063 064 @Converter 065 public StreamCache convertToStreamCache(SAXSource source) throws TransformerException { 066 return new SourceCache(converter.toString(source)); 067 } 068 069 @Converter 070 public StreamCache convertToStreamCache(InputStream stream) throws IOException { 071 return new InputStreamCache(IOConverter.toBytes(stream)); 072 } 073 074 @Converter 075 public StreamCache convertToStreamCache(Reader reader) throws IOException { 076 return new ReaderCache(IOConverter.toString(reader)); 077 } 078 079 /* 080 * {@link StreamCache} implementation for {@link Source}s 081 */ 082 private class SourceCache extends StringSource implements StreamCache { 083 084 private static final long serialVersionUID = 4147248494104812945L; 085 086 public SourceCache(String text) { 087 super(text); 088 } 089 090 public void reset() { 091 // do nothing here 092 } 093 094 } 095 096 private class InputStreamCache extends ByteArrayInputStream implements StreamCache { 097 098 public InputStreamCache(byte[] data) { 099 super(data); 100 } 101 102 } 103 104 private class ReaderCache extends StringReader implements StreamCache { 105 106 public ReaderCache(String s) { 107 super(s); 108 } 109 110 public void reset() { 111 try { 112 super.reset(); 113 } catch (IOException e) { 114 LOG.warn("Exception is thrown when resets the ReaderCache", e); 115 } 116 } 117 118 public void close() { 119 // Do not release the string for caching 120 } 121 122 } 123 124 125 }