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.Collections;
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.XmlAttribute;
024    import javax.xml.bind.annotation.XmlRootElement;
025    
026    import org.apache.camel.Expression;
027    import org.apache.camel.Processor;
028    import org.apache.camel.model.language.HeaderExpression;
029    import org.apache.camel.processor.RoutingSlip;
030    import org.apache.camel.spi.RouteContext;
031    
032    /**
033     * Represents an XML <routingSlip/> element
034     */
035    @XmlRootElement(name = "routingSlip")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class RoutingSlipDefinition<Type extends ProcessorDefinition<Type>> extends NoOutputExpressionNode {
038        public static final String DEFAULT_DELIMITER = ",";
039    
040        @XmlAttribute
041        private String uriDelimiter;
042        @XmlAttribute
043        private Boolean ignoreInvalidEndpoints;
044    
045        public RoutingSlipDefinition() {
046            this((String)null, DEFAULT_DELIMITER);
047        }
048    
049        public RoutingSlipDefinition(String headerName) {
050            this(headerName, DEFAULT_DELIMITER);
051        }
052    
053        public RoutingSlipDefinition(String headerName, String uriDelimiter) {
054            super(new HeaderExpression(headerName));
055            setUriDelimiter(uriDelimiter);
056        }
057        
058        public RoutingSlipDefinition(Expression expression, String uriDelimiter) {
059            super(expression);
060            setUriDelimiter(uriDelimiter);
061        }
062        
063        public RoutingSlipDefinition(Expression expression) {
064            this(expression, DEFAULT_DELIMITER);
065        }
066    
067        @Override
068        public String toString() {
069            return "RoutingSlip[" + getExpression() + "]";
070        }
071    
072        @Override
073        public String getShortName() {
074            return "routingSlip";
075        }
076        
077        @Override
078        public String getLabel() {
079            return "routingSlip[" + getExpression() + "]";
080        }
081    
082        @Override
083        public Processor createProcessor(RouteContext routeContext) throws Exception {
084            Expression expression = getExpression().createExpression(routeContext);
085            String delimiter = getUriDelimiter() != null ? getUriDelimiter() : DEFAULT_DELIMITER;
086    
087            RoutingSlip routingSlip = new RoutingSlip(routeContext.getCamelContext(), expression, delimiter);
088            if (getIgnoreInvalidEndpoints() != null) {
089                routingSlip.setIgnoreInvalidEndpoints(getIgnoreInvalidEndpoints());
090            }
091            return routingSlip;
092        }
093    
094        @Override
095        public List<ProcessorDefinition<?>> getOutputs() {
096            return Collections.emptyList();
097        }
098    
099        public void setUriDelimiter(String uriDelimiter) {
100            this.uriDelimiter = uriDelimiter;
101        }
102    
103        public String getUriDelimiter() {
104            return uriDelimiter;
105        }
106        
107        public void setIgnoreInvalidEndpoints(Boolean ignoreInvalidEndpoints) {
108            this.ignoreInvalidEndpoints = ignoreInvalidEndpoints;
109        }
110        
111        public Boolean getIgnoreInvalidEndpoints() {
112            return ignoreInvalidEndpoints;
113        }
114        
115        // Fluent API
116        // -------------------------------------------------------------------------
117    
118        @Override
119        @SuppressWarnings("unchecked")
120        public Type end() {
121            // allow end() to return to previous type so you can continue in the DSL
122            return (Type) super.end();
123        }
124        
125        /**
126         * Ignore the invalidate endpoint exception when try to create a producer with that endpoint
127         *
128         * @return the builder
129         */
130        public RoutingSlipDefinition<Type> ignoreInvalidEndpoints() {
131            setIgnoreInvalidEndpoints(true);
132            return this;
133        }
134    
135        /**
136         * Sets the uri delimiter to use
137         *
138         * @param uriDelimiter the delimiter
139         * @return the builder
140         */
141        public RoutingSlipDefinition<Type> uriDelimiter(String uriDelimiter) {
142            setUriDelimiter(uriDelimiter);
143            return this;
144        }
145    }