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.processor.interceptor;
018    
019    import java.io.InputStream;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Message;
023    import org.apache.camel.converter.stream.StreamCache;
024    import org.apache.camel.converter.stream.StreamCacheConverter;
025    import org.apache.camel.spi.UnitOfWork;
026    import org.apache.camel.util.ObjectHelper;
027    
028    /**
029     * @version $Revision: 47491 $
030     */
031    public class TraceFormatter {
032        private boolean showBreadCrumb = true;
033        private boolean showNode = true;
034        private boolean showExchangeId;
035        private boolean showProperties = true;
036        private boolean showHeaders = true;
037        private boolean showBody = true;
038        private boolean showBodyType = true;
039    
040        public Object format(TraceInterceptor interceptor, Exchange exchange) {
041            Message in = exchange.getIn();
042            Throwable exception = exchange.getException();
043            return (showBreadCrumb ? getBreadCrumbID(exchange) + " " : "")
044                    + "-> " + getNodeMessage(interceptor) + " "
045                    + (showNode ? interceptor.getNode() + " " : "")
046                    + exchange.getPattern()
047                    + (showExchangeId ? " Id: " + exchange.getExchangeId() : "")
048                    + (showProperties ? " Properties:" + exchange.getProperties() : "")
049                    + (showHeaders ? " Headers:" + in.getHeaders() : "")
050                    + (showBodyType ? " BodyType:" + getBodyTypeAsString(in) : "")
051                    + (showBody ? " Body:" + getBodyAsString(in) : "")
052                    + (exception != null ? " Exception: " + exception : "");
053        }
054    
055        public boolean isShowBody() {
056            return showBody;
057        }
058    
059        public void setShowBody(boolean showBody) {
060            this.showBody = showBody;
061        }
062    
063        public boolean isShowBodyType() {
064            return showBodyType;
065        }
066    
067        public void setShowBodyType(boolean showBodyType) {
068            this.showBodyType = showBodyType;
069        }
070    
071        public boolean isShowBreadCrumb() {
072            return showBreadCrumb;
073        }
074    
075        public void setShowBreadCrumb(boolean showBreadCrumb) {
076            this.showBreadCrumb = showBreadCrumb;
077        }
078    
079        public boolean isShowExchangeId() {
080            return showExchangeId;
081        }
082    
083        public void setShowExchangeId(boolean showExchangeId) {
084            this.showExchangeId = showExchangeId;
085        }
086    
087        public boolean isShowHeaders() {
088            return showHeaders;
089        }
090    
091        public void setShowHeaders(boolean showHeaders) {
092            this.showHeaders = showHeaders;
093        }
094    
095        public boolean isShowProperties() {
096            return showProperties;
097        }
098    
099        public void setShowProperties(boolean showProperties) {
100            this.showProperties = showProperties;
101        }
102    
103        public boolean isShowNode() {
104            return showNode;
105        }
106    
107        public void setShowNode(boolean showNode) {
108            this.showNode = showNode;
109        }
110    
111        // Implementation methods
112        //-------------------------------------------------------------------------
113        protected Object getBreadCrumbID(Exchange exchange) {
114            UnitOfWork unitOfWork = exchange.getUnitOfWork();
115            return unitOfWork.getId();
116        }
117    
118        protected Object getBodyAsString(Message in) {
119    
120            StreamCache newBody = in.getBody(StreamCache.class);
121            if (newBody != null) {
122                in.setBody(newBody);
123            }
124            Object answer = in.getBody(String.class);
125            if (answer == null) {
126                answer = in.getBody();
127            }
128            if (newBody != null) {
129                // Reset the InputStreamCache
130                newBody.reset();
131            }
132            return answer;
133        }
134    
135        protected Object getBodyTypeAsString(Message message) {
136            String answer = ObjectHelper.classCanonicalName(message.getBody());
137            if (answer != null && answer.startsWith("java.lang.")) {
138                return answer.substring(10);
139            }
140            return answer;
141        }
142    
143        protected String getNodeMessage(TraceInterceptor interceptor) {
144            return interceptor.getNode().idOrCreate();
145        }
146    
147    }