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.Message;
020    import org.apache.camel.NoTypeConversionAvailableException;
021    import org.apache.camel.converter.stream.StreamCache;
022    
023    /**
024     * Some helper methods when working with {@link org.apache.camel.Message}.
025     *
026     * @version $Revision: 740251 $
027     */
028    public final class MessageHelper {
029    
030        /**
031         * Utility classes should not have a public constructor.
032         */
033        private MessageHelper() {
034        }
035    
036        /**
037         * Extracts the given body and returns it as a String, that
038         * can be used for logging etc.
039         * <p/>
040         * Will handle stream based bodies wrapped in StreamCache.
041         *
042         * @param message  the message with the body
043         * @return the body as String, can return <tt>null</null> if no body
044         */
045        public static String extractBodyAsString(Message message) {
046            if (message == null) {
047                return null;
048            }
049    
050            StreamCache newBody = null;
051            try {
052                newBody = message.getBody(StreamCache.class);
053                if (newBody != null) {
054                    message.setBody(newBody);
055                }
056            } catch (NoTypeConversionAvailableException ex) {
057                // ignore, in not of StreamCache type
058            }
059    
060            Object answer;
061            try {
062                answer = message.getBody(String.class);
063            } catch (NoTypeConversionAvailableException ex) {
064                answer = message.getBody();
065            }
066    
067            if (newBody != null) {
068                // Reset the InputStreamCache
069                newBody.reset();
070            }
071    
072            return answer != null ? answer.toString() : null;
073        }
074    
075        /**
076         * Gets the given body class type name as a String.
077         * <p/>
078         * Will skip java.lang. for the build in Java types.
079         *
080         * @param message  the message with the body
081         * @return the body typename as String, can return <tt>null</null> if no body
082         */
083        public static String getBodyTypeName(Message message) {
084            if (message == null) {
085                return null;
086            }
087            String answer = ObjectHelper.classCanonicalName(message.getBody());
088            if (answer != null && answer.startsWith("java.lang.")) {
089                return answer.substring(10);
090            }
091            return answer;
092        }
093        
094        /**
095         * If the message body contains a {@link StreamCache} instance, reset the cache to 
096         * enable reading from it again.
097         * 
098         * @param message the message for which to reset the body
099         */
100        public static void resetStreamCache(Message message) {
101            if (message == null) {
102                return;
103            }
104            if (message.getBody() instanceof StreamCache) {
105                ((StreamCache) message.getBody()).reset();
106            }
107        }
108    }