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