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.util;
018    
019    import org.apache.camel.Endpoint;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.InvalidPayloadException;
022    import org.apache.camel.InvalidTypeException;
023    import org.apache.camel.NoSuchEndpointException;
024    import org.apache.camel.NoSuchPropertyException;
025    
026    /**
027     * Some helper methods for working with {@link Exchange} objects
028     * 
029     * @version $Revision: 563607 $
030     */
031    public class ExchangeHelper {
032    
033        /**
034         * Utility classes should not have a public constructor.
035         */
036        private ExchangeHelper() {        
037        }
038        
039        /**
040         * Attempts to resolve the endpoint for the given value
041         * 
042         * @param exchange the message exchange being processed
043         * @param value the value which can be an {@link Endpoint} or an object
044         *                which provides a String representation of an endpoint via
045         *                {@link #toString()}
046         * 
047         * @return the endpoint
048         * @throws NoSuchEndpointException if the endpoint cannot be resolved
049         */
050        @SuppressWarnings({"unchecked" })
051        public static <E extends Exchange> Endpoint<E> resolveEndpoint(E exchange, Object value)
052            throws NoSuchEndpointException {
053            Endpoint<E> endpoint;
054            if (value instanceof Endpoint) {
055                endpoint = (Endpoint<E>)value;
056            } else {
057                String uri = value.toString();
058                endpoint = CamelContextHelper.getMandatoryEndpoint(exchange.getContext(), uri);
059            }
060            return endpoint;
061        }
062    
063        public static <T> T getMandatoryProperty(Exchange exchange, String propertyName, Class<T> type)
064            throws NoSuchPropertyException {
065            T answer = exchange.getProperty(propertyName, type);
066            if (answer == null) {
067                throw new NoSuchPropertyException(exchange, propertyName, type);
068            }
069            return answer;
070        }
071    
072        /**
073         * Returns the mandatory inbound message body of the correct type or throws
074         * an exception if it is not present
075         */
076        public static <T> T getMandatoryInBody(Exchange exchange, Class<T> type) throws InvalidPayloadException {
077            T answer = exchange.getIn().getBody(type);
078            if (answer == null) {
079                throw new InvalidPayloadException(exchange, type);
080            }
081            return answer;
082        }
083    
084        /**
085         * Converts the value to the given expected type or throws an exception
086         */
087        public static <T> T convertToMandatoryType(Exchange exchange, Class<T> type, Object value)
088            throws InvalidTypeException {
089            T answer = convertToType(exchange, type, value);
090            if (answer == null) {
091                throw new InvalidTypeException(exchange, value, type);
092            }
093            return answer;
094        }
095    
096        /**
097         * Converts the value to the given expected type returning null if it could
098         * not be converted
099         */
100        public static <T> T convertToType(Exchange exchange, Class<T> type, Object value) {
101            return exchange.getContext().getTypeConverter().convertTo(type, value);
102        }
103    }