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.Serializable;
020    import java.util.Date;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Message;
024    import org.apache.camel.RouteNode;
025    import org.apache.camel.model.ProcessorDefinition;
026    import org.apache.camel.spi.TraceableUnitOfWork;
027    import org.apache.camel.util.MessageHelper;
028    
029    /**
030     * Default {@link TraceEventMessage}.
031     */
032    public final class DefaultTraceEventMessage implements Serializable, TraceEventMessage {
033    
034        private Date timestamp;
035        private String fromEndpointUri;
036        private String previousNode;
037        private String toNode;
038        private String exchangeId;
039        private String shortExchangeId;
040        private String exchangePattern;
041        private String properties;
042        private String headers;
043        private String body;
044        private String bodyType;
045        private String outHeaders;
046        private String outBody;
047        private String outBodyType;
048        private String causedByException;
049    
050        /**
051         * Creates a {@link DefaultTraceEventMessage} based on the given node it was traced while processing
052         * the current {@link Exchange}
053         *
054         * @param toNode the node where this trace is intercepted
055         * @param exchange the current {@link Exchange}
056         */
057        public DefaultTraceEventMessage(final Date timestamp, final ProcessorDefinition toNode, final Exchange exchange) {
058            Message in = exchange.getIn();
059    
060            // need to use defensive copies to avoid Exchange altering after the point of interception
061            this.timestamp = timestamp;
062            this.fromEndpointUri = exchange.getFromEndpoint() != null ? exchange.getFromEndpoint().getEndpointUri() : null;
063            this.previousNode = extractFromNode(exchange);
064            this.toNode = extractToNode(exchange);
065            this.exchangeId = exchange.getExchangeId();
066            this.shortExchangeId = extractShortExchangeId(exchange);
067            this.exchangePattern = exchange.getPattern().toString();
068            this.properties = exchange.getProperties().isEmpty() ? null : exchange.getProperties().toString();
069            this.headers = in.getHeaders().isEmpty() ? null : in.getHeaders().toString();
070            this.body = MessageHelper.extractBodyAsString(in);
071            this.bodyType = MessageHelper.getBodyTypeName(in);
072            if (exchange.hasOut()) {
073                Message out = exchange.getOut();
074                this.outHeaders = out.getHeaders().isEmpty() ? null : out.getHeaders().toString();
075                this.outBody = MessageHelper.extractBodyAsString(out);
076                this.outBodyType = MessageHelper.getBodyTypeName(out);
077            }
078            this.causedByException = exchange.getException() != null ? exchange.getException().toString() : null;
079        }
080    
081        // Implementation
082        //---------------------------------------------------------------
083    
084        private static String extractShortExchangeId(Exchange exchange) {
085            return exchange.getExchangeId().substring(exchange.getExchangeId().indexOf("/") + 1);
086        }
087    
088        private static String extractFromNode(Exchange exchange) {
089            if (exchange.getUnitOfWork() instanceof TraceableUnitOfWork) {
090                TraceableUnitOfWork tuow = (TraceableUnitOfWork) exchange.getUnitOfWork();
091                RouteNode last = tuow.getSecondLastNode();
092                return last != null ? last.getLabel(exchange) : null;
093            }
094            return null;
095        }
096    
097        private static String extractToNode(Exchange exchange) {
098            if (exchange.getUnitOfWork() instanceof TraceableUnitOfWork) {
099                TraceableUnitOfWork tuow = (TraceableUnitOfWork) exchange.getUnitOfWork();
100                RouteNode last = tuow.getLastNode();
101                return last != null ? last.getLabel(exchange) : null;
102            }
103            return null;
104        }
105    
106        // Properties
107        //---------------------------------------------------------------
108    
109        public Date getTimestamp() {
110            return timestamp;
111        }
112    
113        public String getFromEndpointUri() {
114            return fromEndpointUri;
115        }
116    
117        public String getPreviousNode() {
118            return previousNode;
119        }
120    
121        public String getToNode() {
122            return toNode;
123        }
124    
125        public String getExchangeId() {
126            return exchangeId;
127        }
128    
129        public String getShortExchangeId() {
130            return shortExchangeId;
131        }
132    
133        public String getExchangePattern() {
134            return exchangePattern;
135        }
136    
137        public String getProperties() {
138            return properties;
139        }
140    
141        public String getHeaders() {
142            return headers;
143        }
144    
145        public String getBody() {
146            return body;
147        }
148    
149        public String getBodyType() {
150            return bodyType;
151        }
152    
153        public String getOutBody() {
154            return outBody;
155        }
156    
157        public String getOutBodyType() {
158            return outBodyType;
159        }
160    
161        public String getOutHeaders() {
162            return outHeaders;
163        }
164    
165        public void setOutHeaders(String outHeaders) {
166            this.outHeaders = outHeaders;
167        }
168    
169        public String getCausedByException() {
170            return causedByException;
171        }
172    
173        public void setTimestamp(Date timestamp) {
174            this.timestamp = timestamp;
175        }
176    
177        public void setFromEndpointUri(String fromEndpointUri) {
178            this.fromEndpointUri = fromEndpointUri;
179        }
180    
181        public void setPreviousNode(String previousNode) {
182            this.previousNode = previousNode;
183        }
184    
185        public void setToNode(String toNode) {
186            this.toNode = toNode;
187        }
188    
189        public void setExchangeId(String exchangeId) {
190            this.exchangeId = exchangeId;
191        }
192    
193        public void setShortExchangeId(String shortExchangeId) {
194            this.shortExchangeId = shortExchangeId;
195        }
196    
197        public void setExchangePattern(String exchangePattern) {
198            this.exchangePattern = exchangePattern;
199        }
200    
201        public void setProperties(String properties) {
202            this.properties = properties;
203        }
204    
205        public void setHeaders(String headers) {
206            this.headers = headers;
207        }
208    
209        public void setBody(String body) {
210            this.body = body;
211        }
212    
213        public void setBodyType(String bodyType) {
214            this.bodyType = bodyType;
215        }
216    
217        public void setOutBody(String outBody) {
218            this.outBody = outBody;
219        }
220    
221        public void setOutBodyType(String outBodyType) {
222            this.outBodyType = outBodyType;
223        }
224    
225        public void setCausedByException(String causedByException) {
226            this.causedByException = causedByException;
227        }
228    
229        @Override
230        public String toString() {
231            return "TraceEventMessage[" + exchangeId + "] on node: " + toNode;
232        }
233    }