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 aad9224db0e3 CAMEL-27614: camel-printer - default mimeType, unknown
doc flavors fail, service formatted print data (#30981)
aad9224db0e3 is described below
commit aad9224db0e32a0a1ccf45568f8539cd67b48679
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Nov 27 22:37:12 2026 +0530
CAMEL-27614: camel-printer - default mimeType, unknown doc flavors fail,
service formatted print data (#30981)
1. sendToPrinter=false without mimeType failed every exchange with a
NullPointerException: assignDocFlavor() applied the AUTOSENSE default to
a
local variable only, so getMimeType() stayed null and
PrinterOperations.print() called equalsIgnoreCase on it. The flavor and
mimeType fields now hold their defaults (DocFlavor.BYTE_ARRAY,
AUTOSENSE),
and parseURI() only overwrites them when the option is set.
2. A flavor or mimeType value the component does not know, or a pair that
names no doc flavor (such as the default DocFlavor.BYTE_ARRAY with
TEXT_PLAIN), printed as DocFlavor.BYTE_ARRAY.AUTOSENSE without an error.
The endpoint now fails to be created with an IllegalArgumentException
that
lists the accepted values, as an unknown mediaSize does.
DocFlavorAssigner
returns null instead of AUTOSENSE when the flavor has no doc flavor for
the
mime type. While there: TEXT_HTML_UTF_8 and TEXT_HTML compared the mime
type
with TEXT_HTML_UTF_16BE (a copy and paste slip), so they selected the
text/plain doc flavor; they now select text/html.
3. With mimeType PAGEABLE, PRINTABLE or RENDERABLE_IMAGE the print data was
the body as an InputStream, while these SERVICE_FORMATTED doc flavors
need
a java.awt.print.Pageable, a java.awt.print.Printable or a
java.awt.image.renderable.RenderableImage. The producer now converts the
body to the representation class of the doc flavor and passes it as the
print data. With sendToPrinter=false the body is still written to a file
as bytes, as before.
4. sendToPrinter=false failed each exchange when ./target did not exist in
the working directory. PrinterOperations now creates the directory when
it
is missing. The directory stays ./target (not an option).
5. The flavor and mimeType options get defaultValue DocFlavor.BYTE_ARRAY and
AUTOSENSE, the values the code uses.
Not changed: 6 (the port path parameter is not used to find the printer).
The descriptions pull request for CAMEL-23832 (#30950) already says that the
producer does not use the port; deprecating the path parameter would change
the endpoint syntax for a value that only shows in the service location.
Regenerated lpr.json (component and catalog copy) and the endpoint DSL
javadoc of flavor and mimeType. Upgrade guide entry for 4.24 for points 2
and 3.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/catalog/components/lpr.json | 4 +-
.../org/apache/camel/component/printer/lpr.json | 4 +-
.../camel/component/printer/DocFlavorAssigner.java | 7 +-
.../camel/component/printer/PrintDocument.java | 16 +++
.../component/printer/PrinterConfiguration.java | 50 +++++--
.../camel/component/printer/PrinterOperations.java | 25 +++-
.../camel/component/printer/PrinterProducer.java | 21 ++-
.../printer/PrinterDocFlavorOptionsTest.java | 160 +++++++++++++++++++++
.../PrinterOperationsOutputDirectoryTest.java | 63 ++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 16 +++
.../dsl/PrinterEndpointBuilderFactory.java | 7 +-
11 files changed, 345 insertions(+), 28 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/lpr.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/lpr.json
index 6a509b9027fb..a5e63ce29310 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/lpr.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/lpr.json
@@ -40,10 +40,10 @@
"printername": { "index": 2, "kind": "path", "displayName": "Printername",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The name of the printer, for
example default or sales\/salesprinter, as the print services of this machine
(javax.print.Pri [...]
"copies": { "index": 3, "kind": "parameter", "displayName": "Copies",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The number of copies to print,
sent as the Copies attribute of the print request. The producer fails to start
when the va [...]
"docFlavor": { "index": 4, "kind": "parameter", "displayName": "Doc
Flavor", "group": "producer", "label": "", "required": false, "type": "object",
"javaType": "javax.print.DocFlavor", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The doc flavor as a
javax.print.DocFlavor, for example a reference to a bean. When set, it replaces
the doc flavor t [...]
- "flavor": { "index": 5, "kind": "parameter", "displayName": "Flavor",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The representation class of the
doc flavor, as the name of a nested class of javax.print.DocFlavor:
DocFlavor.BYTE_ARRAY, DocFla [...]
+ "flavor": { "index": 5, "kind": "parameter", "displayName": "Flavor",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "DocFlavor.BYTE_ARRAY", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The representation class of the
doc flavor, as the name of a nested class of javax.print [...]
"mediaSize": { "index": 6, "kind": "parameter", "displayName": "Media
Size", "group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "na-letter", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The paper size, as the name
that javax.print.attribute.standard.MediaSizeName gives it, for [...]
"mediaTray": { "index": 7, "kind": "parameter", "displayName": "Media
Tray", "group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The name of the input tray to
take the paper from, as javax.print.attribute.standard.MediaTray gives it with
spaces repla [...]
- "mimeType": { "index": 8, "kind": "parameter", "displayName": "Mime Type",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The format of the document, as
the name of a doc flavor constant of javax.print.DocFlavor, for example
AUTOSENSE, PDF, POST [...]
+ "mimeType": { "index": 8, "kind": "parameter", "displayName": "Mime Type",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "AUTOSENSE", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The format of the document, as
the name of a doc flavor constant of javax.print.DocFlavor, for [...]
"orientation": { "index": 9, "kind": "parameter", "displayName":
"Orientation", "group": "producer", "label": "", "required": false, "type":
"enum", "javaType": "java.lang.String", "enum": [ "portrait", "landscape",
"reverse-portrait", "reverse-landscape" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "portrait", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The orientatio [...]
"printerPrefix": { "index": 10, "kind": "parameter", "displayName":
"Printer Prefix", "group": "producer", "label": "", "required": false, "type":
"string", "javaType": "java.lang.String", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "A text that the producer puts
before hostname\/printername to form the name of the printer to look up. It is
not [...]
"sendToPrinter": { "index": 11, "kind": "parameter", "displayName": "Send
To Printer", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "Whether to send the print job
to the printer. If false, the producer writes the document to a fil [...]
diff --git
a/components/camel-printer/src/generated/resources/META-INF/org/apache/camel/component/printer/lpr.json
b/components/camel-printer/src/generated/resources/META-INF/org/apache/camel/component/printer/lpr.json
index 6a509b9027fb..a5e63ce29310 100644
---
a/components/camel-printer/src/generated/resources/META-INF/org/apache/camel/component/printer/lpr.json
+++
b/components/camel-printer/src/generated/resources/META-INF/org/apache/camel/component/printer/lpr.json
@@ -40,10 +40,10 @@
"printername": { "index": 2, "kind": "path", "displayName": "Printername",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The name of the printer, for
example default or sales\/salesprinter, as the print services of this machine
(javax.print.Pri [...]
"copies": { "index": 3, "kind": "parameter", "displayName": "Copies",
"group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The number of copies to print,
sent as the Copies attribute of the print request. The producer fails to start
when the va [...]
"docFlavor": { "index": 4, "kind": "parameter", "displayName": "Doc
Flavor", "group": "producer", "label": "", "required": false, "type": "object",
"javaType": "javax.print.DocFlavor", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The doc flavor as a
javax.print.DocFlavor, for example a reference to a bean. When set, it replaces
the doc flavor t [...]
- "flavor": { "index": 5, "kind": "parameter", "displayName": "Flavor",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The representation class of the
doc flavor, as the name of a nested class of javax.print.DocFlavor:
DocFlavor.BYTE_ARRAY, DocFla [...]
+ "flavor": { "index": 5, "kind": "parameter", "displayName": "Flavor",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "DocFlavor.BYTE_ARRAY", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The representation class of the
doc flavor, as the name of a nested class of javax.print [...]
"mediaSize": { "index": 6, "kind": "parameter", "displayName": "Media
Size", "group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "na-letter", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The paper size, as the name
that javax.print.attribute.standard.MediaSizeName gives it, for [...]
"mediaTray": { "index": 7, "kind": "parameter", "displayName": "Media
Tray", "group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The name of the input tray to
take the paper from, as javax.print.attribute.standard.MediaTray gives it with
spaces repla [...]
- "mimeType": { "index": 8, "kind": "parameter", "displayName": "Mime Type",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The format of the document, as
the name of a doc flavor constant of javax.print.DocFlavor, for example
AUTOSENSE, PDF, POST [...]
+ "mimeType": { "index": 8, "kind": "parameter", "displayName": "Mime Type",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "AUTOSENSE", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The format of the document, as
the name of a doc flavor constant of javax.print.DocFlavor, for [...]
"orientation": { "index": 9, "kind": "parameter", "displayName":
"Orientation", "group": "producer", "label": "", "required": false, "type":
"enum", "javaType": "java.lang.String", "enum": [ "portrait", "landscape",
"reverse-portrait", "reverse-landscape" ], "deprecated": false, "autowired":
false, "secret": false, "defaultValue": "portrait", "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "The orientatio [...]
"printerPrefix": { "index": 10, "kind": "parameter", "displayName":
"Printer Prefix", "group": "producer", "label": "", "required": false, "type":
"string", "javaType": "java.lang.String", "deprecated": false, "autowired":
false, "secret": false, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "A text that the producer puts
before hostname\/printername to form the name of the printer to look up. It is
not [...]
"sendToPrinter": { "index": 11, "kind": "parameter", "displayName": "Send
To Printer", "group": "producer", "label": "", "required": false, "type":
"boolean", "javaType": "boolean", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": true, "configurationClass":
"org.apache.camel.component.printer.PrinterConfiguration",
"configurationField": "config", "description": "Whether to send the print job
to the printer. If false, the producer writes the document to a fil [...]
diff --git
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/DocFlavorAssigner.java
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/DocFlavorAssigner.java
index b72354425404..8b82eff71087 100644
---
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/DocFlavorAssigner.java
+++
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/DocFlavorAssigner.java
@@ -19,7 +19,8 @@ package org.apache.camel.component.printer;
import javax.print.DocFlavor;
public class DocFlavorAssigner {
- private DocFlavor d = DocFlavor.BYTE_ARRAY.AUTOSENSE;
+ // null when the flavor has no doc flavor for the mime type
+ private DocFlavor d;
public DocFlavor forMimeTypeAUTOSENSE(String flavor) {
if (flavor.equalsIgnoreCase("DocFlavor.BYTE_ARRAY")) {
@@ -204,7 +205,7 @@ public class DocFlavorAssigner {
}
public DocFlavor forMimeTypeUTF8(String flavor, String mimeType) {
- if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_16BE")) {
+ if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_8")) {
if (flavor.equalsIgnoreCase("DocFlavor.BYTE_ARRAY")) {
d = DocFlavor.BYTE_ARRAY.TEXT_HTML_UTF_8;
} else if (flavor.equalsIgnoreCase("DocFlavor.INPUT_STREAM")) {
@@ -226,7 +227,7 @@ public class DocFlavorAssigner {
}
public DocFlavor forMimeTypeBasic(String flavor, String mimeType) {
- if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_16BE")) {
+ if (mimeType.equalsIgnoreCase("TEXT_HTML")) {
if (flavor.equalsIgnoreCase("DocFlavor.CHAR_ARRAY")) {
d = DocFlavor.CHAR_ARRAY.TEXT_HTML;
} else if (flavor.equalsIgnoreCase("DocFlavor.READER")) {
diff --git
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrintDocument.java
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrintDocument.java
index b959bf10b0ab..e12d32757398 100644
---
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrintDocument.java
+++
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrintDocument.java
@@ -35,11 +35,24 @@ class PrintDocument implements Doc {
private Reader reader;
private byte[] buffer;
+ private Object printData;
+
PrintDocument(InputStream stream, DocFlavor docFlavor) {
this.stream = stream;
this.docFlavor = docFlavor;
}
+ /**
+ * A document whose print data is the given object rather than a stream,
for the SERVICE_FORMATTED doc flavors whose
+ * print data is a java.awt.print.Pageable, a java.awt.print.Printable or a
+ * java.awt.image.renderable.RenderableImage.
+ */
+ static PrintDocument ofPrintData(Object printData, DocFlavor docFlavor) {
+ PrintDocument answer = new PrintDocument(null, docFlavor);
+ answer.printData = printData;
+ return answer;
+ }
+
@Override
public DocFlavor getDocFlavor() {
return docFlavor;
@@ -52,6 +65,9 @@ class PrintDocument implements Doc {
@Override
public Object getPrintData() throws IOException {
+ if (printData != null) {
+ return printData;
+ }
return getStreamForBytes();
}
diff --git
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterConfiguration.java
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterConfiguration.java
index b87ac5d2243f..fc1bd66c1c8d 100644
---
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterConfiguration.java
+++
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterConfiguration.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.printer;
import java.net.URI;
+import java.util.List;
import java.util.Map;
import javax.print.DocFlavor;
@@ -33,6 +34,15 @@ import org.apache.camel.util.URISupport;
@UriParams
public class PrinterConfiguration {
+
+ private static final List<String> FLAVORS = List.of(
+ "DocFlavor.BYTE_ARRAY", "DocFlavor.INPUT_STREAM", "DocFlavor.URL",
"DocFlavor.CHAR_ARRAY", "DocFlavor.READER",
+ "DocFlavor.STRING");
+ private static final String MIME_TYPES = "AUTOSENSE, GIF, JPEG, PDF, PCL,
POSTSCRIPT, TEXT_HTML_HOST, TEXT_HTML_US_ASCII,"
+ + " TEXT_HTML_UTF_16,
TEXT_HTML_UTF_16LE, TEXT_HTML_UTF_16BE, TEXT_HTML_UTF_8,"
+ + " TEXT_PLAIN_HOST,
TEXT_PLAIN_US_ASCII, TEXT_PLAIN_UTF_16, TEXT_PLAIN_UTF_16LE,"
+ + " TEXT_PLAIN_UTF_16BE,
TEXT_PLAIN_UTF_8, TEXT_HTML, TEXT_PLAIN, PAGEABLE,"
+ + " PRINTABLE, RENDERABLE_IMAGE";
private URI uri;
private MediaSizeName mediaSizeName;
private Sides internalSides;
@@ -59,21 +69,23 @@ public class PrinterConfiguration {
description = "The number of copies to print, sent as the Copies
attribute of the print request. The producer "
+ "fails to start when the value is less than 1.")
private int copies = 1;
- @UriParam(description = "The representation class of the doc flavor, as
the name of a nested class of "
+ @UriParam(defaultValue = "DocFlavor.BYTE_ARRAY",
+ description = "The representation class of the doc flavor, as
the name of a nested class of "
+ "javax.print.DocFlavor: DocFlavor.BYTE_ARRAY,
DocFlavor.INPUT_STREAM or DocFlavor.URL, and for "
+ "the mimeType TEXT_HTML or TEXT_PLAIN
DocFlavor.CHAR_ARRAY, DocFlavor.READER or "
+ "DocFlavor.STRING. If not set,
DocFlavor.BYTE_ARRAY is used. Together with mimeType it selects "
- + "the doc flavor, and a combination that the
component does not know selects "
- + "DocFlavor.BYTE_ARRAY.AUTOSENSE.")
- private String flavor;
+ + "the doc flavor, and an unknown value or a
combination that names no doc flavor fails the "
+ + "creation of the endpoint.")
+ private String flavor = "DocFlavor.BYTE_ARRAY";
@UriParam(description = "The doc flavor as a javax.print.DocFlavor, for
example a reference to a bean. When set, it "
+ "replaces the doc flavor that the flavor and
mimeType options select.")
private DocFlavor docFlavor;
- @UriParam(description = "The format of the document, as the name of a doc
flavor constant of javax.print.DocFlavor, for "
+ @UriParam(defaultValue = "AUTOSENSE",
+ description = "The format of the document, as the name of a doc
flavor constant of javax.print.DocFlavor, for "
+ "example AUTOSENSE, PDF, POSTSCRIPT, PCL, GIF,
JPEG, TEXT_PLAIN_UTF_8 or TEXT_HTML. If not set, "
+ "AUTOSENSE is used, which lets the print service
detect the format. With sendToPrinter set to "
+ "false, it also selects the extension of the
written file.")
- private String mimeType;
+ private String mimeType = "AUTOSENSE";
@UriParam(defaultValue = "na-letter",
description = "The paper size, as the name that
javax.print.attribute.standard.MediaSizeName gives it, for "
+ "example na-letter, na-legal, iso-a4 or
japanese-postcard. Case is ignored and _ may be used for "
@@ -124,8 +136,13 @@ public class PrinterConfiguration {
setPrintername(path);
Map<String, Object> printSettings = URISupport.parseParameters(uri);
- setFlavor((String) printSettings.get("flavor"));
- setMimeType((String) printSettings.get("mimeType"));
+ // keep the defaults when not set, as the producer reads the mime type
for the file of sendToPrinter=false
+ if (printSettings.containsKey("flavor")) {
+ setFlavor((String) printSettings.get("flavor"));
+ }
+ if (printSettings.containsKey("mimeType")) {
+ setMimeType((String) printSettings.get("mimeType"));
+ }
setDocFlavor(assignDocFlavor(flavor, mimeType));
setPrinterPrefix((String) printSettings.get("printerPrefix"));
@@ -159,7 +176,12 @@ public class PrinterConfiguration {
flavor = "DocFlavor.BYTE_ARRAY";
}
- DocFlavor d = DocFlavor.BYTE_ARRAY.AUTOSENSE;
+ if (FLAVORS.stream().noneMatch(flavor::equalsIgnoreCase)) {
+ throw new IllegalArgumentException(
+ "Unknown flavor: " + flavor + ". Use one of " +
String.join(", ", FLAVORS) + ".");
+ }
+
+ DocFlavor d;
DocFlavorAssigner docFlavorAssigner = new DocFlavorAssigner();
if (mimeType.equalsIgnoreCase("AUTOSENSE")) {
d = docFlavorAssigner.forMimeTypeAUTOSENSE(flavor);
@@ -207,6 +229,16 @@ public class PrinterConfiguration {
d = docFlavorAssigner.forMimeTypePRINTABLE();
} else if (mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
d = docFlavorAssigner.forMimeTypeRENDERABLEIMAGE();
+ } else {
+ throw new IllegalArgumentException("Unknown mimeType: " + mimeType
+ ". Use one of " + MIME_TYPES + ".");
+ }
+ if (d == null) {
+ // fail rather than printing with a doc flavor the user did not
ask for
+ throw new IllegalArgumentException(
+ "The flavor " + flavor + " has no doc flavor for the
mimeType " + mimeType
+ + ". TEXT_HTML and TEXT_PLAIN
need DocFlavor.CHAR_ARRAY, DocFlavor.READER or"
+ + " DocFlavor.STRING, and the
other mime types DocFlavor.BYTE_ARRAY,"
+ + " DocFlavor.INPUT_STREAM or
DocFlavor.URL.");
}
return d;
diff --git
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
index d672f9c7bdc2..55122d4f53d3 100644
---
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
+++
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterOperations.java
@@ -45,6 +45,7 @@ public class PrinterOperations implements
PrinterOperationsInterface {
private DocFlavor flavor;
private PrintRequestAttributeSet printRequestAttributeSet;
private Doc doc;
+ private File outputDirectory = new File("./target");
public PrinterOperations() throws PrintException {
printService = PrintServiceLookup.lookupDefaultPrintService();
@@ -74,13 +75,17 @@ public class PrinterOperations implements
PrinterOperationsInterface {
File file;
if (mimeType.equalsIgnoreCase("GIF") ||
mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
- file = new File("./target/PrintOutput_" + UUID.randomUUID() +
".gif");
+ file = new File(outputDirectory, "PrintOutput_" +
UUID.randomUUID() + ".gif");
} else if (mimeType.equalsIgnoreCase("JPEG")) {
- file = new File("./target/PrintOutput_" + UUID.randomUUID() +
".jpeg");
+ file = new File(outputDirectory, "PrintOutput_" +
UUID.randomUUID() + ".jpeg");
} else if (mimeType.equalsIgnoreCase("PDF")) {
- file = new File("./target/PrintOutput_" + UUID.randomUUID() +
".pdf");
+ file = new File(outputDirectory, "PrintOutput_" +
UUID.randomUUID() + ".pdf");
} else {
- file = new File("./target/PrintOutput_" + UUID.randomUUID() +
".txt");
+ file = new File(outputDirectory, "PrintOutput_" +
UUID.randomUUID() + ".txt");
+ }
+ // create the directory when missing, as each exchange failed in a
working directory without it
+ if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs() &&
!outputDirectory.isDirectory()) {
+ throw new PrintException("Cannot create the directory of the
target file " + file.getAbsolutePath());
}
LOG.debug("Writing print job to file: {}", file.getAbsolutePath());
@@ -131,6 +136,18 @@ public class PrinterOperations implements
PrinterOperationsInterface {
this.printRequestAttributeSet = printRequestAttributeSet;
}
+ File getOutputDirectory() {
+ return outputDirectory;
+ }
+
+ /**
+ * The directory that sendToPrinter=false writes the documents to,
./target of the working directory unless a test
+ * changes it.
+ */
+ void setOutputDirectory(File outputDirectory) {
+ this.outputDirectory = outputDirectory;
+ }
+
public Doc getDoc() {
return doc;
}
diff --git
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
index ddf1f40aeb91..0e75468ea736 100644
---
a/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
+++
b/components/camel-printer/src/main/java/org/apache/camel/component/printer/PrinterProducer.java
@@ -52,22 +52,31 @@ public class PrinterProducer extends DefaultProducer {
@Override
public void process(Exchange exchange) throws Exception {
- // a body that is not text, bytes, a stream or a file fails, rather
than printing a job with no data
- InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
+ DocFlavor flavor = printerOperations.getFlavor();
+ PrintDocument printDoc;
+ if (config.isSendToPrinter() && flavor instanceof
DocFlavor.SERVICE_FORMATTED) {
+ // the print data of these doc flavors is the Pageable, Printable
or RenderableImage itself, not a stream
+ Class<?> type = getEndpoint().getCamelContext().getClassResolver()
+
.resolveMandatoryClass(flavor.getRepresentationClassName());
+ printDoc =
PrintDocument.ofPrintData(exchange.getIn().getMandatoryBody(type), flavor);
+ } else {
+ // a body that is not text, bytes, a stream or a file fails,
rather than printing a job with no data
+ InputStream is =
exchange.getIn().getMandatoryBody(InputStream.class);
+ printDoc = new PrintDocument(is, flavor);
+ }
String jobName = exchange.getIn().getHeader(PrinterEndpoint.JOB_NAME,
"Camel: lpr", String.class);
- print(is, jobName);
+ print(printDoc, jobName);
}
- private void print(InputStream body, String jobName) throws PrintException
{
+ private void print(PrintDocument printDoc, String jobName) throws
PrintException {
PrintService service = printerOperations.getPrintService();
- DocFlavor flavor = printerOperations.getFlavor();
+ DocFlavor flavor = printDoc.getDocFlavor();
// fail, as DocPrintJob.print does, rather than skipping the job
without an error
if (!service.isDocFlavorSupported(flavor)) {
throw new PrintException(
"Print service " + service.getName() + " does not support
the doc flavor " + flavor
+ ". Set the flavor and mimeType options
to a doc flavor this print service supports.");
}
- PrintDocument printDoc = new PrintDocument(body, flavor);
printerOperations.print(printDoc, config.isSendToPrinter(),
config.getMimeType(), jobName);
}
diff --git
a/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterDocFlavorOptionsTest.java
b/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterDocFlavorOptionsTest.java
new file mode 100644
index 000000000000..21f524aa1cf0
--- /dev/null
+++
b/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterDocFlavorOptionsTest.java
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.printer;
+
+import java.awt.print.Printable;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
+
+import javax.print.Doc;
+import javax.print.DocFlavor;
+import javax.print.DocPrintJob;
+import javax.print.PrintService;
+import javax.print.attribute.PrintRequestAttributeSet;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.ArgumentCaptor;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The defaults of the flavor and mimeType options, the doc flavor they
select, and the print data of the
+ * SERVICE_FORMATTED doc flavors, on a mocked PrintService (no print service
lookup, so nothing reaches a real printer).
+ */
+class PrinterDocFlavorOptionsTest {
+
+ private CamelContext context;
+ private PrintService printService;
+ private DocPrintJob printJob;
+ private PrinterProducer producer;
+
+ @BeforeEach
+ void setUp() {
+ context = new DefaultCamelContext();
+ context.start();
+
+ printService = mock(PrintService.class);
+ when(printService.getName()).thenReturn("MockedPrinter");
+
when(printService.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
+ printJob = mock(DocPrintJob.class);
+ when(printService.createPrintJob()).thenReturn(printJob);
+ }
+
+ @AfterEach
+ void tearDown() {
+ if (producer != null) {
+ producer.stop();
+ }
+ context.stop();
+ }
+
+ private PrinterEndpoint startProducer(String uri) {
+ PrinterEndpoint endpoint = context.getEndpoint(uri,
PrinterEndpoint.class);
+ producer = new PrinterProducer(endpoint, endpoint.getConfig());
+ producer.setPrintService(printService);
+ producer.start();
+ return endpoint;
+ }
+
+ private Exchange exchange(Object body) {
+ Exchange exchange = new DefaultExchange(context);
+ exchange.getIn().setBody(body);
+ return exchange;
+ }
+
+ @Test
+ void sendToPrinterFalseWithoutMimeTypeWritesTheDocument() throws Exception
{
+ PrinterEndpoint endpoint =
startProducer("lpr://localhost/MockedPrinter?sendToPrinter=false");
+
+ // failed with a NullPointerException, as the mime type of the file
name was null
+ producer.process(exchange("Hello
Printer".getBytes(StandardCharsets.UTF_8)));
+
+ assertEquals("AUTOSENSE", endpoint.getConfig().getMimeType());
+ assertEquals("DocFlavor.BYTE_ARRAY", endpoint.getConfig().getFlavor());
+ assertEquals(DocFlavor.BYTE_ARRAY.AUTOSENSE,
endpoint.getConfig().getDocFlavor());
+ }
+
+ static Stream<Arguments> unknownOptions() {
+ return Stream.of(
+ Arguments.of("mimeType=PDFF", "Unknown mimeType: PDFF"),
+ Arguments.of("flavor=DocFlavor.BYTES&mimeType=PDF", "Unknown
flavor: DocFlavor.BYTES"),
+ Arguments.of("mimeType=TEXT_PLAIN",
+ "The flavor DocFlavor.BYTE_ARRAY has no doc flavor for
the mimeType TEXT_PLAIN"),
+ Arguments.of("flavor=DocFlavor.STRING&mimeType=PDF",
+ "The flavor DocFlavor.STRING has no doc flavor for the
mimeType PDF"));
+ }
+
+ @ParameterizedTest
+ @MethodSource("unknownOptions")
+ void flavorAndMimeTypeWithoutDocFlavorFailTheEndpoint(String options,
String message) {
+ // printed as DocFlavor.BYTE_ARRAY.AUTOSENSE without an error
+ ResolveEndpointFailedException e =
assertThrows(ResolveEndpointFailedException.class,
+ () -> context.getEndpoint("lpr://localhost/MockedPrinter?" +
options));
+
+ IllegalArgumentException cause =
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertTrue(cause.getMessage().startsWith(message), cause.getMessage());
+ }
+
+ static Stream<Arguments> htmlMimeTypes() {
+ return Stream.of(
+ Arguments.of("mimeType=TEXT_HTML_UTF_8",
DocFlavor.BYTE_ARRAY.TEXT_HTML_UTF_8),
+ Arguments.of("flavor=DocFlavor.STRING&mimeType=TEXT_HTML",
DocFlavor.STRING.TEXT_HTML),
+ Arguments.of("mimeType=TEXT_PLAIN_UTF_8",
DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_8),
+ Arguments.of("flavor=DocFlavor.STRING&mimeType=TEXT_PLAIN",
DocFlavor.STRING.TEXT_PLAIN));
+ }
+
+ @ParameterizedTest
+ @MethodSource("htmlMimeTypes")
+ void htmlMimeTypeSelectsTheHtmlDocFlavor(String options, DocFlavor
expected) {
+ // TEXT_HTML_UTF_8 and TEXT_HTML selected the text/plain doc flavor
+ PrinterEndpoint endpoint =
context.getEndpoint("lpr://localhost/MockedPrinter?" + options,
PrinterEndpoint.class);
+
+ assertEquals(expected, endpoint.getConfig().getDocFlavor());
+ }
+
+ @Test
+ void printableBodyIsThePrintDataOfThePrintableDocFlavor() throws Exception
{
+ startProducer("lpr://localhost/MockedPrinter?mimeType=PRINTABLE");
+ Printable printable = (graphics, pageFormat, pageIndex) ->
Printable.NO_SUCH_PAGE;
+
+ // failed with an InvalidPayloadException, as the body was read as an
InputStream
+ producer.process(exchange(printable));
+
+ ArgumentCaptor<Doc> doc = ArgumentCaptor.forClass(Doc.class);
+ verify(printJob).print(doc.capture(),
any(PrintRequestAttributeSet.class));
+ assertEquals(DocFlavor.SERVICE_FORMATTED.PRINTABLE,
doc.getValue().getDocFlavor());
+ assertSame(printable, doc.getValue().getPrintData());
+ }
+}
diff --git
a/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterOperationsOutputDirectoryTest.java
b/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterOperationsOutputDirectoryTest.java
new file mode 100644
index 000000000000..767b8e89bdff
--- /dev/null
+++
b/components/camel-printer/src/test/java/org/apache/camel/component/printer/PrinterOperationsOutputDirectoryTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.printer;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import javax.print.DocFlavor;
+import javax.print.PrintService;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * sendToPrinter=false writes the document to a file, also when the directory
does not exist yet.
+ */
+class PrinterOperationsOutputDirectoryTest {
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ void missingOutputDirectoryIsCreated() throws Exception {
+ PrintService printService = mock(PrintService.class);
+ when(printService.getName()).thenReturn("MockedPrinter");
+ PrinterOperations operations = new PrinterOperations(
+ printService, DocFlavor.BYTE_ARRAY.AUTOSENSE, new
HashPrintRequestAttributeSet());
+ File missing = tempDir.resolve("work").resolve("target").toFile();
+ operations.setOutputDirectory(missing);
+ byte[] data = "Hello Printer".getBytes(StandardCharsets.UTF_8);
+
+ // failed with a PrintException from the FileOutputStream when
./target did not exist
+ operations.print(new PrintDocument(new ByteArrayInputStream(data),
DocFlavor.BYTE_ARRAY.AUTOSENSE), false,
+ "AUTOSENSE", "job");
+
+ File[] written = missing.listFiles();
+ assertEquals(1, written.length);
+ assertArrayEquals(data, Files.readAllBytes(written[0].toPath()));
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index bd6144c741a0..76747c0b15de 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -12393,6 +12393,22 @@ With `allowUrlBody=true`, a body that does not start
with `version: 1` and is no
with a space or a relative name, now fails with an `InvalidPayloadException`,
as a URL with an unknown scheme already
did. Before, it failed with an `IllegalArgumentException`. A URL that cannot
be fetched keeps its own exception.
+=== camel-printer - an unknown flavor or mimeType fails the endpoint
+
+An `lpr` endpoint now fails to be created when the `flavor` or the `mimeType`
option has a value the component does not
+know, or when the two together name no doc flavor, such as the default
`flavor` `DocFlavor.BYTE_ARRAY` with
+`mimeType=TEXT_PLAIN` (`TEXT_PLAIN` and `TEXT_HTML` need
`DocFlavor.CHAR_ARRAY`, `DocFlavor.READER` or
+`DocFlavor.STRING`). Before, such an endpoint printed with the doc flavor
`DocFlavor.BYTE_ARRAY.AUTOSENSE` without an
+error. Use a `mimeType` with a charset, such as `TEXT_PLAIN_UTF_8`, or remove
the `flavor` and `mimeType` options to
+keep printing as before.
+
+`mimeType=TEXT_HTML_UTF_8` and `mimeType=TEXT_HTML` now select the `text/html`
doc flavor; they selected the
+`text/plain` doc flavor before.
+
+With `mimeType` `PAGEABLE`, `PRINTABLE` or `RENDERABLE_IMAGE`, the producer
now prints the body as a
+`java.awt.print.Pageable`, `java.awt.print.Printable` or
`java.awt.image.renderable.RenderableImage`, as these doc
+flavors need, instead of a stream. With `sendToPrinter=false`, the body is
still written to a file as bytes.
+
=== Kamelets - removed Kamelets
The Kamelet catalog (camel-kamelets 4.24) removes Kamelets that could not work
on Camel 4.23 or 4.24:
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PrinterEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PrinterEndpointBuilderFactory.java
index e136a83c2e90..c9898123bc5c 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PrinterEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PrinterEndpointBuilderFactory.java
@@ -118,11 +118,13 @@ public interface PrinterEndpointBuilderFactory {
* DocFlavor.INPUT_STREAM or DocFlavor.URL, and for the mimeType
* TEXT_HTML or TEXT_PLAIN DocFlavor.CHAR_ARRAY, DocFlavor.READER or
* DocFlavor.STRING. If not set, DocFlavor.BYTE_ARRAY is used. Together
- * with mimeType it selects the doc flavor, and a combination that the
- * component does not know selects DocFlavor.BYTE_ARRAY.AUTOSENSE.
+ * with mimeType it selects the doc flavor, and an unknown value or a
+ * combination that names no doc flavor fails the creation of the
+ * endpoint.
*
* The option is a: <code>java.lang.String</code> type.
*
+ * Default: DocFlavor.BYTE_ARRAY
* Group: producer
*
* @param flavor the value to set
@@ -179,6 +181,7 @@ public interface PrinterEndpointBuilderFactory {
*
* The option is a: <code>java.lang.String</code> type.
*
+ * Default: AUTOSENSE
* Group: producer
*
* @param mimeType the value to set