| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| ExchangeHelper |
|
| 0.0;0 |
| 1 | /** |
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
|
| 4 | * this work for additional information regarding copyright ownership. |
|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
| 6 | * (the "License"); you may not use this file except in compliance with |
|
| 7 | * the License. You may obtain a copy of the License at |
|
| 8 | * |
|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 10 | * |
|
| 11 | * Unless required by applicable law or agreed to in writing, software |
|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 14 | * See the License for the specific language governing permissions and |
|
| 15 | * limitations under the License. |
|
| 16 | */ |
|
| 17 | package org.apache.camel.util; |
|
| 18 | ||
| 19 | import org.apache.camel.Endpoint; |
|
| 20 | import org.apache.camel.Exchange; |
|
| 21 | import org.apache.camel.InvalidPayloadException; |
|
| 22 | import org.apache.camel.InvalidTypeException; |
|
| 23 | import org.apache.camel.NoSuchEndpointException; |
|
| 24 | import org.apache.camel.NoSuchPropertyException; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * Some helper methods for working with {@link Exchange} objects |
|
| 28 | * |
|
| 29 | * @version $Revision: 563607 $ |
|
| 30 | */ |
|
| 31 | public class ExchangeHelper { |
|
| 32 | ||
| 33 | /** |
|
| 34 | * Utility classes should not have a public constructor. |
|
| 35 | */ |
|
| 36 | 0 | private ExchangeHelper() { |
| 37 | 0 | } |
| 38 | ||
| 39 | /** |
|
| 40 | * Attempts to resolve the endpoint for the given value |
|
| 41 | * |
|
| 42 | * @param exchange the message exchange being processed |
|
| 43 | * @param value the value which can be an {@link Endpoint} or an object |
|
| 44 | * which provides a String representation of an endpoint via |
|
| 45 | * {@link #toString()} |
|
| 46 | * |
|
| 47 | * @return the endpoint |
|
| 48 | * @throws NoSuchEndpointException if the endpoint cannot be resolved |
|
| 49 | */ |
|
| 50 | @SuppressWarnings({"unchecked" }) |
|
| 51 | public static <E extends Exchange> Endpoint<E> resolveEndpoint(E exchange, Object value) |
|
| 52 | throws NoSuchEndpointException { |
|
| 53 | Endpoint<E> endpoint; |
|
| 54 | 9 | if (value instanceof Endpoint) { |
| 55 | 0 | endpoint = (Endpoint<E>)value; |
| 56 | 0 | } else { |
| 57 | 9 | String uri = value.toString(); |
| 58 | 9 | endpoint = CamelContextHelper.getMandatoryEndpoint(exchange.getContext(), uri); |
| 59 | } |
|
| 60 | 9 | return endpoint; |
| 61 | } |
|
| 62 | ||
| 63 | public static <T> T getMandatoryProperty(Exchange exchange, String propertyName, Class<T> type) |
|
| 64 | throws NoSuchPropertyException { |
|
| 65 | 45 | T answer = exchange.getProperty(propertyName, type); |
| 66 | 45 | if (answer == null) { |
| 67 | 6 | throw new NoSuchPropertyException(exchange, propertyName, type); |
| 68 | } |
|
| 69 | 39 | return answer; |
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * Returns the mandatory inbound message body of the correct type or throws |
|
| 74 | * an exception if it is not present |
|
| 75 | */ |
|
| 76 | public static <T> T getMandatoryInBody(Exchange exchange, Class<T> type) throws InvalidPayloadException { |
|
| 77 | 18 | T answer = exchange.getIn().getBody(type); |
| 78 | 18 | if (answer == null) { |
| 79 | 0 | throw new InvalidPayloadException(exchange, type); |
| 80 | } |
|
| 81 | 18 | return answer; |
| 82 | } |
|
| 83 | ||
| 84 | /** |
|
| 85 | * Converts the value to the given expected type or throws an exception |
|
| 86 | */ |
|
| 87 | public static <T> T convertToMandatoryType(Exchange exchange, Class<T> type, Object value) |
|
| 88 | throws InvalidTypeException { |
|
| 89 | 0 | T answer = convertToType(exchange, type, value); |
| 90 | 0 | if (answer == null) { |
| 91 | 0 | throw new InvalidTypeException(exchange, value, type); |
| 92 | } |
|
| 93 | 0 | return answer; |
| 94 | } |
|
| 95 | ||
| 96 | /** |
|
| 97 | * Converts the value to the given expected type returning null if it could |
|
| 98 | * not be converted |
|
| 99 | */ |
|
| 100 | public static <T> T convertToType(Exchange exchange, Class<T> type, Object value) { |
|
| 101 | 0 | return exchange.getContext().getTypeConverter().convertTo(type, value); |
| 102 | } |
|
| 103 | } |