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: 782956 $
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 null if one is not defined
104         */
105        public static String getContentType(Message message) {        
106            String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class);
107            if (contentType == null) {
108                // fallback with the Content-Type
109                contentType = message.getHeader("Content-Type", String.class);
110            }
111            return contentType;
112        }
113    
114        /**
115         * Extracts the body for logging purpose.
116         * <p/>
117         * Will clip the body if its too big for logging.
118         *
119         * @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
120         * @param message the message
121         * @return the logging message
122         */
123        public static String extractBodyForLogging(Message message) {
124            // default to 1000 chars
125            int length = 1000;
126    
127            String property = message.getExchange().getContext().getProperties().get(Exchange.LOG_DEBUG_BODY_MAX_CHARS);
128            if (property != null) {
129                length = message.getExchange().getContext().getTypeConverter().convertTo(Integer.class, property);
130            }
131    
132            Object obj = message.getBody();
133            if (obj == null) {
134                return "Message: [Body is null]";
135            }
136    
137            String body = obj.toString();
138            if (body == null) {
139                return "Message: [Body is null]";
140            }
141    
142            // clip body if length enabled and the body is too big
143            if (length > 0 && body.length() > length) {
144                body = body.substring(0, length) + "... [Body clipped after " + length + " chars, total length is " + body.length() + "]";
145            }
146    
147            return "Message: " + body;
148        }
149    }