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.CacheConstants;
027 import org.apache.camel.component.cache.DefaultCacheManagerFactory;
028 import org.apache.camel.converter.IOConverter;
029 import org.apache.camel.util.IOHelper;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 public class CacheBasedTokenReplacer extends CacheValidate implements Processor {
034 private static final transient Logger LOG = LoggerFactory.getLogger(CacheBasedTokenReplacer.class);
035 private String cacheName;
036 private String key;
037 private String replacementToken;
038 private CacheManager cacheManager;
039 private Ehcache cache;
040
041 public CacheBasedTokenReplacer(String cacheName, String key, String replacementToken) {
042 super();
043 if (cacheName.contains("cache://")) {
044 this.setCacheName(cacheName.replace("cache://", ""));
045 } else {
046 this.setCacheName(cacheName);
047 }
048 this.setKey(key);
049 this.setReplacementToken(replacementToken);
050 }
051
052 public void process(Exchange exchange) throws Exception {
053 // Cache the buffer to the specified Cache against the specified key
054 cacheManager = new DefaultCacheManagerFactory().getInstance();
055
056 if (isValid(cacheManager, cacheName, key)) {
057 cache = cacheManager.getCache(cacheName);
058
059 if (LOG.isDebugEnabled()) {
060 LOG.debug("Replacing Token {} in Message with value stored against key {} in CacheName {}",
061 new Object[]{replacementToken, key, cacheName});
062 }
063 exchange.getIn().setHeader(CacheConstants.CACHE_KEY, key);
064
065 Object body = exchange.getIn().getBody();
066 InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
067 byte[] buffer;
068 try {
069 buffer = IOConverter.toBytes(is);
070 } finally {
071 IOHelper.close(is, "is", LOG);
072 }
073
074 // Note: The value in the cache must be a String
075 String cacheValue = exchange.getContext().getTypeConverter()
076 .convertTo(String.class, cache.get(key).getObjectValue());
077 String replacedTokenString = new String(buffer).replaceAll(replacementToken, cacheValue);
078
079 LOG.trace("replacedTokenString = {}", replacedTokenString);
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 }