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