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.component.log;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Message;
021    import org.apache.camel.NoTypeConversionAvailableException;
022    import org.apache.camel.StreamCache;
023    import org.apache.camel.spi.ExchangeFormatter;
024    import org.apache.camel.util.ObjectHelper;
025    
026    /**
027     * Log formatter to format the logging output.
028     */
029    public class LogFormatter implements ExchangeFormatter {
030    
031        private boolean showExchangeId;
032        private boolean showProperties;
033        private boolean showHeaders;
034        private boolean showBodyType = true;
035        private boolean showBody = true;
036        private boolean showOut;
037        private boolean showAll;
038        private boolean multiline;
039    
040        public Object format(Exchange exchange) {
041            Message in = exchange.getIn();
042    
043            StringBuilder sb = new StringBuilder("");
044            if (showAll || showExchangeId) {
045                if (multiline) {
046                    sb.append('\n');
047                }
048                sb.append(", Id:").append(exchange.getExchangeId());
049            }
050            if (showAll || showProperties) {
051                if (multiline) {
052                    sb.append('\n');
053                }
054                sb.append(", Properties:").append(exchange.getProperties());
055            }
056            if (showAll || showHeaders) {
057                if (multiline) {
058                    sb.append('\n');
059                }
060                sb.append(", Headers:").append(in.getHeaders());
061            }
062            if (showAll || showBodyType) {
063                if (multiline) {
064                    sb.append('\n');
065                }
066                sb.append(", BodyType:").append(getBodyTypeAsString(in));
067            }
068            if (showAll || showBody) {
069                if (multiline) {
070                    sb.append('\n');
071                }
072                sb.append(", Body:").append(getBodyAsString(in));
073            }
074    
075            Message out = exchange.getOut(false);
076            if (showAll || showOut) {
077                if (out != null) {
078                    if (showAll || showHeaders) {
079                        if (multiline) {
080                            sb.append('\n');
081                        }
082                        sb.append(", OutHeaders:").append(out.getHeaders());
083                    }
084                    if (showAll || showBodyType) {
085                        if (multiline) {
086                            sb.append('\n');
087                        }
088                        sb.append(", OutBodyType:").append(getBodyTypeAsString(out));
089                    }
090                    if (showAll || showBody) {
091                        if (multiline) {
092                            sb.append('\n');
093                        }
094                        sb.append(", OutBody:").append(getBodyAsString(out));
095                    }
096                } else {
097                    if (multiline) {
098                        sb.append('\n');
099                    }
100                    sb.append(", Out: null");
101                }
102            }
103    
104            // get rid of the leading space comma if needed
105            return "Exchange[" + (multiline ? sb.append(']').toString() : sb.toString().substring(2) + "]");
106        }
107    
108        public boolean isShowExchangeId() {
109            return showExchangeId;
110        }
111    
112        public void setShowExchangeId(boolean showExchangeId) {
113            this.showExchangeId = showExchangeId;
114        }
115    
116        public boolean isShowProperties() {
117            return showProperties;
118        }
119    
120        public void setShowProperties(boolean showProperties) {
121            this.showProperties = showProperties;
122        }
123    
124        public boolean isShowHeaders() {
125            return showHeaders;
126        }
127    
128        public void setShowHeaders(boolean showHeaders) {
129            this.showHeaders = showHeaders;
130        }
131    
132        public boolean isShowBodyType() {
133            return showBodyType;
134        }
135    
136        public void setShowBodyType(boolean showBodyType) {
137            this.showBodyType = showBodyType;
138        }
139    
140        public boolean isShowBody() {
141            return showBody;
142        }
143    
144        public void setShowBody(boolean showBody) {
145            this.showBody = showBody;
146        }
147    
148        public boolean isShowOut() {
149            return showOut;
150        }
151    
152        public void setShowOut(boolean showOut) {
153            this.showOut = showOut;
154        }
155    
156        public boolean isShowAll() {
157            return showAll;
158        }
159    
160        public void setShowAll(boolean showAll) {
161            this.showAll = showAll;
162        }
163    
164        public boolean isMultiline() {
165            return multiline;
166        }
167    
168        /**
169         * If enabled then each information is outputted on a newline.
170         */
171        public void setMultiline(boolean multiline) {
172            this.multiline = multiline;
173        }
174    
175        // Implementation methods
176        //-------------------------------------------------------------------------
177        protected Object getBodyAsString(Message message) {
178            StreamCache newBody = null;
179            try {
180                newBody = message.getBody(StreamCache.class);
181                if (newBody != null) {
182                    message.setBody(newBody);
183                }
184            } catch (NoTypeConversionAvailableException ex) {
185                // ignore
186            }
187            Object answer = null;
188            try {
189                answer = message.getBody(String.class);
190            } catch (NoTypeConversionAvailableException ex) {
191                answer = message.getBody();
192            }
193    
194            if (newBody != null) {
195                // Reset the StreamCache
196                newBody.reset();
197            }
198            return answer;
199        }
200    
201        protected Object getBodyTypeAsString(Message message) {
202            String answer = ObjectHelper.classCanonicalName(message.getBody());
203            if (answer != null && answer.startsWith("java.lang.")) {
204                return answer.substring(10);
205            }
206            return answer;
207        }
208    
209    }