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.IOException; 020import java.io.InputStream; 021import java.io.OutputStream; 022import javax.xml.transform.stream.StreamSource; 023 024import org.apache.camel.Exchange; 025import org.apache.camel.StreamCache; 026import org.apache.camel.util.IOHelper; 027 028/** 029 * A {@link org.apache.camel.StreamCache} for {@link javax.xml.transform.stream.StreamSource}s 030 */ 031public final class StreamSourceCache extends StreamSource implements StreamCache { 032 033 private final StreamCache streamCache; 034 private final ReaderCache readCache; 035 036 public StreamSourceCache(StreamSource source, Exchange exchange) throws IOException { 037 if (source.getInputStream() != null) { 038 // set up CachedOutputStream with the properties 039 CachedOutputStream cos = new CachedOutputStream(exchange); 040 IOHelper.copyAndCloseInput(source.getInputStream(), cos); 041 streamCache = cos.newStreamCache(); 042 readCache = null; 043 setSystemId(source.getSystemId()); 044 setInputStream((InputStream) streamCache); 045 } else if (source.getReader() != null) { 046 String data = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, source.getReader()); 047 readCache = new ReaderCache(data); 048 streamCache = null; 049 setReader(readCache); 050 } else { 051 streamCache = null; 052 readCache = null; 053 } 054 } 055 056 public StreamSourceCache(StreamCache streamCache) { 057 this.streamCache = streamCache; 058 if (streamCache instanceof InputStream) { 059 setInputStream((InputStream) streamCache); 060 this.readCache = null; 061 } else if (streamCache instanceof ReaderCache) { 062 this.readCache = (ReaderCache) streamCache; 063 setReader((java.io.Reader) streamCache); 064 } else { 065 this.readCache = null; 066 } 067 } 068 069 public void reset() { 070 if (streamCache != null) { 071 streamCache.reset(); 072 } 073 if (readCache != null) { 074 readCache.reset(); 075 } 076 } 077 078 public void writeTo(OutputStream os) throws IOException { 079 if (streamCache != null) { 080 streamCache.writeTo(os); 081 } else if (readCache != null) { 082 readCache.writeTo(os); 083 } 084 } 085 086 public StreamCache copy(Exchange exchange) throws IOException { 087 if (streamCache != null) { 088 return new StreamSourceCache(streamCache.copy(exchange)); 089 } 090 if (readCache != null) { 091 return new StreamSourceCache(readCache.copy(exchange)); 092 } 093 return null; 094 } 095 096 public boolean inMemory() { 097 if (streamCache != null) { 098 return streamCache.inMemory(); 099 } else if (readCache != null) { 100 return readCache.inMemory(); 101 } else { 102 // should not happen 103 return true; 104 } 105 } 106 107 108 public long length() { 109 if (streamCache != null) { 110 return streamCache.length(); 111 } else if (readCache != null) { 112 return readCache.length(); 113 } else { 114 // should not happen 115 return 0; 116 } 117 } 118 119}