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: 62931 $
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 showOutBody;    
039        private boolean showOutBodyType;
040        private boolean showException = true;
041    
042        public Object format(TraceInterceptor interceptor, Exchange exchange) {
043            Message in = exchange.getIn();
044           
045            // false because we don't want to introduce side effects
046            Message out = exchange.getOut(false); 
047            
048            Throwable exception = exchange.getException();
049            StringBuilder sb = new StringBuilder();
050            sb.append(getExchangeAndNode(interceptor, exchange));
051            
052            if (showExchangePattern) {
053                sb.append(", Pattern:").append(exchange.getPattern()).append(" ");
054            }
055            // only show properties if we have any
056            if (showProperties && !exchange.getProperties().isEmpty()) {
057                sb.append(", Properties:").append(exchange.getProperties()).append(" ");
058            }
059            // only show headers if we have any
060            if (showHeaders && !in.getHeaders().isEmpty()) {
061                sb.append(", Headers:").append(in.getHeaders()).append(" ");
062            }
063            if (showBodyType) {
064                sb.append(", BodyType:").append(getBodyTypeAsString(in)).append(" ");
065            }
066            if (showBody) {
067                sb.append(", Body:").append(getBodyAsString(in)).append(" ");
068            }
069            if (showOutBodyType && out != null) {
070                sb.append(", OutBodyType:").append(getBodyTypeAsString(out)).append(" ");
071            }
072            if (showOutBody && out != null) {
073                sb.append(", OutBody:").append(getBodyAsString(out)).append(" ");
074            }        
075            if (showException && exception != null) {
076                sb.append(", Exception:").append(exception);
077            }
078    
079            return sb.toString();
080        }
081    
082        public boolean isShowBody() {
083            return showBody;
084        }
085    
086        public void setShowBody(boolean showBody) {
087            this.showBody = showBody;
088        }
089    
090        public boolean isShowBodyType() {
091            return showBodyType;
092        }
093    
094        public void setShowBodyType(boolean showBodyType) {
095            this.showBodyType = showBodyType;
096        }
097    
098        public void setShowOutBody(boolean showOutBody) {
099            this.showOutBody = showOutBody;
100        }
101    
102        public boolean isShowOutBody() {
103            return showOutBody;
104        }    
105        
106        public void setShowOutBodyType(boolean showOutBodyType) {
107            this.showOutBodyType = showOutBodyType;
108        }
109    
110        public boolean isShowOutBodyType() {
111            return showOutBodyType;
112        }    
113        
114        public boolean isShowBreadCrumb() {
115            return showBreadCrumb;
116        }
117    
118        public void setShowBreadCrumb(boolean showBreadCrumb) {
119            this.showBreadCrumb = showBreadCrumb;
120        }
121    
122        public boolean isShowExchangeId() {
123            return showExchangeId;
124        }
125    
126        public void setShowExchangeId(boolean showExchangeId) {
127            this.showExchangeId = showExchangeId;
128        }
129    
130        public boolean isShowHeaders() {
131            return showHeaders;
132        }
133    
134        public void setShowHeaders(boolean showHeaders) {
135            this.showHeaders = showHeaders;
136        }
137    
138        public boolean isShowProperties() {
139            return showProperties;
140        }
141    
142        public void setShowProperties(boolean showProperties) {
143            this.showProperties = showProperties;
144        }
145    
146        public boolean isShowNode() {
147            return showNode;
148        }
149    
150        public void setShowNode(boolean showNode) {
151            this.showNode = showNode;
152        }
153    
154        public boolean isShowExchangePattern() {
155            return showExchangePattern;
156        }
157    
158        public void setShowExchangePattern(boolean showExchangePattern) {
159            this.showExchangePattern = showExchangePattern;
160        }
161    
162        public boolean isShowException() {
163            return showException;
164        }
165    
166        public void setShowException(boolean showException) {
167            this.showException = showException;
168        }
169    
170        // Implementation methods
171        //-------------------------------------------------------------------------
172        protected Object getBreadCrumbID(Exchange exchange) {
173            UnitOfWork unitOfWork = exchange.getUnitOfWork();
174            return unitOfWork.getId();
175        }
176    
177        protected Object getBodyAsString(Message in) {
178            if (in == null) {
179                return null;
180            }
181            
182            StreamCache newBody = null;
183            try {
184                newBody = in.getBody(StreamCache.class);
185                if (newBody != null) {
186                    in.setBody(newBody);
187                }
188            } catch (NoTypeConversionAvailableException ex) {
189                // ignore, in not of StreamCache type
190            }
191            
192            Object answer = null;
193            try {
194                answer = in.getBody(String.class);
195            } catch (NoTypeConversionAvailableException ex) {
196                answer = in.getBody();
197            }
198            
199            if (newBody != null) {
200                // Reset the InputStreamCache
201                newBody.reset();
202            }
203            return answer;
204        }
205    
206        protected Object getBodyTypeAsString(Message message) {
207            if (message == null) {
208                return null;
209            }
210            String answer = ObjectHelper.classCanonicalName(message.getBody());
211            if (answer != null && answer.startsWith("java.lang.")) {
212                return answer.substring(10);
213            }
214            return answer;
215        }
216    
217        protected String getNodeMessage(TraceInterceptor interceptor) {
218            String message = interceptor.getNode().getShortName() + "(" + interceptor.getNode().getLabel() + ")";
219            return String.format("%1$-25.25s", message);
220        }
221        
222        /**
223         * Returns the exchange id and node, ordered based on whether this was a trace of
224         * an exchange coming out of or into a processing step. For example, 
225         * <br/><tt>transform(body) -> ID-mojo/39713-1225468755256/2-0</tt>
226         * <br/>or
227         * <br/><tt>ID-mojo/39713-1225468755256/2-0 -> transform(body)</tt>
228         */
229        protected String getExchangeAndNode(TraceInterceptor interceptor, Exchange exchange) {
230            String id = "";
231            String node = "";
232            String result;
233            
234            if (showBreadCrumb || showExchangeId) {
235                id = getBreadCrumbID(exchange).toString();
236            }
237            if (showNode) {
238                node = getNodeMessage(interceptor);
239            }
240            if (interceptor.shouldTraceOutExchanges() && exchange.getOut(false) != null) {
241                result = node.trim() + " -> " + id.trim();
242            } else {
243                result = id.trim() + " -> " + node.trim();
244            }
245            
246            // we want to ensure text coming after this is aligned for readability
247            return String.format("%1$-65.65s", result);
248        }
249    }