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.impl; 018 019 import java.util.Map; 020 021 import org.apache.camel.CamelContext; 022 import org.apache.camel.Consumer; 023 import org.apache.camel.Endpoint; 024 import org.apache.camel.Exchange; 025 import org.apache.camel.ExchangePattern; 026 import org.apache.camel.PollingConsumer; 027 import org.apache.camel.Processor; 028 import org.apache.camel.Producer; 029 import org.apache.camel.util.ServiceHelper; 030 import org.apache.commons.logging.Log; 031 import org.apache.commons.logging.LogFactory; 032 033 /** 034 * This is an endpoint when sending to it, is intercepted and is routed in a detour 035 * 036 * @version $Revision: 778418 $ 037 */ 038 public class InterceptSendToEndpoint implements Endpoint { 039 040 private static final transient Log LOG = LogFactory.getLog(InterceptSendToEndpoint.class); 041 042 private final Endpoint delegate; 043 private Producer producer; 044 private Processor detour; 045 private boolean skip; 046 047 /** 048 * Intercepts sending to the given endpoint 049 * 050 * @param destination the original endpoint 051 * @param skip <tt>true</tt> to skip sending after the detour to the original endpoint 052 */ 053 public InterceptSendToEndpoint(final Endpoint destination, boolean skip) { 054 this.delegate = destination; 055 this.skip = skip; 056 } 057 058 public void setDetour(Processor detour) { 059 this.detour = detour; 060 } 061 062 public Endpoint getDelegate() { 063 return delegate; 064 } 065 066 public String getEndpointUri() { 067 return delegate.getEndpointUri(); 068 } 069 070 public Exchange createExchange() { 071 return delegate.createExchange(); 072 } 073 074 public Exchange createExchange(ExchangePattern pattern) { 075 return delegate.createExchange(pattern); 076 } 077 078 public Exchange createExchange(Exchange exchange) { 079 return delegate.createExchange(exchange); 080 } 081 082 public CamelContext getCamelContext() { 083 return delegate.getCamelContext(); 084 } 085 086 public Producer createProducer() throws Exception { 087 producer = delegate.createProducer(); 088 return new Producer() { 089 090 public Endpoint getEndpoint() { 091 return producer.getEndpoint(); 092 } 093 094 public Exchange createExchange() { 095 return producer.createExchange(); 096 } 097 098 public Exchange createExchange(ExchangePattern pattern) { 099 return producer.createExchange(pattern); 100 } 101 102 public Exchange createExchange(Exchange exchange) { 103 return producer.createExchange(exchange); 104 } 105 106 public void process(Exchange exchange) throws Exception { 107 // process the detour so we do the detour routing 108 if (LOG.isDebugEnabled()) { 109 LOG.debug("Sending to endpoint: " + getEndpointUri() + " is intercepted and detoured to: " + detour + " for exchange: " + exchange); 110 } 111 // add header with the real endpoint uri 112 exchange.getIn().setHeader(Exchange.INTERCEPTED_ENDPOINT, delegate.getEndpointUri()); 113 114 detour.process(exchange); 115 // copy OUT to IN 116 if (exchange.hasOut()) { 117 // replace OUT with IN as detour changed something 118 exchange.setIn(exchange.getOut()); 119 exchange.setOut(null); 120 } 121 122 if (!skip) { 123 // route to original destination 124 producer.process(exchange); 125 } else { 126 if (LOG.isDebugEnabled()) { 127 LOG.debug("Stop() means skip sending exchange to original intended destination: " + getEndpointUri() + " for exchange: " + exchange); 128 } 129 } 130 } 131 132 public boolean isSingleton() { 133 return producer.isSingleton(); 134 } 135 136 public void start() throws Exception { 137 ServiceHelper.startService(detour); 138 } 139 140 public void stop() throws Exception { 141 ServiceHelper.stopService(detour); 142 } 143 }; 144 } 145 146 public Consumer createConsumer(Processor processor) throws Exception { 147 return delegate.createConsumer(processor); 148 } 149 150 public PollingConsumer createPollingConsumer() throws Exception { 151 return delegate.createPollingConsumer(); 152 } 153 154 public void configureProperties(Map options) { 155 delegate.configureProperties(options); 156 } 157 158 public void setCamelContext(CamelContext context) { 159 delegate.setCamelContext(context); 160 } 161 162 public boolean isLenientProperties() { 163 return delegate.isLenientProperties(); 164 } 165 166 public boolean isSingleton() { 167 return delegate.isSingleton(); 168 } 169 170 @Override 171 public String toString() { 172 return delegate.toString(); 173 } 174 }