This is an automated email from the ASF dual-hosted git repository. cziegeler pushed a commit to branch update-tika-4.2.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-contentdetection.git
commit f0015c7476ed63eb9fe8572d6133e47f6fc20522 Author: Carsten Ziegeler <[email protected]> AuthorDate: Thu Nov 5 16:21:29 2026 +0100 Update content detection to Apache Tika 4.2.0 Adapt detection and MIME service APIs, provision the required OSGi dependencies for standalone integration tests, and update development settings. Co-authored-by: Copilot <[email protected]> --- .vscode/settings.json | 3 + pom.xml | 46 ++++++------- .../internal/ContentAwareMimeTypeServiceImpl.java | 37 +++++++++-- .../ContentAwareMimeTypeServiceImplTest.java | 20 ++++-- .../internal/it/ContentdetectionTestSupport.java | 77 ++++++++++++++++++---- 5 files changed, 131 insertions(+), 52 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e0f15db --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 513d566..8ea697c 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <org.ops4j.pax.exam.version>4.14.0</org.ops4j.pax.exam.version> + <tika.version>4.2.0</tika.version> </properties> <dependencies> @@ -65,6 +66,13 @@ <artifactId>org.osgi.annotation.versioning</artifactId> <scope>provided</scope> </dependency> + <!-- Apache Commons --> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.22.0</version> + <scope>provided</scope> + </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.cm</artifactId> @@ -75,37 +83,18 @@ <artifactId>org.osgi.service.component.annotations</artifactId> <scope>provided</scope> </dependency> + <!-- Apache Tika--> <dependency> - <groupId>biz.aQute.bnd</groupId> - <artifactId>biz.aQute.bndlib</artifactId> - <version>${bnd.version}</version> - <scope>test</scope> - </dependency> - <!-- Apache Commons --> - <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - <version>2.14.0</version> - <scope>test</scope> + <groupId>org.apache.tika</groupId> + <artifactId>tika-core</artifactId> + <version>${tika.version}</version> + <scope>provided</scope> </dependency> <!-- Apache Sling --> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.commons.mime</artifactId> - <version>2.1.8</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.testing.paxexam</artifactId> - <version>4.1.2</version> - <scope>test</scope> - </dependency> - <!-- Apache Tika--> - <dependency> - <groupId>org.apache.tika</groupId> - <artifactId>tika-core</artifactId> - <version>1.28.3</version> + <version>3.0.0</version> <scope>provided</scope> </dependency> <!-- Apache Felix --> @@ -121,6 +110,13 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> + <!-- Apache Tika--> + <dependency> + <groupId>org.apache.tika</groupId> + <artifactId>tika-bundle-standard</artifactId> + <version>${tika.version}</version> + <scope>test</scope> + </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam</artifactId> diff --git a/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java b/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java index d70b765..f924c0d 100644 --- a/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java +++ b/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java @@ -20,6 +20,7 @@ package org.apache.sling.commons.contentdetection.internal; import java.io.IOException; import java.io.InputStream; +import java.util.Map; import org.apache.sling.commons.contentdetection.ContentAwareMimeTypeService; import org.apache.sling.commons.mime.MimeTypeService; @@ -27,8 +28,11 @@ import org.apache.tika.detect.Detector; import org.apache.tika.io.TemporaryResources; import org.apache.tika.io.TikaInputStream; import org.apache.tika.metadata.Metadata; +import org.apache.tika.metadata.TikaCoreProperties; import org.apache.tika.mime.MediaType; +import org.apache.tika.parser.ParseContext; import org.osgi.framework.Constants; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -40,12 +44,17 @@ import org.osgi.service.component.annotations.Reference; }) public class ContentAwareMimeTypeServiceImpl implements ContentAwareMimeTypeService { - @Reference - Detector detector; + private final Detector detector; - @Reference - MimeTypeService mimeTypeService; + private final MimeTypeService mimeTypeService; + @Activate + public ContentAwareMimeTypeServiceImpl(@Reference Detector detector, @Reference MimeTypeService mimeTypeService) { + this.detector = detector; + this.mimeTypeService = mimeTypeService; + } + + @Override public String getMimeType(String filename, InputStream content) throws IOException, IllegalArgumentException { if (content == null) { return mimeTypeService.getMimeType(filename); @@ -55,27 +64,41 @@ public class ContentAwareMimeTypeServiceImpl implements ContentAwareMimeTypeServ } MediaType mediaType; try (TemporaryResources tmp = new TemporaryResources()) { - TikaInputStream stream = TikaInputStream.get(content, tmp); Metadata metadata = new Metadata(); - metadata.set(Metadata.RESOURCE_NAME_KEY, filename); - mediaType = detector.detect(stream, metadata); + metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, filename); + TikaInputStream stream = TikaInputStream.get(content, tmp, metadata); + mediaType = detector.detect(stream, metadata, new ParseContext()); } return mediaType.toString(); } + @Override public String getMimeType(String name) { return mimeTypeService.getMimeType(name); } + @Override public String getExtension(String mimeType) { return mimeTypeService.getExtension(mimeType); } + @Override public void registerMimeType(String mimeType, String... extensions) { mimeTypeService.registerMimeType(mimeType, extensions); } + @Override public void registerMimeType(InputStream mimeTabStream) throws IOException { mimeTypeService.registerMimeType(mimeTabStream); } + + @Override + public Map<String, String> getMimeMap() { + return mimeTypeService.getMimeMap(); + } + + @Override + public Map<String, String> getExtensionMap() { + return mimeTypeService.getExtensionMap(); + } } diff --git a/src/test/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImplTest.java b/src/test/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImplTest.java index 4cd467d..84c7cde 100644 --- a/src/test/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImplTest.java +++ b/src/test/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImplTest.java @@ -21,7 +21,8 @@ package org.apache.sling.commons.contentdetection.internal; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Field; +import java.util.Collections; +import java.util.Map; import org.apache.sling.commons.mime.MimeTypeService; import org.junit.Assert; @@ -30,7 +31,7 @@ import org.junit.Test; public class ContentAwareMimeTypeServiceImplTest { - private ContentAwareMimeTypeServiceImpl contentAwareMimeTypeService = null; + private ContentAwareMimeTypeServiceImpl contentAwareMimeTypeService; private int counterA; private int counterB; @@ -55,14 +56,21 @@ public class ContentAwareMimeTypeServiceImplTest { public void registerMimeType(InputStream mimeTabStream) throws IOException { counterB++; } + + @Override + public Map<String, String> getMimeMap() { + return Collections.emptyMap(); + } + + @Override + public Map<String, String> getExtensionMap() { + return Collections.emptyMap(); + } }; @Before public void setup() throws NoSuchFieldException, IllegalAccessException { - contentAwareMimeTypeService = new ContentAwareMimeTypeServiceImpl(); - final Field field = contentAwareMimeTypeService.getClass().getDeclaredField("mimeTypeService"); - field.setAccessible(true); - field.set(contentAwareMimeTypeService, mimeTypeService); + contentAwareMimeTypeService = new ContentAwareMimeTypeServiceImpl(null, mimeTypeService); } @Test diff --git a/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentdetectionTestSupport.java b/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentdetectionTestSupport.java index 9f907ac..2e96d3d 100644 --- a/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentdetectionTestSupport.java +++ b/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentdetectionTestSupport.java @@ -18,32 +18,81 @@ */ package org.apache.sling.commons.contentdetection.internal.it; -import org.apache.sling.testing.paxexam.TestSupport; +import java.io.File; + import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.options.ModifiableCompositeOption; -import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar; -import static org.ops4j.pax.exam.CoreOptions.composite; +import static org.ops4j.pax.exam.CoreOptions.bundle; import static org.ops4j.pax.exam.CoreOptions.junitBundles; +import static org.ops4j.pax.exam.CoreOptions.mavenBundle; import static org.ops4j.pax.exam.CoreOptions.options; -public abstract class ContentdetectionTestSupport extends TestSupport { +public abstract class ContentdetectionTestSupport { @Configuration public Option[] configuration() { return options( - baseConfiguration(), - slingQuickstart(), + // update pax logging for SLF4J 2 + mavenBundle() + .groupId("org.ops4j.pax.logging") + .artifactId("pax-logging-api") + .version("2.3.0"), + mavenBundle("org.apache.sling", "org.apache.sling.commons.mime").versionAsInProject(), + mavenBundle("commons-io", "commons-io").versionAsInProject(), + // SCR + mavenBundle() + .groupId("org.osgi") + .artifactId("org.osgi.util.function") + .version("1.2.0"), + mavenBundle() + .groupId("org.osgi") + .artifactId("org.osgi.util.promise") + .version("1.3.0"), + mavenBundle() + .groupId("org.osgi") + .artifactId("org.osgi.service.component") + .version("1.5.1"), + mavenBundle() + .groupId("org.apache.felix") + .artifactId("org.apache.felix.metatype") + .version("1.2.4"), + mavenBundle() + .groupId("org.apache.felix") + .artifactId("org.apache.felix.scr") + .version("2.3.0"), + + // Tika 4.x + mavenBundle("org.apache.commons", "commons-secure-xml", "1.1.0"), + mavenBundle("org.commonmark", "commonmark", "0.32.0"), + mavenBundle("org.commonmark", "commonmark-ext-gfm-strikethrough", "0.32.0"), + mavenBundle("org.commonmark", "commonmark-ext-gfm-tables", "0.32.0"), + mavenBundle("com.adobe.xmp", "xmpcore", "6.1.11"), + mavenBundle("com.googlecode.plist", "dd-plist", "1.30"), + mavenBundle("com.fasterxml.jackson.core", "jackson-core", "2.22.4"), + mavenBundle("commons-codec", "commons-codec", "1.23.0"), + mavenBundle("commons-logging", "commons-logging", "1.4.0"), + mavenBundle("org.apache.commons", "commons-collections4", "4.6.0"), + mavenBundle("org.apache.commons", "commons-compress", "1.28.0"), + mavenBundle("org.apache.commons", "commons-csv", "1.14.1"), + mavenBundle("org.apache.commons", "commons-exec", "1.6.0"), + mavenBundle("org.apache.commons", "commons-lang3", "3.21.0"), + mavenBundle("org.apache.commons", "commons-math3", "3.6.1"), + mavenBundle("org.apache.logging.log4j", "log4j-api", "2.26.1"), + mavenBundle("org.apache.pdfbox", "fontbox", "3.0.8"), + mavenBundle("org.apache.pdfbox", "pdfbox", "3.0.8"), + mavenBundle("org.apache.pdfbox", "pdfbox-io", "3.0.8"), + mavenBundle("org.bouncycastle", "bcprov-jdk18on", "1.86"), + mavenBundle("org.bouncycastle", "bcutil-jdk18on", "1.86"), + mavenBundle("org.bouncycastle", "bcpkix-jdk18on", "1.86"), + mavenBundle("org.jsoup", "jsoup", "1.24.1"), + mavenBundle("org.ow2.asm", "asm", "9.11.1"), + mavenBundle("org.tukaani", "xz", "1.12"), + mavenBundle("org.apache.tika", "tika-core").versionAsInProject(), + mavenBundle("org.apache.tika", "tika-bundle-standard").versionAsInProject(), // Sling Content Detection - testBundle("bundle.filename"), + bundle(new File(System.getProperty("bundle.filename")).toURI().toString()), // testing junitBundles()); } - - protected ModifiableCompositeOption slingQuickstart() { - final int httpPort = findFreePort(); - final String workingDirectory = workingDirectory(); - return composite(slingQuickstartOakTar(workingDirectory, httpPort)); - } }
