This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27304-tika-html-no-title in repository https://gitbox.apache.org/repos/asf/camel.git
commit 8c0ed5ccec63ef4383b7d24a28fe724fd6b0f69b Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 16:04:27 2026 +0100 CAMEL-27304: camel-tika - parse to html works for a document without a title on JDK 21+ Tika's ExpandedTitleContentHandler writes a NUL character into an empty title, expecting the serializer to throw. The JDK 21+ serializer keeps it and fails the parse. Write an empty title without the XHTML namespace instead, so the html output method writes <title></title>. A title with text is passed on unchanged, so its output stays the same. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/component/tika/TikaProducer.java | 81 +++++++++++++++++++++- .../apache/camel/component/tika/TikaParseTest.java | 50 +++++++++++++ 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/components/camel-tika/src/main/java/org/apache/camel/component/tika/TikaProducer.java b/components/camel-tika/src/main/java/org/apache/camel/component/tika/TikaProducer.java index 7ccca28fd453..ce7d3b88256c 100644 --- a/components/camel-tika/src/main/java/org/apache/camel/component/tika/TikaProducer.java +++ b/components/camel-tika/src/main/java/org/apache/camel/component/tika/TikaProducer.java @@ -32,8 +32,10 @@ import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; +import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; import org.apache.camel.Exchange; import org.apache.camel.ExchangePropertyKey; @@ -52,7 +54,8 @@ import org.apache.tika.mime.MediaType; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.Parser; import org.apache.tika.sax.BodyContentHandler; -import org.apache.tika.sax.ExpandedTitleContentHandler; +import org.apache.tika.sax.ContentHandlerDecorator; +import org.apache.tika.sax.XHTMLContentHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -189,7 +192,7 @@ public class TikaProducer extends DefaultProducer { result = new BodyContentHandler(new OutputStreamWriter(outputStream, this.encoding)); break; case html: - result = new ExpandedTitleContentHandler(getTransformerHandler(outputStream, "html", true)); + result = new HtmlTitleContentHandler(getTransformerHandler(outputStream, "html", true)); break; default: throw new IllegalArgumentException( @@ -218,6 +221,80 @@ public class TikaProducer extends DefaultProducer { return handler; } + /** + * Writes an empty XHTML {@code title} element without its namespace, so the html output method treats it as an HTML + * element and writes the end tag: {@code <title></title>} and not {@code <title/>}, which browsers do not handle + * (TIKA-725). A title with text is passed on unchanged. + * <p> + * Tika's {@code ExpandedTitleContentHandler} does this by writing one character from an empty array, expecting the + * serializer to fail with an {@code ArrayIndexOutOfBoundsException} after it has closed the start tag. The JDK 21+ + * serializer keeps a NUL character instead, and fails the parse when the element ends. + */ + private static final class HtmlTitleContentHandler extends ContentHandlerDecorator { + + private String titleUri; + private String titleLocalName; + private String titleName; + private Attributes titleAttributes; + + private HtmlTitleContentHandler(ContentHandler handler) { + super(handler); + } + + @Override + public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException { + startPendingTitle(); + if (XHTMLContentHandler.XHTML.equals(uri) && "title".equalsIgnoreCase(localName)) { + // wait for the title text, to know whether the title is empty + titleUri = uri; + titleLocalName = localName; + titleName = name; + titleAttributes = new AttributesImpl(atts); + } else { + super.startElement(uri, localName, name, atts); + } + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + if (length > 0) { + startPendingTitle(); + super.characters(ch, start, length); + } + } + + @Override + public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { + startPendingTitle(); + super.ignorableWhitespace(ch, start, length); + } + + @Override + public void processingInstruction(String target, String data) throws SAXException { + startPendingTitle(); + super.processingInstruction(target, data); + } + + @Override + public void endElement(String uri, String localName, String name) throws SAXException { + if (titleUri != null) { + // an empty title: without the namespace the html output method writes the end tag + super.startElement("", titleLocalName, titleName, titleAttributes); + super.endElement("", titleLocalName, titleName); + titleUri = null; + } else { + super.endElement(uri, localName, name); + } + } + + private void startPendingTitle() throws SAXException { + if (titleUri != null) { + super.startElement(titleUri, titleLocalName, titleName, titleAttributes); + titleUri = null; + } + } + } + private static HeaderFilterStrategy createHeaderFilterStrategy() { DefaultHeaderFilterStrategy strategy = new DefaultHeaderFilterStrategy(); // Match case-insensitively, and cover the fully qualified form as well as the Camel prefix diff --git a/components/camel-tika/src/test/java/org/apache/camel/component/tika/TikaParseTest.java b/components/camel-tika/src/test/java/org/apache/camel/component/tika/TikaParseTest.java index d4cdad6ed7a3..60a12af7ec72 100644 --- a/components/camel-tika/src/test/java/org/apache/camel/component/tika/TikaParseTest.java +++ b/components/camel-tika/src/test/java/org/apache/camel/component/tika/TikaParseTest.java @@ -56,6 +56,14 @@ public class TikaParseTest extends CamelTestSupport { = ("<html><head><title>Test</title></head><body><p>" + HTML_TEXT + "</p></body></html>") .getBytes(StandardCharsets.UTF_8); + // ASCII only, as the html output method writes other characters as entity references + private static final String TEXT = "Hello from Camel"; + private static final String HTML_WITH_TITLE + = "<html><head><title>Test</title></head><body><p>" + TEXT + "</p></body></html>"; + private static final String HTML_WITHOUT_TITLE = "<html><head></head><body><p>" + TEXT + "</p></body></html>"; + private static final String HTML_WITH_EMPTY_TITLE + = "<html><head><title></title></head><body><p>" + TEXT + "</p></body></html>"; + @EndpointInject("mock:result") protected MockEndpoint resultEndpoint; @@ -181,6 +189,47 @@ public class TikaParseTest extends CamelTestSupport { resultEndpoint.assertIsSatisfied(); } + @Test + public void testHtmlParseWithTitle() { + String html = template.requestBody("direct:html", HTML_WITH_TITLE, String.class); + assertThat(html, containsString("<title>Test</title>")); + assertThat(html, containsString(TEXT)); + } + + @Test + public void testHtmlParsePlainText() { + // plain text has no title + String html = template.requestBody("direct:html", TEXT, String.class); + assertEmptyTitle(html); + } + + @Test + public void testHtmlParsePlainTextBytes() { + String html = template.requestBody("direct:html", TEXT.getBytes(StandardCharsets.UTF_8), String.class); + assertEmptyTitle(html); + } + + @Test + public void testHtmlParseWithoutTitle() { + String html = template.requestBody("direct:html", HTML_WITHOUT_TITLE.getBytes(StandardCharsets.UTF_8), String.class); + assertEmptyTitle(html); + } + + @Test + public void testHtmlParseWithEmptyTitle() { + String html = template.requestBody("direct:html", HTML_WITH_EMPTY_TITLE, String.class); + assertEmptyTitle(html); + } + + private static void assertEmptyTitle(String html) { + // TIKA-725: browsers do not handle <title/>, so the empty title must be written with an end tag + assertThat(html, containsString("<title></title>")); + assertThat(html, not(containsString("<title/>"))); + assertThat(html, not(containsString("\0"))); + assertThat(html, not(containsString("�"))); + assertThat(html, containsString(TEXT)); + } + @Test public void testParseBodyNotStream() { Exception e = assertThrows(CamelExecutionException.class, @@ -226,6 +275,7 @@ public class TikaParseTest extends CamelTestSupport { from("direct:start3").to("tika:parse?tikaLoader=#testLoader").to("mock:result"); from("direct:start4").to("tika:parse?tikaParseOutputEncoding=" + StandardCharsets.UTF_16.name()) .to("mock:result"); + from("direct:html").to("tika:parse?tikaParseOutputFormat=html"); from("direct:start5") .to("tika:parse?tikaParseOutputFormat=text&tikaParseOutputEncoding=" + StandardCharsets.UTF_16.name()) .to("mock:result");
