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 */ 017package org.apache.camel.model; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023 024import org.apache.camel.Exchange; 025import org.apache.camel.Processor; 026import org.apache.camel.spi.Metadata; 027import org.apache.camel.spi.RouteContext; 028import org.apache.camel.support.ExpressionAdapter; 029 030/** 031 * Intercepts incoming messages 032 * 033 * @version 034 */ 035@Metadata(label = "configuration") 036@XmlRootElement(name = "interceptFrom") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class InterceptFromDefinition extends InterceptDefinition { 039 @XmlAttribute 040 protected String uri; 041 042 public InterceptFromDefinition() { 043 } 044 045 public InterceptFromDefinition(String uri) { 046 this.uri = uri; 047 } 048 049 @Override 050 public String toString() { 051 return "InterceptFrom[" + getOutputs() + "]"; 052 } 053 054 @Override 055 public String getLabel() { 056 return "interceptFrom"; 057 } 058 059 @Override 060 public boolean isAbstract() { 061 return true; 062 } 063 064 @Override 065 public boolean isTopLevelOnly() { 066 return true; 067 } 068 069 @Override 070 @SuppressWarnings({"unchecked", "rawtypes"}) 071 public Processor createProcessor(RouteContext routeContext) throws Exception { 072 // insert a set header definition so we can set the intercepted endpoint uri as a header 073 // this allows us to use the same header for both the interceptFrom and interceptSendToEndpoint 074 SetHeaderDefinition headerDefinition = new SetHeaderDefinition(Exchange.INTERCEPTED_ENDPOINT, new ExpressionAdapter() { 075 public Object evaluate(Exchange exchange, Class type) { 076 if (exchange.getFromEndpoint() != null) { 077 return exchange.getFromEndpoint().getEndpointUri(); 078 } else { 079 return null; 080 } 081 } 082 083 public String toString() { 084 return ""; 085 } 086 }); 087 getOutputs().add(0, headerDefinition); 088 089 return this.createChildProcessor(routeContext, true); 090 } 091 092 public String getUri() { 093 return uri; 094 } 095 096 /** 097 * Intercept incoming messages from the uri or uri pattern. 098 * If this option is not configured, then all incoming messages is intercepted. 099 */ 100 public void setUri(String uri) { 101 this.uri = uri; 102 } 103}