This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new dced9394719 CAMEL-18789: camel-log - Should show cached stream by
default. Added option to turn this on|off.
dced9394719 is described below
commit dced9394719ddd344a07bfc03b29c071b683a38e
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Dec 2 10:03:15 2022 +0100
CAMEL-18789: camel-log - Should show cached stream by default. Added option
to turn this on|off.
---
.../camel/component/log/LogEndpointConfigurer.java | 6 ++
.../camel/component/log/LogEndpointUriFactory.java | 3 +-
.../org/apache/camel/component/log/log.json | 1 +
.../apache/camel/component/log/LogEndpoint.java | 14 +++-
.../camel/component/log/LogInputStreamTest.java | 32 ++++++++-
.../DefaultExchangeFormatterConfigurer.java | 6 ++
.../org/apache/camel/support/MessageHelper.java | 77 ++++++++++++++++++----
.../processor/DefaultExchangeFormatter.java | 19 +++++-
.../ROOT/pages/camel-3x-upgrade-guide-3_20.adoc | 10 +--
9 files changed, 145 insertions(+), 23 deletions(-)
diff --git
a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
index 993c787e55a..d3bde1d6763 100644
---
a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
+++
b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointConfigurer.java
@@ -49,6 +49,8 @@ public class LogEndpointConfigurer extends
PropertyConfigurerSupport implements
case "showBody": target.setShowBody(property(camelContext,
boolean.class, value)); return true;
case "showbodytype":
case "showBodyType": target.setShowBodyType(property(camelContext,
boolean.class, value)); return true;
+ case "showcachedstreams":
+ case "showCachedStreams":
target.setShowCachedStreams(property(camelContext, boolean.class, value));
return true;
case "showcaughtexception":
case "showCaughtException":
target.setShowCaughtException(property(camelContext, boolean.class, value));
return true;
case "showexception":
@@ -109,6 +111,8 @@ public class LogEndpointConfigurer extends
PropertyConfigurerSupport implements
case "showBody": return boolean.class;
case "showbodytype":
case "showBodyType": return boolean.class;
+ case "showcachedstreams":
+ case "showCachedStreams": return boolean.class;
case "showcaughtexception":
case "showCaughtException": return boolean.class;
case "showexception":
@@ -170,6 +174,8 @@ public class LogEndpointConfigurer extends
PropertyConfigurerSupport implements
case "showBody": return target.isShowBody();
case "showbodytype":
case "showBodyType": return target.isShowBodyType();
+ case "showcachedstreams":
+ case "showCachedStreams": return target.isShowCachedStreams();
case "showcaughtexception":
case "showCaughtException": return target.isShowCaughtException();
case "showexception":
diff --git
a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
index 2da52c77051..30a890a73d2 100644
---
a/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
+++
b/components/camel-log/src/generated/java/org/apache/camel/component/log/LogEndpointUriFactory.java
@@ -21,7 +21,7 @@ public class LogEndpointUriFactory extends
org.apache.camel.support.component.En
private static final Set<String> SECRET_PROPERTY_NAMES;
private static final Set<String> MULTI_VALUE_PREFIXES;
static {
- Set<String> props = new HashSet<>(30);
+ Set<String> props = new HashSet<>(31);
props.add("exchangeFormatter");
props.add("groupActiveOnly");
props.add("groupDelay");
@@ -39,6 +39,7 @@ public class LogEndpointUriFactory extends
org.apache.camel.support.component.En
props.add("showAllProperties");
props.add("showBody");
props.add("showBodyType");
+ props.add("showCachedStreams");
props.add("showCaughtException");
props.add("showException");
props.add("showExchangeId");
diff --git
a/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
b/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
index 2a63759b36b..ba7384688c3 100644
---
a/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
+++
b/components/camel-log/src/generated/resources/org/apache/camel/component/log/log.json
@@ -46,6 +46,7 @@
"showAllProperties": { "kind": "parameter", "displayName": "Show All
Properties", "group": "formatting", "label": "formatting", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Show all of the
exchange properties (both internal and custom)." },
"showBody": { "kind": "parameter", "displayName": "Show Body", "group":
"formatting", "label": "formatting", "required": false, "type": "boolean",
"javaType": "boolean", "deprecated": false, "autowired": false, "secret":
false, "defaultValue": true, "description": "Show the message body." },
"showBodyType": { "kind": "parameter", "displayName": "Show Body Type",
"group": "formatting", "label": "formatting", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "description": "Show the body Java
type." },
+ "showCachedStreams": { "kind": "parameter", "displayName": "Show Cached
Streams", "group": "formatting", "label": "formatting", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": true, "description": "Whether Camel
should show cached stream bodies or not (org.apache.camel.StreamCache)." },
"showCaughtException": { "kind": "parameter", "displayName": "Show Caught
Exception", "group": "formatting", "label": "formatting", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "If the exchange
has a caught exception, show the exception message (no stack trace). A caught
exception is stored as a property on the exchange (using the key
org.apache.camel.Exchange#EXCEPTION_CAU [...]
"showException": { "kind": "parameter", "displayName": "Show Exception",
"group": "formatting", "label": "formatting", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": false, "description": "If the exchange has an
exception, show the exception message (no stacktrace)" },
"showExchangeId": { "kind": "parameter", "displayName": "Show Exchange
Id", "group": "formatting", "label": "formatting", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": false, "description": "Show the unique
exchange ID." },
diff --git
a/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
b/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
index 7ffb09989cb..da72b7916b6 100644
---
a/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
+++
b/components/camel-log/src/main/java/org/apache/camel/component/log/LogEndpoint.java
@@ -112,6 +112,9 @@ public class LogEndpoint extends ProcessorEndpoint
implements LineNumberAware {
@UriParam(label = "formatting",
description = "If enabled Camel will on Future objects wait for
it to complete to obtain the payload to be logged.")
private boolean showFuture;
+ @UriParam(label = "formatting", defaultValue = "true",
+ description = "Whether Camel should show cached stream bodies or
not (org.apache.camel.StreamCache).")
+ private boolean showCachedStreams = true;
@UriParam(label = "formatting",
description = "Whether Camel should show stream bodies or not
(eg such as java.io.InputStream). Beware if you enable this option then "
+ "you may not be able later to access the message
body as the stream have already been read by this logger. To remedy this you
will have to use Stream Caching.")
@@ -159,7 +162,7 @@ public class LogEndpoint extends ProcessorEndpoint
implements LineNumberAware {
changed |= showExchangeId || showProperties || showAllProperties
|| showHeaders || showException
|| showCaughtException
|| showStackTrace;
- changed |= showAll || multiline || showFuture || showStreams ||
showFiles;
+ changed |= showAll || multiline || showFuture ||
!showCachedStreams || showStreams || showFiles;
if (changed) {
DefaultExchangeFormatter def = new DefaultExchangeFormatter();
@@ -177,6 +180,7 @@ public class LogEndpoint extends ProcessorEndpoint
implements LineNumberAware {
def.setShowProperties(showProperties);
def.setShowAllProperties(showAllProperties);
def.setShowStackTrace(showStackTrace);
+ def.setShowCachedStreams(showCachedStreams);
def.setShowStreams(showStreams);
def.setMaxChars(maxChars);
def.setMultiline(multiline);
@@ -549,6 +553,14 @@ public class LogEndpoint extends ProcessorEndpoint
implements LineNumberAware {
this.showFuture = showFuture;
}
+ public boolean isShowCachedStreams() {
+ return showCachedStreams;
+ }
+
+ public void setShowCachedStreams(boolean showCachedStreams) {
+ this.showCachedStreams = showCachedStreams;
+ }
+
public boolean isShowStreams() {
return showStreams;
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/log/LogInputStreamTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/log/LogInputStreamTest.java
index 7a8aa66ce44..54bbda6ed82 100644
---
a/core/camel-core/src/test/java/org/apache/camel/component/log/LogInputStreamTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/component/log/LogInputStreamTest.java
@@ -73,18 +73,46 @@ public class LogInputStreamTest extends ContextTestSupport {
assertMockEndpointsSatisfied();
}
+ @Test
+ public void testE() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:e");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World");
+
+ InputStream is = new ByteArrayInputStream("Hello World".getBytes());
+ template.sendBody("direct:e", is);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testF() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:f");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World");
+
+ InputStream is = new ByteArrayInputStream("Hello World".getBytes());
+ template.sendBody("direct:f", is);
+
+ assertMockEndpointsSatisfied();
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:a").to("log:a").to("mock:a");
+ from("direct:a").noStreamCaching().to("log:a").to("mock:a");
- from("direct:b").to("log:b?showStreams=true").to("mock:b");
+
from("direct:b").noStreamCaching().to("log:b?showStreams=true").to("mock:b");
from("direct:c").streamCaching().to("log:c").to("mock:c");
from("direct:d").streamCaching().to("log:d?showStreams=true").to("mock:d");
+
+
from("direct:e").streamCaching().to("log:e?showCachedStreams=true").to("mock:e");
+
+
from("direct:f").streamCaching().to("log:f?showCachedStreams=false").to("mock:f");
}
};
}
diff --git
a/core/camel-support/src/generated/java/org/apache/camel/support/processor/DefaultExchangeFormatterConfigurer.java
b/core/camel-support/src/generated/java/org/apache/camel/support/processor/DefaultExchangeFormatterConfigurer.java
index 25d0ffee910..07eee12a5cb 100644
---
a/core/camel-support/src/generated/java/org/apache/camel/support/processor/DefaultExchangeFormatterConfigurer.java
+++
b/core/camel-support/src/generated/java/org/apache/camel/support/processor/DefaultExchangeFormatterConfigurer.java
@@ -35,6 +35,8 @@ public class DefaultExchangeFormatterConfigurer extends
org.apache.camel.support
case "ShowBody": target.setShowBody(property(camelContext,
boolean.class, value)); return true;
case "showbodytype":
case "ShowBodyType": target.setShowBodyType(property(camelContext,
boolean.class, value)); return true;
+ case "showcachedstreams":
+ case "ShowCachedStreams":
target.setShowCachedStreams(property(camelContext, boolean.class, value));
return true;
case "showcaughtexception":
case "ShowCaughtException":
target.setShowCaughtException(property(camelContext, boolean.class, value));
return true;
case "showexception":
@@ -80,6 +82,8 @@ public class DefaultExchangeFormatterConfigurer extends
org.apache.camel.support
case "ShowBody": return boolean.class;
case "showbodytype":
case "ShowBodyType": return boolean.class;
+ case "showcachedstreams":
+ case "ShowCachedStreams": return boolean.class;
case "showcaughtexception":
case "ShowCaughtException": return boolean.class;
case "showexception":
@@ -126,6 +130,8 @@ public class DefaultExchangeFormatterConfigurer extends
org.apache.camel.support
case "ShowBody": return target.isShowBody();
case "showbodytype":
case "ShowBodyType": return target.isShowBodyType();
+ case "showcachedstreams":
+ case "ShowCachedStreams": return target.isShowCachedStreams();
case "showcaughtexception":
case "ShowCaughtException": return target.isShowCaughtException();
case "showexception":
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
index bba3e842fdc..bb512a3559f 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/MessageHelper.java
@@ -270,6 +270,33 @@ public final class MessageHelper {
}
}
+ /**
+ * Extracts the body for logging purpose.
+ * <p/>
+ * Will clip the body if its too big for logging.
+ *
+ * @see
org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
+ * @param message the message
+ * @param prepend a message to prepend (optional)
+ * @param allowCachedStreams whether or not cached streams is allowed
+ * @param allowStreams whether or not streams is allowed
+ * @param allowFiles whether or not files is allowed (currently
not in use)
+ * @param maxChars limit to maximum number of chars. Use 0 for
not limit, and -1 for turning logging
+ * message body off.
+ * @return the logging message
+ */
+ public static String extractBodyForLogging(
+ Message message, String prepend, boolean allowCachedStreams,
boolean allowStreams, boolean allowFiles,
+ int maxChars) {
+ String value
+ = extractValueForLogging(message.getBody(), message,
allowCachedStreams, allowStreams, allowFiles, maxChars);
+ if (prepend != null) {
+ return prepend + value;
+ } else {
+ return value;
+ }
+ }
+
/**
* Extracts the value for logging purpose.
* <p/>
@@ -286,6 +313,27 @@ public final class MessageHelper {
*/
public static String extractValueForLogging(
Object obj, Message message, boolean allowStreams, boolean
allowFiles, int maxChars) {
+ return extractValueForLogging(obj, message, allowStreams,
allowStreams, allowFiles, maxChars);
+
+ }
+
+ /**
+ * Extracts the value for logging purpose.
+ * <p/>
+ * Will clip the value if its too big for logging.
+ *
+ * @see
org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
+ * @param obj the value
+ * @param message the message
+ * @param allowCachedStreams whether or not cached streams is allowed
+ * @param allowStreams whether or not streams is allowed
+ * @param allowFiles whether or not files is allowed (currently
not in use)
+ * @param maxChars limit to maximum number of chars. Use 0 for
not limit, and -1 for turning logging
+ * message body off.
+ * @return the logging message
+ */
+ public static String extractValueForLogging(
+ Object obj, Message message, boolean allowCachedStreams, boolean
allowStreams, boolean allowFiles, int maxChars) {
if (maxChars < 0) {
return "[Body is not logged]";
}
@@ -301,19 +349,22 @@ public final class MessageHelper {
}
if (!allowStreams) {
- if (obj instanceof StreamCache) {
- return "[Body is instance of org.apache.camel.StreamCache]";
- } else if (obj instanceof InputStream) {
- return "[Body is instance of java.io.InputStream]";
- } else if (obj instanceof OutputStream) {
- return "[Body is instance of java.io.OutputStream]";
- } else if (obj instanceof Reader) {
- return "[Body is instance of java.io.Reader]";
- } else if (obj instanceof Writer) {
- return "[Body is instance of java.io.Writer]";
- } else if
(obj.getClass().getName().equals("javax.xml.transform.stax.StAXSource")) {
- // StAX source is streaming based
- return "[Body is instance of javax.xml.transform.Source]";
+ boolean allow = allowCachedStreams && obj instanceof StreamCache;
+ if (!allow) {
+ if (obj instanceof StreamCache) {
+ return "[Body is instance of
org.apache.camel.StreamCache]";
+ } else if (obj instanceof InputStream) {
+ return "[Body is instance of java.io.InputStream]";
+ } else if (obj instanceof OutputStream) {
+ return "[Body is instance of java.io.OutputStream]";
+ } else if (obj instanceof Reader) {
+ return "[Body is instance of java.io.Reader]";
+ } else if (obj instanceof Writer) {
+ return "[Body is instance of java.io.Writer]";
+ } else if
(obj.getClass().getName().equals("javax.xml.transform.stax.StAXSource")) {
+ // StAX source is streaming based
+ return "[Body is instance of javax.xml.transform.Source]";
+ }
}
}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java
b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java
index ceea457b93c..79e0843ee63 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultExchangeFormatter.java
@@ -85,6 +85,9 @@ public class DefaultExchangeFormatter implements
ExchangeFormatter {
@UriParam(label = "formatting",
description = "If enabled Camel will on Future objects wait for
it to complete to obtain the payload to be logged.")
private boolean showFuture;
+ @UriParam(label = "formatting", defaultValue = "true",
+ description = "Whether Camel should show cached stream bodies or
not (org.apache.camel.StreamCache).")
+ private boolean showCachedStreams = true;
@UriParam(label = "formatting",
description = "Whether Camel should show stream bodies or not
(eg such as java.io.InputStream). Beware if you enable this option then "
+ "you may not be able later to access the message
body as the stream have already been read by this logger. To remedy this you
will have to use Stream Caching.")
@@ -415,6 +418,17 @@ public class DefaultExchangeFormatter implements
ExchangeFormatter {
this.showExchangePattern = showExchangePattern;
}
+ public boolean isShowCachedStreams() {
+ return showCachedStreams;
+ }
+
+ /**
+ * Whether Camel should show cached stream bodies or not
(org.apache.camel.StreamCache).
+ */
+ public void setShowCachedStreams(boolean showCachedStreams) {
+ this.showCachedStreams = showCachedStreams;
+ }
+
public boolean isShowStreams() {
return showStreams;
}
@@ -466,12 +480,13 @@ public class DefaultExchangeFormatter implements
ExchangeFormatter {
protected String getBodyAsString(Message message) {
if (message.getBody() instanceof Future) {
if (!isShowFuture()) {
- // just use a to string of the future object
+ // just use to string of the future object
return message.getBody().toString();
}
}
- return MessageHelper.extractBodyForLogging(message, null,
isShowStreams(), isShowFiles(), getMaxChars(message));
+ return MessageHelper.extractBodyForLogging(message, null,
isShowCachedStreams(), isShowStreams(), isShowFiles(),
+ getMaxChars(message));
}
private int getMaxChars(Message message) {
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_20.adoc
b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_20.adoc
index d491c633e93..f67f7c30f08 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_20.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide-3_20.adoc
@@ -27,11 +27,13 @@ from `<groupdId>org.apache.camel</groupdId>` to
`<groupdId>org.apache.camel.mave
Dependencies not intended for end users has been removed, such as all
`-parent` JARs.
-=== camel-jsonpath
+=== camel-log
-There is a new option `unpackArray` that unpacks a single-element Json array,
matched by a Jsonpath, into an object. This option is disabled by default (this
behaviour was enabled by default in previous Camel versions). There is a new
expression `jsonpathUnpack(String text, Class<?> resultType)` that makes use of
this new option.
+The log component now shows cached streams (`org.apache.camel.StreamCache`)
message bodies by default.
+Camel comes with stream caching enabled out-of-the-box, and therefore using
the log component you would
+expect to see the message body. Setting `showCachedStreams=false` to use old
behaviour.
-=== camel-google-pubsub
+=== camel-jsonpath
-The headers used by the Camel Google Pubsub component has been renamed. The
prefix `CamelGooglePubsub.` has been updated to `CamelGooglePubsub` without any
dot. This is related to CAMEL-18773 issue.
+There is a new option `unpackArray` that unpacks a single-element Json array,
matched by a Jsonpath, into an object. This option is disabled by default (this
behaviour was enabled by default in previous Camel versions). There is a new
expression `jsonpathUnpack(String text, Class<?> resultType)` that makes use of
this new option.