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.model; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 import javax.xml.bind.annotation.XmlAccessType; 022 import javax.xml.bind.annotation.XmlAccessorType; 023 import javax.xml.bind.annotation.XmlRootElement; 024 import javax.xml.bind.annotation.XmlTransient; 025 026 import org.apache.camel.Predicate; 027 import org.apache.camel.Processor; 028 import org.apache.camel.processor.Pipeline; 029 import org.apache.camel.spi.InterceptStrategy; 030 import org.apache.camel.spi.RouteContext; 031 032 /** 033 * Represents an XML <intercept/> element 034 * 035 * @version $Revision: 771227 $ 036 */ 037 @XmlRootElement(name = "intercept") 038 @XmlAccessorType(XmlAccessType.FIELD) 039 public class InterceptDefinition extends OutputDefinition<ProcessorDefinition> { 040 041 // TODO: support stop later (its a bit hard as it needs to break entire processing of route) 042 043 @XmlTransient 044 protected Processor output; 045 046 public InterceptDefinition() { 047 } 048 049 @Override 050 public String toString() { 051 return "Intercept[" + getOutputs() + "]"; 052 } 053 054 @Override 055 public String getShortName() { 056 return "intercept"; 057 } 058 059 @Override 060 public String getLabel() { 061 return "intercept"; 062 } 063 064 @Override 065 public Processor createProcessor(final RouteContext routeContext) throws Exception { 066 // create the output processor 067 output = createOutputsProcessor(routeContext); 068 069 // add the output as a intercept strategy to the route context so its invoked on each processing step 070 routeContext.getInterceptStrategies().add(new InterceptStrategy() { 071 public Processor wrapProcessorInInterceptors(ProcessorDefinition processorDefinition, Processor target, Processor nextTarget) throws Exception { 072 if (nextTarget != null) { 073 // wrap in a pipeline so we continue routing to the next 074 List<Processor> list = new ArrayList<Processor>(2); 075 list.add(output); 076 list.add(nextTarget); 077 return new Pipeline(list); 078 } else { 079 return output; 080 } 081 } 082 083 @Override 084 public String toString() { 085 return "intercept[" + output + "]"; 086 } 087 }); 088 089 // remove me from the route so I am not invoked in a regular route path 090 routeContext.getRoute().getOutputs().remove(this); 091 // and return no processor to invoke next from me 092 return null; 093 } 094 095 /** 096 * Applies this interceptor only if the given predicate is true 097 * 098 * @param predicate the predicate 099 * @return the builder 100 */ 101 public ChoiceDefinition when(Predicate predicate) { 102 return choice().when(predicate); 103 } 104 105 /** 106 * This method is <b>only</b> for handling some post configuration 107 * that is needed from the Spring DSL side as JAXB does not invoke the fluent 108 * builders, so we need to manually handle this afterwards, and since this is 109 * an interceptor it has to do a bit of magic logic to fixup to handle predicates 110 * with or without proceed/stop set as well. 111 */ 112 public void afterPropertiesSet() { 113 if (getOutputs().size() == 0) { 114 // no outputs 115 return; 116 } 117 118 ProcessorDefinition first = getOutputs().get(0); 119 if (first instanceof WhenDefinition) { 120 WhenDefinition when = (WhenDefinition) first; 121 // move this outputs to the when, expect the first one 122 // as the first one is the interceptor itself 123 for (int i = 1; i < outputs.size(); i++) { 124 ProcessorDefinition out = outputs.get(i); 125 when.addOutput(out); 126 } 127 // remove the moved from the original output, by just keeping the first one 128 ProcessorDefinition keep = outputs.get(0); 129 clearOutput(); 130 outputs.add(keep); 131 } 132 } 133 134 }