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.reifier;
018
019import java.util.List;
020
021import org.apache.camel.Endpoint;
022import org.apache.camel.ExtendedCamelContext;
023import org.apache.camel.Processor;
024import org.apache.camel.Route;
025import org.apache.camel.impl.engine.DefaultInterceptSendToEndpoint;
026import org.apache.camel.model.InterceptSendToEndpointDefinition;
027import org.apache.camel.model.ProcessorDefinition;
028import org.apache.camel.model.RouteDefinition;
029import org.apache.camel.model.ToDefinition;
030import org.apache.camel.processor.InterceptEndpointProcessor;
031import org.apache.camel.spi.EndpointStrategy;
032import org.apache.camel.support.EndpointHelper;
033import org.apache.camel.util.URISupport;
034
035public class InterceptSendToEndpointReifier extends ProcessorReifier<InterceptSendToEndpointDefinition> {
036
037    public InterceptSendToEndpointReifier(Route route, ProcessorDefinition<?> definition) {
038        super(route, (InterceptSendToEndpointDefinition) definition);
039    }
040
041    @Override
042    public Processor createProcessor() throws Exception {
043        // create the before
044        final Processor before = this.createChildProcessor(true);
045        // create the after
046        Processor afterProcessor = null;
047        if (definition.getAfterUri() != null) {
048            ToDefinition to = new ToDefinition(parseString(definition.getAfterUri()));
049            // at first use custom factory
050            if (camelContext.adapt(ExtendedCamelContext.class).getProcessorFactory() != null) {
051                afterProcessor = camelContext.adapt(ExtendedCamelContext.class).getProcessorFactory().createProcessor(route, to);
052            }
053            // fallback to default implementation if factory did not create the
054            // processor
055            if (afterProcessor == null) {
056                afterProcessor = createProcessor(to);
057            }
058        }
059        final Processor after = afterProcessor;
060        final String matchURI = parseString(definition.getUri());
061
062        // register endpoint callback so we can proxy the endpoint
063        camelContext.adapt(ExtendedCamelContext.class).registerEndpointCallback(new EndpointStrategy() {
064            public Endpoint registerEndpoint(String uri, Endpoint endpoint) {
065                if (endpoint instanceof DefaultInterceptSendToEndpoint) {
066                    // endpoint already decorated
067                    return endpoint;
068                } else if (matchURI == null || matchPattern(uri, matchURI)) {
069                    // only proxy if the uri is matched decorate endpoint with
070                    // our proxy
071                    // should be false by default
072                    boolean skip = parseBoolean(definition.getSkipSendToOriginalEndpoint(), false);
073                    DefaultInterceptSendToEndpoint proxy = new DefaultInterceptSendToEndpoint(endpoint, skip);
074                    proxy.setBefore(before);
075                    proxy.setAfter(after);
076                    return proxy;
077                } else {
078                    // no proxy so return regular endpoint
079                    return endpoint;
080                }
081            }
082        });
083
084        // remove the original intercepted route from the outputs as we do not
085        // intercept as the regular interceptor
086        // instead we use the proxy endpoints producer do the triggering. That
087        // is we trigger when someone sends
088        // an exchange to the endpoint, see InterceptSendToEndpoint for details.
089        RouteDefinition route = (RouteDefinition) this.route.getRoute();
090        List<ProcessorDefinition<?>> outputs = route.getOutputs();
091        outputs.remove(definition);
092
093        return new InterceptEndpointProcessor(matchURI, before);
094    }
095
096    /**
097     * Does the uri match the pattern.
098     *
099     * @param uri the uri
100     * @param pattern the pattern, which can be an endpoint uri as well
101     * @return <tt>true</tt> if matched and we should intercept, <tt>false</tt>
102     *         if not matched, and not intercept.
103     */
104    protected boolean matchPattern(String uri, String pattern) {
105        // match using the pattern as-is
106        boolean match = EndpointHelper.matchEndpoint(camelContext, uri, pattern);
107        if (!match) {
108            try {
109                // the pattern could be an uri, so we need to normalize it
110                // before matching again
111                pattern = URISupport.normalizeUri(pattern);
112                match = EndpointHelper.matchEndpoint(camelContext, uri, pattern);
113            } catch (Exception e) {
114                // ignore
115            }
116        }
117        return match;
118    }
119
120}