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