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.processor.cache;
018
019 import java.io.File;
020 import java.io.InputStream;
021 import java.io.StringReader;
022
023 import javax.xml.transform.Source;
024 import javax.xml.transform.Transformer;
025 import javax.xml.transform.TransformerFactory;
026 import javax.xml.transform.dom.DOMResult;
027 import javax.xml.transform.dom.DOMSource;
028
029 import org.w3c.dom.Document;
030
031 import net.sf.ehcache.CacheManager;
032 import net.sf.ehcache.Ehcache;
033 import org.apache.camel.Exchange;
034 import org.apache.camel.Processor;
035 import org.apache.camel.component.cache.factory.CacheManagerFactory;
036 import org.apache.camel.converter.IOConverter;
037 import org.apache.camel.converter.jaxp.XmlConverter;
038 import org.apache.camel.util.ObjectHelper;
039 import org.apache.commons.logging.Log;
040 import org.apache.commons.logging.LogFactory;
041
042 public class CacheBasedXPathReplacer extends CacheValidate implements Processor {
043 private static final transient Log LOG = LogFactory.getLog(CacheBasedXPathReplacer.class);
044 private String cacheName;
045 private String key;
046 private String xpath;
047 private CacheManager cacheManager;
048 private Ehcache cache;
049 private Document document;
050 private DOMSource source;
051 private DOMResult result;
052
053 public CacheBasedXPathReplacer(String cacheName, String key, String xpath) {
054 super();
055 if (cacheName.contains("cache://")) {
056 this.setCacheName(cacheName.replace("cache://", ""));
057 } else {
058 this.setCacheName(cacheName);
059 }
060 this.key = key;
061 this.xpath = xpath;
062 }
063
064 public void process(Exchange exchange) throws Exception {
065 // Cache the buffer to the specified Cache against the specified key
066 cacheManager = new CacheManagerFactory().instantiateCacheManager();
067
068 if (isValid(cacheManager, cacheName, key)) {
069 cache = cacheManager.getCache(cacheName);
070
071 if (LOG.isDebugEnabled()) {
072 LOG.debug("Replacing XPath value " + xpath + "in Message with value stored against key " + key
073 + " in CacheName " + cacheName);
074 }
075 exchange.getIn().setHeader("CACHE_KEY", key);
076 Object body = exchange.getIn().getBody();
077 InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
078 try {
079 document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is);
080 } finally {
081 ObjectHelper.close(is, "is", LOG);
082 }
083
084 InputStream cis = exchange.getContext().getTypeConverter()
085 .convertTo(InputStream.class, cache.get(key).getObjectValue());
086
087 try {
088 Document cacheValueDocument = exchange.getContext().getTypeConverter()
089 .convertTo(Document.class, exchange, cis);
090
091 // Create/setup the Transformer
092 XmlConverter xmlConverter = new XmlConverter();
093 String xslString = IOConverter.toString(new File("./src/main/resources/xpathreplacer.xsl"), exchange);
094 xslString = xslString.replace("##match_token##", xpath);
095 Source xslSource = xmlConverter.toStreamSource(new StringReader(xslString));
096 TransformerFactory transformerFactory = xmlConverter.createTransformerFactory();
097 Transformer transformer = transformerFactory.newTransformer(xslSource);
098 source = xmlConverter.toSource(document);
099 result = new DOMResult();
100
101 transformer.setParameter("cacheValue", cacheValueDocument);
102 transformer.transform(source, result);
103 } finally {
104 ObjectHelper.close(cis, "cis", LOG);
105 }
106 }
107
108 // DOMSource can be converted to byte[] by camel type converter mechanism
109 DOMSource dom = new DOMSource(result.getNode());
110 exchange.getIn().setBody(dom, byte[].class);
111 }
112
113 public String getCacheName() {
114 return cacheName;
115 }
116
117 public void setCacheName(String cacheName) {
118 this.cacheName = cacheName;
119 }
120
121 public String getKey() {
122 return key;
123 }
124
125 public void setKey(String key) {
126 this.key = key;
127 }
128
129 public String getXpath() {
130 return xpath;
131 }
132
133 public void setXpath(String xpath) {
134 this.xpath = xpath;
135 }
136
137 }