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.Exchange;
020    import org.apache.camel.Message;
021    import org.apache.camel.StreamCache;
022    
023    /**
024     * Some helper methods when working with {@link org.apache.camel.Message}.
025     *
026     * @version $Revision: 790811 $
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 = message.getBody(StreamCache.class);
051            if (newBody != null) {
052                message.setBody(newBody);
053            }
054    
055            Object answer = message.getBody(String.class);
056            if (answer == null) {
057                answer = message.getBody();
058            }
059    
060            if (newBody != null) {
061                // Reset the InputStreamCache
062                newBody.reset();
063            }
064    
065            return answer != null ? answer.toString() : null;
066        }
067    
068        /**
069         * Gets the given body class type name as a String.
070         * <p/>
071         * Will skip java.lang. for the build in Java types.
072         *
073         * @param message  the message with the body
074         * @return the body typename as String, can return <tt>null</null> if no body
075         */
076        public static String getBodyTypeName(Message message) {
077            if (message == null) {
078                return null;
079            }
080            String answer = ObjectHelper.classCanonicalName(message.getBody());
081            if (answer != null && answer.startsWith("java.lang.")) {
082                return answer.substring(10);
083            }
084            return answer;
085        }
086        
087        /**
088         * If the message body contains a {@link StreamCache} instance, reset the cache to 
089         * enable reading from it again.
090         * 
091         * @param message the message for which to reset the body
092         */
093        public static void resetStreamCache(Message message) {
094            if (message == null) {
095                return;
096            }
097            if (message.getBody() instanceof StreamCache) {
098                ((StreamCache) message.getBody()).reset();
099            }
100        }
101        
102        /**
103         * Returns the MIME content type on the message or <tt>null</tt> if none defined
104         */
105        public static String getContentType(Message message) {        
106            return message.getHeader(Exchange.CONTENT_TYPE, String.class);
107        }
108    
109        /**
110         * Returns the MIME content encoding on the message or <tt>null</tt> if none defined
111         */
112        public static String getContentEncoding(Message message) {
113            return message.getHeader(Exchange.CONTENT_ENCODING, String.class);
114        }
115    
116        /**
117         * Extracts the body for logging purpose.
118         * <p/>
119         * Will clip the body if its too big for logging.
120         *
121         * @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
122         * @param message the message
123         * @return the logging message
124         */
125        public static String extractBodyForLogging(Message message) {
126            // default to 1000 chars
127            int length = 1000;
128    
129            String property = message.getExchange().getContext().getProperties().get(Exchange.LOG_DEBUG_BODY_MAX_CHARS);
130            if (property != null) {
131                length = message.getExchange().getContext().getTypeConverter().convertTo(Integer.class, property);
132            }
133    
134            Object obj = message.getBody();
135            if (obj == null) {
136                return "Message: [Body is null]";
137            }
138    
139            String body = obj.toString();
140            if (body == null) {
141                return "Message: [Body is null]";
142            }
143    
144            // clip body if length enabled and the body is too big
145            if (length > 0 && body.length() > length) {
146                body = body.substring(0, length) + "... [Body clipped after " + length + " chars, total length is " + body.length() + "]";
147            }
148    
149            return "Message: " + body;
150        }
151    }