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.impl; 018 019import org.apache.camel.Endpoint; 020import org.apache.camel.Producer; 021import org.apache.camel.component.mock.MockEndpoint; 022import org.apache.camel.spi.EndpointStrategy; 023import org.apache.camel.util.EndpointHelper; 024import org.apache.camel.util.ObjectHelper; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import 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 are being created. If you want to replace 034 * existing endpoints, you will have to remove them from the {@link org.apache.camel.CamelContext} beforehand. 035 * 036 * @version 037 */ 038public 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 (matchPattern(uri, endpoint, 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 [{}] with mock endpoint [{}]", uri, key); 095 096 MockEndpoint mock = endpoint.getCamelContext().getEndpoint(key, MockEndpoint.class); 097 Producer producer; 098 try { 099 producer = mock.createProducer(); 100 } catch (Exception e) { 101 throw wrapRuntimeCamelException(e); 102 } 103 104 // allow custom logic 105 producer = onInterceptEndpoint(uri, endpoint, mock, producer); 106 proxy.setDetour(producer); 107 108 return proxy; 109 } else { 110 // no proxy so return regular endpoint 111 return endpoint; 112 } 113 } 114 115 /** 116 * Does the pattern match the endpoint? 117 * 118 * @param uri the uri 119 * @param endpoint the endpoint 120 * @param pattern the pattern 121 * @return <tt>true</tt> to match and therefore intercept, <tt>false</tt> if not matched and should not intercept 122 */ 123 protected boolean matchPattern(String uri, Endpoint endpoint, String pattern) { 124 return uri == null || pattern == null || EndpointHelper.matchEndpoint(endpoint.getCamelContext(), uri, pattern); 125 } 126 127 /** 128 * Callback when an endpoint was intercepted with the given mock endpoint 129 * 130 * @param uri the uri 131 * @param endpoint the endpoint 132 * @param mockEndpoint the mocked endpoint 133 * @param mockProducer the mock producer 134 * @return the mock producer 135 */ 136 protected Producer onInterceptEndpoint(String uri, Endpoint endpoint, MockEndpoint mockEndpoint, Producer mockProducer) { 137 return mockProducer; 138 } 139 140 @Override 141 public String toString() { 142 return "InterceptSendToMockEndpointStrategy"; 143 } 144 145}