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.CamelContext;
027 import org.apache.camel.Predicate;
028 import org.apache.camel.Processor;
029 import org.apache.camel.processor.Pipeline;
030 import org.apache.camel.spi.InterceptStrategy;
031 import org.apache.camel.spi.RouteContext;
032
033 /**
034 * Represents an XML <intercept/> element
035 *
036 * @version
037 */
038 @XmlRootElement(name = "intercept")
039 @XmlAccessorType(XmlAccessType.FIELD)
040 public class InterceptDefinition extends OutputDefinition<InterceptDefinition> {
041 @XmlTransient
042 protected Processor output;
043 @XmlTransient
044 protected final List<Processor> intercepted = new ArrayList<Processor>();
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 boolean isAbstract() {
066 return true;
067 }
068
069 @Override
070 public Processor createProcessor(final RouteContext routeContext) throws Exception {
071 // create the output processor
072 output = this.createChildProcessor(routeContext, true);
073
074 // add the output as a intercept strategy to the route context so its invoked on each processing step
075 routeContext.getInterceptStrategies().add(new InterceptStrategy() {
076 private Processor interceptedTarget;
077
078 public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition,
079 Processor target, Processor nextTarget) throws Exception {
080 // store the target we are intercepting
081 this.interceptedTarget = target;
082
083 // remember the target that was intercepted
084 intercepted.add(interceptedTarget);
085
086 if (interceptedTarget != null) {
087 // wrap in a pipeline so we continue routing to the next
088 List<Processor> list = new ArrayList<Processor>(2);
089 list.add(output);
090 list.add(interceptedTarget);
091 return new Pipeline(context, list);
092 } else {
093 return output;
094 }
095 }
096
097 @Override
098 public String toString() {
099 return "intercept[" + (interceptedTarget != null ? interceptedTarget : output) + "]";
100 }
101 });
102
103 // remove me from the route so I am not invoked in a regular route path
104 routeContext.getRoute().getOutputs().remove(this);
105 // and return no processor to invoke next from me
106 return null;
107 }
108
109 /**
110 * Applies this interceptor only if the given predicate is true
111 *
112 * @param predicate the predicate
113 * @return the builder
114 */
115 public InterceptDefinition when(Predicate predicate) {
116 WhenDefinition when = new WhenDefinition(predicate);
117 addOutput(when);
118 return this;
119 }
120
121 /**
122 * This method is <b>only</b> for handling some post configuration
123 * that is needed since this is an interceptor, and we have to do
124 * a bit of magic logic to fixup to handle predicates
125 * with or without proceed/stop set as well.
126 */
127 public void afterPropertiesSet() {
128 if (getOutputs().size() == 0) {
129 // no outputs
130 return;
131 }
132
133 ProcessorDefinition<?> first = getOutputs().get(0);
134 if (first instanceof WhenDefinition) {
135 WhenDefinition when = (WhenDefinition) first;
136 // move this outputs to the when, expect the first one
137 // as the first one is the interceptor itself
138 for (int i = 1; i < outputs.size(); i++) {
139 ProcessorDefinition<?> out = outputs.get(i);
140 when.addOutput(out);
141 }
142 // remove the moved from the original output, by just keeping the first one
143 ProcessorDefinition<?> keep = outputs.get(0);
144 clearOutput();
145 outputs.add(keep);
146 }
147 }
148
149 public Processor getInterceptedProcessor(int index) {
150 // avoid out of bounds
151 if (index <= intercepted.size() - 1) {
152 return intercepted.get(index);
153 } else {
154 return null;
155 }
156 }
157 }