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.util;
018
019import java.net.URI;
020import java.net.URISyntaxException;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Map;
027import java.util.concurrent.atomic.AtomicLong;
028import java.util.regex.PatternSyntaxException;
029
030import org.apache.camel.CamelContext;
031import org.apache.camel.DelegateEndpoint;
032import org.apache.camel.Endpoint;
033import org.apache.camel.Exchange;
034import org.apache.camel.ExchangePattern;
035import org.apache.camel.Message;
036import org.apache.camel.PollingConsumer;
037import org.apache.camel.Processor;
038import org.apache.camel.ResolveEndpointFailedException;
039import org.apache.camel.Route;
040import org.apache.camel.spi.BrowsableEndpoint;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044/**
045 * Some helper methods for working with {@link Endpoint} instances
046 *
047 * @version 
048 */
049public final class EndpointHelper {
050
051    private static final Logger LOG = LoggerFactory.getLogger(EndpointHelper.class);
052    private static final AtomicLong ENDPOINT_COUNTER = new AtomicLong(0);
053
054    private EndpointHelper() {
055        //Utility Class
056    }
057
058    /**
059     * Creates a {@link PollingConsumer} and polls all pending messages on the endpoint
060     * and invokes the given {@link Processor} to process each {@link Exchange} and then closes
061     * down the consumer and throws any exceptions thrown.
062     */
063    public static void pollEndpoint(Endpoint endpoint, Processor processor, long timeout) throws Exception {
064        PollingConsumer consumer = endpoint.createPollingConsumer();
065        try {
066            consumer.start();
067
068            while (true) {
069                Exchange exchange = consumer.receive(timeout);
070                if (exchange == null) {
071                    break;
072                } else {
073                    processor.process(exchange);
074                }
075            }
076        } finally {
077            try {
078                consumer.stop();
079            } catch (Exception e) {
080                LOG.warn("Failed to stop PollingConsumer: " + e, e);
081            }
082        }
083    }
084
085    /**
086     * Creates a {@link PollingConsumer} and polls all pending messages on the
087     * endpoint and invokes the given {@link Processor} to process each
088     * {@link Exchange} and then closes down the consumer and throws any
089     * exceptions thrown.
090     */
091    public static void pollEndpoint(Endpoint endpoint, Processor processor) throws Exception {
092        pollEndpoint(endpoint, processor, 1000L);
093    }
094
095    /**
096     * Matches the endpoint with the given pattern.
097     * <p/>
098     * The endpoint will first resolve property placeholders using {@link CamelContext#resolvePropertyPlaceholders(String)}.
099     * <p/>
100     * The match rules are applied in this order:
101     * <ul>
102     *   <li>exact match, returns true</li>
103     *   <li>wildcard match (pattern ends with a * and the uri starts with the pattern), returns true</li>
104     *   <li>regular expression match, returns true</li>
105     *   <li>otherwise returns false</li>
106     * </ul>
107     *
108     * @param context the Camel context, if <tt>null</tt> then property placeholder resolution is skipped.
109     * @param uri     the endpoint uri
110     * @param pattern a pattern to match
111     * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
112     */
113    public static boolean matchEndpoint(CamelContext context, String uri, String pattern) {
114        if (context != null) {
115            try {
116                uri = context.resolvePropertyPlaceholders(uri);
117            } catch (Exception e) {
118                throw new ResolveEndpointFailedException(uri, e);
119            }
120        }
121
122        // normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order
123        try {
124            uri = URISupport.normalizeUri(uri);
125        } catch (Exception e) {
126            throw new ResolveEndpointFailedException(uri, e);
127        }
128
129        // we need to test with and without scheme separators (//)
130        if (uri.contains("://")) {
131            // try without :// also
132            String scheme = ObjectHelper.before(uri, "://");
133            String path = ObjectHelper.after(uri, "://");
134            if (matchPattern(scheme + ":" + path, pattern)) {
135                return true;
136            }
137        } else {
138            // try with :// also
139            String scheme = ObjectHelper.before(uri, ":");
140            String path = ObjectHelper.after(uri, ":");
141            if (matchPattern(scheme + "://" + path, pattern)) {
142                return true;
143            }
144        }
145
146        // and fallback to test with the uri as is
147        return matchPattern(uri, pattern);
148    }
149
150    /**
151     * Matches the endpoint with the given pattern.
152     * @see #matchEndpoint(org.apache.camel.CamelContext, String, String)
153     *
154     * @deprecated use {@link #matchEndpoint(org.apache.camel.CamelContext, String, String)} instead.
155     */
156    @Deprecated
157    public static boolean matchEndpoint(String uri, String pattern) {
158        return matchEndpoint(null, uri, pattern);
159    }
160
161    /**
162     * Matches the name with the given pattern.
163     * <p/>
164     * The match rules are applied in this order:
165     * <ul>
166     *   <li>exact match, returns true</li>
167     *   <li>wildcard match (pattern ends with a * and the name starts with the pattern), returns true</li>
168     *   <li>regular expression match, returns true</li>
169     *   <li>otherwise returns false</li>
170     * </ul>
171     *
172     * @param name    the name
173     * @param pattern a pattern to match
174     * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
175     */
176    public static boolean matchPattern(String name, String pattern) {
177        if (name == null || pattern == null) {
178            return false;
179        }
180
181        if (name.equals(pattern)) {
182            // exact match
183            return true;
184        }
185
186        if (matchWildcard(name, pattern)) {
187            return true;
188        }
189
190        if (matchRegex(name, pattern)) {
191            return true;
192        }
193
194        // no match
195        return false;
196    }
197
198    /**
199     * Matches the name with the given pattern.
200     * <p/>
201     * The match rules are applied in this order:
202     * <ul>
203     *   <li>wildcard match (pattern ends with a * and the name starts with the pattern), returns true</li>
204     *   <li>otherwise returns false</li>
205     * </ul>
206     *
207     * @param name    the name
208     * @param pattern a pattern to match
209     * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
210     */
211    private static boolean matchWildcard(String name, String pattern) {
212        // we have wildcard support in that hence you can match with: file* to match any file endpoints
213        if (pattern.endsWith("*") && name.startsWith(pattern.substring(0, pattern.length() - 1))) {
214            return true;
215        }
216        return false;
217    }
218
219    /**
220     * Matches the name with the given pattern.
221     * <p/>
222     * The match rules are applied in this order:
223     * <ul>
224     *   <li>regular expression match, returns true</li>
225     *   <li>otherwise returns false</li>
226     * </ul>
227     *
228     * @param name    the name
229     * @param pattern a pattern to match
230     * @return <tt>true</tt> if match, <tt>false</tt> otherwise.
231     */
232    private static boolean matchRegex(String name, String pattern) {
233        // match by regular expression
234        try {
235            if (name.matches(pattern)) {
236                return true;
237            }
238        } catch (PatternSyntaxException e) {
239            // ignore
240        }
241        return false;
242    }
243
244    /**
245     * Sets the regular properties on the given bean
246     *
247     * @param context    the camel context
248     * @param bean       the bean
249     * @param parameters parameters
250     * @throws Exception is thrown if setting property fails
251     */
252    public static void setProperties(CamelContext context, Object bean, Map<String, Object> parameters) throws Exception {
253        IntrospectionSupport.setProperties(context.getTypeConverter(), bean, parameters);
254    }
255
256    /**
257     * Sets the reference properties on the given bean
258     * <p/>
259     * This is convention over configuration, setting all reference parameters (using {@link #isReferenceParameter(String)}
260     * by looking it up in registry and setting it on the bean if possible.
261     *
262     * @param context    the camel context
263     * @param bean       the bean
264     * @param parameters parameters
265     * @throws Exception is thrown if setting property fails
266     */
267    public static void setReferenceProperties(CamelContext context, Object bean, Map<String, Object> parameters) throws Exception {
268        Iterator<Map.Entry<String, Object>> it = parameters.entrySet().iterator();
269        while (it.hasNext()) {
270            Map.Entry<String, Object> entry = it.next();
271            String name = entry.getKey();
272            Object v = entry.getValue();
273            String value = v != null ? v.toString() : null;
274            if (value != null && isReferenceParameter(value)) {
275                boolean hit = IntrospectionSupport.setProperty(context, context.getTypeConverter(), bean, name, null, value, true);
276                if (hit) {
277                    // must remove as its a valid option and we could configure it
278                    it.remove();
279                }
280            }
281        }
282    }
283
284    /**
285     * Is the given parameter a reference parameter (starting with a # char)
286     *
287     * @param parameter the parameter
288     * @return <tt>true</tt> if its a reference parameter
289     */
290    public static boolean isReferenceParameter(String parameter) {
291        return parameter != null && parameter.trim().startsWith("#");
292    }
293
294    /**
295     * Resolves a reference parameter by making a lookup in the registry.
296     *
297     * @param <T>     type of object to lookup.
298     * @param context Camel context to use for lookup.
299     * @param value   reference parameter value.
300     * @param type    type of object to lookup.
301     * @return lookup result.
302     * @throws IllegalArgumentException if referenced object was not found in registry.
303     */
304    public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type) {
305        return resolveReferenceParameter(context, value, type, true);
306    }
307
308    /**
309     * Resolves a reference parameter by making a lookup in the registry.
310     *
311     * @param <T>     type of object to lookup.
312     * @param context Camel context to use for lookup.
313     * @param value   reference parameter value.
314     * @param type    type of object to lookup.
315     * @return lookup result (or <code>null</code> only if
316     *         <code>mandatory</code> is <code>false</code>).
317     * @throws IllegalArgumentException if object was not found in registry and
318     *                                  <code>mandatory</code> is <code>true</code>.
319     */
320    public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type, boolean mandatory) {
321        String valueNoHash = value.replaceAll("#", "");
322        if (mandatory) {
323            return CamelContextHelper.mandatoryLookup(context, valueNoHash, type);
324        } else {
325            return CamelContextHelper.lookup(context, valueNoHash, type);
326        }
327    }
328
329    /**
330     * Resolves a reference list parameter by making lookups in the registry.
331     * The parameter value must be one of the following:
332     * <ul>
333     *   <li>a comma-separated list of references to beans of type T</li>
334     *   <li>a single reference to a bean type T</li>
335     *   <li>a single reference to a bean of type java.util.List</li>
336     * </ul>
337     *
338     * @param context     Camel context to use for lookup.
339     * @param value       reference parameter value.
340     * @param elementType result list element type.
341     * @return list of lookup results.
342     * @throws IllegalArgumentException if any referenced object was not found in registry.
343     */
344    @SuppressWarnings({"unchecked", "rawtypes"})
345    public static <T> List<T> resolveReferenceListParameter(CamelContext context, String value, Class<T> elementType) {
346        if (value == null) {
347            return Collections.emptyList();
348        }
349        List<String> elements = Arrays.asList(value.split(","));
350        if (elements.size() == 1) {
351            Object bean = resolveReferenceParameter(context, elements.get(0).trim(), Object.class);
352            if (bean instanceof List) {
353                // The bean is a list
354                return (List) bean;
355            } else {
356                // The bean is a list element
357                return Arrays.asList(elementType.cast(bean));
358            }
359        } else { // more than one list element
360            List<T> result = new ArrayList<T>(elements.size());
361            for (String element : elements) {
362                result.add(resolveReferenceParameter(context, element.trim(), elementType));
363            }
364            return result;
365        }
366    }
367
368    /**
369     * Resolves a parameter, by doing a reference lookup if the parameter is a reference, and converting
370     * the parameter to the given type.
371     *
372     * @param <T>     type of object to convert the parameter value as.
373     * @param context Camel context to use for lookup.
374     * @param value   parameter or reference parameter value.
375     * @param type    type of object to lookup.
376     * @return lookup result if it was a reference parameter, or the value converted to the given type
377     * @throws IllegalArgumentException if referenced object was not found in registry.
378     */
379    public static <T> T resolveParameter(CamelContext context, String value, Class<T> type) {
380        T result;
381        if (EndpointHelper.isReferenceParameter(value)) {
382            result = EndpointHelper.resolveReferenceParameter(context, value, type);
383        } else {
384            result = context.getTypeConverter().convertTo(type, value);
385        }
386        return result;
387    }
388
389    /**
390     * @deprecated use {@link #resolveParameter(org.apache.camel.CamelContext, String, Class)}
391     */
392    @Deprecated
393    public static <T> T resloveStringParameter(CamelContext context, String value, Class<T> type) {
394        return resolveParameter(context, value, type);
395    }
396
397    /**
398     * Gets the route id for the given endpoint in which there is a consumer listening.
399     *
400     * @param endpoint  the endpoint
401     * @return the route id, or <tt>null</tt> if none found
402     */
403    public static String getRouteIdFromEndpoint(Endpoint endpoint) {
404        if (endpoint == null || endpoint.getCamelContext() == null) {
405            return null;
406        }
407
408        List<Route> routes = endpoint.getCamelContext().getRoutes();
409        for (Route route : routes) {
410            if (route.getEndpoint().equals(endpoint)
411                    || route.getEndpoint().getEndpointKey().equals(endpoint.getEndpointKey())) {
412                return route.getId();
413            }
414        }
415        return null;
416    }
417
418    /**
419     * A helper method for Endpoint implementations to create new Ids for Endpoints which also implement
420     * {@link org.apache.camel.spi.HasId}
421     */
422    public static String createEndpointId() {
423        return "endpoint" + ENDPOINT_COUNTER.incrementAndGet();
424    }
425
426    /**
427     * Lookup the id the given endpoint has been enlisted with in the {@link org.apache.camel.spi.Registry}.
428     *
429     * @param endpoint  the endpoint
430     * @return the endpoint id, or <tt>null</tt> if not found
431     */
432    public static String lookupEndpointRegistryId(Endpoint endpoint) {
433        if (endpoint == null || endpoint.getCamelContext() == null) {
434            return null;
435        }
436
437        // it may be a delegate endpoint, which we need to match as well
438        Endpoint delegate = null;
439        if (endpoint instanceof DelegateEndpoint) {
440            delegate = ((DelegateEndpoint) endpoint).getEndpoint();
441        }
442
443        Map<String, Endpoint> map = endpoint.getCamelContext().getRegistry().findByTypeWithName(Endpoint.class);
444        for (Map.Entry<String, Endpoint> entry : map.entrySet()) {
445            if (entry.getValue().equals(endpoint) || entry.getValue().equals(delegate)) {
446                return entry.getKey();
447            }
448        }
449
450        // not found
451        return null;
452    }
453
454    /**
455     * Browses the {@link BrowsableEndpoint} within the given range, and returns the messages as a XML payload.
456     *
457     * @param endpoint the browsable endpoint
458     * @param fromIndex  from range
459     * @param toIndex    to range
460     * @param includeBody whether to include the message body in the XML payload
461     * @return XML payload with the messages
462     * @throws IllegalArgumentException if the from and to range is invalid
463     * @see MessageHelper#dumpAsXml(org.apache.camel.Message)
464     */
465    public static String browseRangeMessagesAsXml(BrowsableEndpoint endpoint, Integer fromIndex, Integer toIndex, Boolean includeBody) {
466        if (fromIndex == null) {
467            fromIndex = 0;
468        }
469        if (toIndex == null) {
470            toIndex = Integer.MAX_VALUE;
471        }
472        if (fromIndex > toIndex) {
473            throw new IllegalArgumentException("From index cannot be larger than to index, was: " + fromIndex + " > " + toIndex);
474        }
475
476        List<Exchange> exchanges = endpoint.getExchanges();
477        if (exchanges.size() == 0) {
478            return null;
479        }
480
481        StringBuilder sb = new StringBuilder();
482        sb.append("<messages>");
483        for (int i = fromIndex; i < exchanges.size() && i <= toIndex; i++) {
484            Exchange exchange = exchanges.get(i);
485            Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
486            String xml = MessageHelper.dumpAsXml(msg, includeBody);
487            sb.append("\n").append(xml);
488        }
489        sb.append("\n</messages>");
490        return sb.toString();
491    }
492
493    /**
494     * Attempts to resolve if the url has an <tt>exchangePattern</tt> option configured
495     *
496     * @param url the url
497     * @return the exchange pattern, or <tt>null</tt> if the url has no <tt>exchangePattern</tt> configured.
498     * @throws URISyntaxException is thrown if uri is invalid
499     */
500    public static ExchangePattern resolveExchangePatternFromUrl(String url) throws URISyntaxException {
501        URI uri = new URI(url);
502        Map<String, Object> parameters = URISupport.parseParameters(uri);
503        String pattern = (String) parameters.get("exchangePattern");
504        if (pattern != null) {
505            return ExchangePattern.asEnum(pattern);
506        }
507        return null;
508    }
509
510}