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.impl;
018    
019    import org.apache.camel.Endpoint;
020    import org.apache.camel.Processor;
021    import org.apache.camel.component.mock.MockEndpoint;
022    import org.apache.camel.spi.EndpointStrategy;
023    import org.apache.camel.util.EndpointHelper;
024    import org.apache.camel.util.ObjectHelper;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
029    
030    /**
031     * A {@link EndpointStrategy} which is capable of mocking endpoints.
032     * <p/>
033     * This strategy will only apply when new endpoints is being created. If you want to apply
034     * existing endpoints, you will have to remove them from the {@link org.apache.camel.CamelContext} beforehand.
035     *
036     * @version 
037     */
038    public class InterceptSendToMockEndpointStrategy implements EndpointStrategy {
039    
040        private static final Logger LOG = LoggerFactory.getLogger(InterceptSendToMockEndpointStrategy.class);
041        private final String pattern;
042        private boolean skip;
043    
044        /**
045         * Mock all endpoints.
046         */
047        public InterceptSendToMockEndpointStrategy() {
048            this(null);
049        }
050    
051        /**
052         * Mock endpoints based on the given pattern.
053         *
054         * @param pattern the pattern.
055         * @see EndpointHelper#matchEndpoint(org.apache.camel.CamelContext, String, String)
056         */
057        public InterceptSendToMockEndpointStrategy(String pattern) {
058            this(pattern, false);
059        }
060    
061        /**
062         * Mock endpoints based on the given pattern.
063         *
064         * @param pattern the pattern.
065         * @param skip <tt>true</tt> to skip sending after the detour to the original endpoint
066         * @see EndpointHelper#matchEndpoint(org.apache.camel.CamelContext, String, String)
067         */
068        public InterceptSendToMockEndpointStrategy(String pattern, boolean skip) {
069            this.pattern = pattern;
070            this.skip = skip;
071        }
072    
073        public Endpoint registerEndpoint(String uri, Endpoint endpoint) {
074            if (endpoint instanceof InterceptSendToEndpoint) {
075                // endpoint already decorated
076                return endpoint;
077            } else if (endpoint instanceof MockEndpoint) {
078                // we should not intercept mock endpoints
079                return endpoint;
080            } else if (uri == null || pattern == null || EndpointHelper.matchEndpoint(endpoint.getCamelContext(), uri, pattern)) {
081                // if pattern is null then it mean to match all
082    
083                // only proxy if the uri is matched decorate endpoint with our proxy
084                // should be false by default
085                InterceptSendToEndpoint proxy = new InterceptSendToEndpoint(endpoint, skip);
086    
087                // create mock endpoint which we will use as interceptor
088                // replace :// from scheme to make it easy to lookup the mock endpoint without having double :// in uri
089                String key = "mock:" + endpoint.getEndpointKey().replaceFirst("://", ":");
090                // strip off parameters as well
091                if (key.contains("?")) {
092                    key = ObjectHelper.before(key, "?");
093                }
094                LOG.info("Adviced endpoint [" + uri + "] with mock endpoint [" + key + "]");
095    
096                MockEndpoint mock = endpoint.getCamelContext().getEndpoint(key, MockEndpoint.class);
097                Processor producer;
098                try {
099                    producer = mock.createProducer();
100                } catch (Exception e) {
101                    throw wrapRuntimeCamelException(e);
102                }
103    
104                proxy.setDetour(producer);
105                return proxy;
106            } else {
107                // no proxy so return regular endpoint
108                return endpoint;
109            }
110        }
111    
112        @Override
113        public String toString() {
114            return "InterceptSendToMockEndpointStrategy";
115        }
116    
117    }