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.InputStream;
020    
021    import net.sf.ehcache.CacheManager;
022    import net.sf.ehcache.Ehcache;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Processor;
025    import org.apache.camel.component.cache.CacheManagerFactory;
026    import org.apache.camel.converter.IOConverter;
027    import org.apache.camel.util.IOHelper;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    public class CacheBasedTokenReplacer extends CacheValidate implements Processor {
032        private static final transient Log LOG = LogFactory.getLog(CacheBasedTokenReplacer.class);
033        private String cacheName;
034        private String key;
035        private String replacementToken;
036        private CacheManager cacheManager;
037        private Ehcache cache;
038    
039        public CacheBasedTokenReplacer(String cacheName, String key, String replacementToken) {
040            super();
041            if (cacheName.contains("cache://")) {
042                this.setCacheName(cacheName.replace("cache://", ""));
043            } else {
044                this.setCacheName(cacheName);
045            }
046            this.setKey(key);
047            this.setReplacementToken(replacementToken);
048        }
049    
050        public void process(Exchange exchange) throws Exception {
051            // Cache the buffer to the specified Cache against the specified key
052            cacheManager = new CacheManagerFactory().instantiateCacheManager();
053    
054            if (isValid(cacheManager, cacheName, key)) {
055                cache = cacheManager.getCache(cacheName);
056    
057                if (LOG.isDebugEnabled()) {
058                    LOG.debug("Replacing Token " + replacementToken + "in Message with value stored against key "
059                             + key + " in CacheName " + cacheName);
060                }
061                exchange.getIn().setHeader("CACHE_KEY", key);
062    
063                Object body = exchange.getIn().getBody();
064                InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
065                byte[] buffer;
066                try {
067                    buffer = IOConverter.toBytes(is);
068                } finally {
069                    IOHelper.close(is, "is", LOG);
070                }
071    
072                // Note: The value in the cache must be a String
073                String cacheValue = exchange.getContext().getTypeConverter()
074                        .convertTo(String.class, cache.get(key).getObjectValue());
075                String replacedTokenString = new String(buffer).replaceAll(replacementToken, cacheValue);
076    
077                if (LOG.isTraceEnabled()) {
078                    LOG.trace("replacedTokenString = " + replacedTokenString);
079                }
080                exchange.getIn().setBody(replacedTokenString.getBytes());
081            }
082        }
083    
084        public String getCacheName() {
085            return cacheName;
086        }
087    
088        public void setCacheName(String cacheName) {
089            this.cacheName = cacheName;
090        }
091    
092        public String getKey() {
093            return key;
094        }
095    
096        public void setKey(String key) {
097            this.key = key;
098        }
099    
100        public String getReplacementToken() {
101            return replacementToken;
102        }
103    
104        public void setReplacementToken(String replacementToken) {
105            this.replacementToken = replacementToken;
106        }
107    
108    }