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 java.util.Collections;
020import java.util.List;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.camel.AsyncProcessor;
027import org.apache.camel.ErrorHandlerFactory;
028import org.apache.camel.Exchange;
029import org.apache.camel.Expression;
030import org.apache.camel.Processor;
031import org.apache.camel.model.language.ExpressionDefinition;
032import org.apache.camel.model.language.HeaderExpression;
033import org.apache.camel.processor.RoutingSlip;
034import org.apache.camel.processor.SendDynamicProcessor;
035import org.apache.camel.spi.Metadata;
036import org.apache.camel.spi.RouteContext;
037
038import static org.apache.camel.builder.ExpressionBuilder.headerExpression;
039
040/**
041 * Routes a message through a series of steps that are pre-determined (the slip)
042 */
043@Metadata(label = "eip,endpoint,routing")
044@XmlRootElement(name = "routingSlip")
045@XmlAccessorType(XmlAccessType.FIELD)
046public class RoutingSlipDefinition<Type extends ProcessorDefinition<Type>> extends NoOutputExpressionNode {
047    public static final String DEFAULT_DELIMITER = ",";
048
049    @XmlAttribute @Metadata(defaultValue = ",")
050    private String uriDelimiter;
051    @XmlAttribute
052    private Boolean ignoreInvalidEndpoints;
053    @XmlAttribute
054    private Integer cacheSize;
055
056    public RoutingSlipDefinition() {
057        this((String)null, DEFAULT_DELIMITER);
058    }
059
060    public RoutingSlipDefinition(String headerName) {
061        this(headerName, DEFAULT_DELIMITER);
062    }
063
064    public RoutingSlipDefinition(String headerName, String uriDelimiter) {
065        super(new HeaderExpression(headerName));
066        setUriDelimiter(uriDelimiter);
067    }
068    
069    public RoutingSlipDefinition(Expression expression, String uriDelimiter) {
070        super(expression);
071        setUriDelimiter(uriDelimiter);
072    }
073    
074    public RoutingSlipDefinition(Expression expression) {
075        this(expression, DEFAULT_DELIMITER);
076    }
077
078    @Override
079    public String toString() {
080        return "RoutingSlip[" + getExpression() + "]";
081    }
082
083    @Override
084    public String getLabel() {
085        return "routingSlip[" + getExpression() + "]";
086    }
087
088    @Override
089    public Processor createProcessor(RouteContext routeContext) throws Exception {
090        Expression expression = getExpression().createExpression(routeContext);
091        String delimiter = getUriDelimiter() != null ? getUriDelimiter() : DEFAULT_DELIMITER;
092
093        RoutingSlip routingSlip = new RoutingSlip(routeContext.getCamelContext(), expression, delimiter);
094        if (getIgnoreInvalidEndpoints() != null) {
095            routingSlip.setIgnoreInvalidEndpoints(getIgnoreInvalidEndpoints());
096        }
097        if (getCacheSize() != null) {
098            routingSlip.setCacheSize(getCacheSize());
099        }
100
101        // and wrap this in an error handler
102        ErrorHandlerFactory builder = routeContext.getRoute().getErrorHandlerBuilder();
103        // create error handler (create error handler directly to keep it light weight,
104        // instead of using ProcessorDefinition.wrapInErrorHandler)
105        AsyncProcessor errorHandler = (AsyncProcessor) builder.createErrorHandler(routeContext, routingSlip.newRoutingSlipProcessorForErrorHandler());
106        routingSlip.setErrorHandler(errorHandler);
107
108        return routingSlip;
109    }
110
111    @Override
112    public List<ProcessorDefinition<?>> getOutputs() {
113        return Collections.emptyList();
114    }
115
116    /**
117     * Expression to define the routing slip, which defines which endpoints to route the message in a pipeline style.
118     * Notice the expression is evaluated once, if you want a more dynamic style, then the dynamic router eip is a better choice.
119     */
120    @Override
121    public void setExpression(ExpressionDefinition expression) {
122        // override to include javadoc what the expression is used for
123        super.setExpression(expression);
124    }
125
126    public void setUriDelimiter(String uriDelimiter) {
127        this.uriDelimiter = uriDelimiter;
128    }
129
130    public String getUriDelimiter() {
131        return uriDelimiter;
132    }
133    
134    public void setIgnoreInvalidEndpoints(Boolean ignoreInvalidEndpoints) {
135        this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
136    }
137    
138    public Boolean getIgnoreInvalidEndpoints() {
139        return ignoreInvalidEndpoints;
140    }
141
142    public Integer getCacheSize() {
143        return cacheSize;
144    }
145
146    public void setCacheSize(Integer cacheSize) {
147        this.cacheSize = cacheSize;
148    }
149
150    // Fluent API
151    // -------------------------------------------------------------------------
152
153    @Override
154    @SuppressWarnings("unchecked")
155    public Type end() {
156        // allow end() to return to previous type so you can continue in the DSL
157        return (Type) super.end();
158    }
159    
160    /**
161     * Ignore the invalidate endpoint exception when try to create a producer with that endpoint
162     *
163     * @return the builder
164     */
165    public RoutingSlipDefinition<Type> ignoreInvalidEndpoints() {
166        setIgnoreInvalidEndpoints(true);
167        return this;
168    }
169
170    /**
171     * Sets the uri delimiter to use
172     *
173     * @param uriDelimiter the delimiter
174     * @return the builder
175     */
176    public RoutingSlipDefinition<Type> uriDelimiter(String uriDelimiter) {
177        setUriDelimiter(uriDelimiter);
178        return this;
179    }
180
181    /**
182     * Sets the maximum size used by the {@link org.apache.camel.impl.ProducerCache} which is used
183     * to cache and reuse producers when using this routing slip, when uris are reused.
184     *
185     * @param cacheSize  the cache size, use <tt>0</tt> for default cache size, or <tt>-1</tt> to turn cache off.
186     * @return the builder
187     */
188    public RoutingSlipDefinition<Type> cacheSize(int cacheSize) {
189        setCacheSize(cacheSize);
190        return this;
191    }
192
193}