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 org.apache.camel.Exchange;
020    import org.apache.camel.Message;
021    import org.apache.camel.NoTypeConversionAvailableException;
022    import org.apache.camel.converter.stream.StreamCache;
023    import org.apache.camel.spi.UnitOfWork;
024    import org.apache.camel.util.ObjectHelper;
025    
026    /**
027     * @version $Revision: 699876 $
028     */
029    public class TraceFormatter {
030        private boolean showBreadCrumb = true;
031        private boolean showNode = true;
032        private boolean showExchangeId;
033        private boolean showExchangePattern = true;
034        private boolean showProperties = true;
035        private boolean showHeaders = true;
036        private boolean showBody = true;
037        private boolean showBodyType = true;
038        private boolean showException = true;
039    
040        public Object format(TraceInterceptor interceptor, Exchange exchange) {
041            Message in = exchange.getIn();
042            Throwable exception = exchange.getException();
043            StringBuilder sb = new StringBuilder();
044            if (showBreadCrumb || showExchangeId) {
045                sb.append(getBreadCrumbID(exchange)).append(" ");
046            }
047            if (showNode) {
048                sb.append("-> ").append(getNodeMessage(interceptor)).append(" ");
049            }
050            if (showExchangePattern) {
051                sb.append(", Pattern:").append(exchange.getPattern()).append(" ");
052            }
053            // only show properties if we have any
054            if (showProperties && !exchange.getProperties().isEmpty()) {
055                sb.append(", Properties:").append(exchange.getProperties()).append(" ");
056            }
057            // only show headers if we have any
058            if (showHeaders && !in.getHeaders().isEmpty()) {
059                sb.append(", Headers:").append(in.getHeaders()).append(" ");
060            }
061            if (showBodyType) {
062                sb.append(", BodyType:").append(getBodyTypeAsString(in)).append(" ");
063            }
064            if (showBody) {
065                sb.append(", Body:").append(getBodyAsString(in)).append(" ");
066            }
067            if (showException && exception != null) {
068                sb.append(", Exception:").append(exception);
069            }
070    
071            return sb.toString();
072        }
073    
074        public boolean isShowBody() {
075            return showBody;
076        }
077    
078        public void setShowBody(boolean showBody) {
079            this.showBody = showBody;
080        }
081    
082        public boolean isShowBodyType() {
083            return showBodyType;
084        }
085    
086        public void setShowBodyType(boolean showBodyType) {
087            this.showBodyType = showBodyType;
088        }
089    
090        public boolean isShowBreadCrumb() {
091            return showBreadCrumb;
092        }
093    
094        public void setShowBreadCrumb(boolean showBreadCrumb) {
095            this.showBreadCrumb = showBreadCrumb;
096        }
097    
098        public boolean isShowExchangeId() {
099            return showExchangeId;
100        }
101    
102        public void setShowExchangeId(boolean showExchangeId) {
103            this.showExchangeId = showExchangeId;
104        }
105    
106        public boolean isShowHeaders() {
107            return showHeaders;
108        }
109    
110        public void setShowHeaders(boolean showHeaders) {
111            this.showHeaders = showHeaders;
112        }
113    
114        public boolean isShowProperties() {
115            return showProperties;
116        }
117    
118        public void setShowProperties(boolean showProperties) {
119            this.showProperties = showProperties;
120        }
121    
122        public boolean isShowNode() {
123            return showNode;
124        }
125    
126        public void setShowNode(boolean showNode) {
127            this.showNode = showNode;
128        }
129    
130        public boolean isShowExchangePattern() {
131            return showExchangePattern;
132        }
133    
134        public void setShowExchangePattern(boolean showExchangePattern) {
135            this.showExchangePattern = showExchangePattern;
136        }
137    
138        public boolean isShowException() {
139            return showException;
140        }
141    
142        public void setShowException(boolean showException) {
143            this.showException = showException;
144        }
145    
146        // Implementation methods
147        //-------------------------------------------------------------------------
148        protected Object getBreadCrumbID(Exchange exchange) {
149            UnitOfWork unitOfWork = exchange.getUnitOfWork();
150            return unitOfWork.getId();
151        }
152    
153        protected Object getBodyAsString(Message in) {
154            StreamCache newBody = null;
155            try {
156                newBody = in.getBody(StreamCache.class);
157                if (newBody != null) {
158                    in.setBody(newBody);
159                }
160            } catch (NoTypeConversionAvailableException ex) {
161                // ignore, in not of StreamCache type
162            }
163            
164            Object answer = null;
165            try {
166                answer = in.getBody(String.class);
167            } catch (NoTypeConversionAvailableException ex) {
168                answer = in.getBody();
169            }
170            
171            if (newBody != null) {
172                // Reset the InputStreamCache
173                newBody.reset();
174            }
175            return answer;
176        }
177    
178        protected Object getBodyTypeAsString(Message message) {
179            String answer = ObjectHelper.classCanonicalName(message.getBody());
180            if (answer != null && answer.startsWith("java.lang.")) {
181                return answer.substring(10);
182            }
183            return answer;
184        }
185    
186        protected String getNodeMessage(TraceInterceptor interceptor) {
187            String message = interceptor.getNode().getShortName() + "(" + interceptor.getNode().getLabel() + ")";
188            return String.format("%1$-25s", message);
189        }
190    
191    }