-
Notifications
You must be signed in to change notification settings - Fork 914
TIKA-4679: Add HTTP/2 support to tika-server via Jetty http2-server #2672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nddipiazza
wants to merge
5
commits into
main
Choose a base branch
from
TIKA-4679-http2-server-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+389
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
184803b
Adding jetty http2 dependency allows cxf to support http2 requests.
elemdisc 6ced8d4
TIKA-4679: Add e2e test module for HTTP/2 tika-server
nddipiazza 1061ef5
TIKA-4679: Fix e2e test - skip when fat-jar absent, use /status healt…
nddipiazza 499443e
TIKA-4679: Fix e2e HTTP/2 test server startup and health-check
nddipiazza 30b9ff3
TIKA-4679: Address Copilot review comments on TikaServerHttp2Test
nddipiazza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,7 @@ | |
|
|
||
| <modules> | ||
| <module>tika-grpc</module> | ||
| <module>tika-server</module> | ||
| </modules> | ||
|
|
||
| <dependencyManagement> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>org.apache.tika</groupId> | ||
| <artifactId>tika-e2e-tests</artifactId> | ||
| <version>${revision}</version> | ||
| <relativePath>../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>tika-e2e-tests-server</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>Apache Tika E2E Tests: REST Server</name> | ||
| <description>End-to-end tests for tika-server-standard, including HTTP/2 support verification</description> | ||
|
|
||
| <properties> | ||
| <!-- Path to the tika-server-standard binary assembly zip built in the same reactor --> | ||
| <tika.server.zip>${project.basedir}/../../tika-server/tika-server-standard/target/tika-server-standard-${revision}-bin.zip</tika.server.zip> | ||
| <!-- Directory where the assembly is unpacked before tests run --> | ||
| <tika.server.home>${project.build.directory}/tika-server-dist</tika.server.home> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.logging.log4j</groupId> | ||
| <artifactId>log4j-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.logging.log4j</groupId> | ||
| <artifactId>log4j-slf4j2-impl</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <configuration> | ||
| <!-- Skip by default; run with -Pe2e --> | ||
| <skipTests>true</skipTests> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.rat</groupId> | ||
| <artifactId>apache-rat-plugin</artifactId> | ||
| <configuration> | ||
| <inputExcludes> | ||
| <inputExclude>**/README*.md</inputExclude> | ||
| <inputExclude>src/test/resources/**</inputExclude> | ||
| </inputExcludes> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>e2e</id> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-dependency-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <id>unpack-tika-server</id> | ||
| <phase>process-test-resources</phase> | ||
| <goals> | ||
| <goal>unpack</goal> | ||
| </goals> | ||
| <configuration> | ||
| <artifactItems> | ||
| <artifactItem> | ||
| <groupId>org.apache.tika</groupId> | ||
| <artifactId>tika-server-standard</artifactId> | ||
| <version>${revision}</version> | ||
| <classifier>bin</classifier> | ||
| <type>zip</type> | ||
| <overWrite>false</overWrite> | ||
| <outputDirectory>${tika.server.home}</outputDirectory> | ||
| </artifactItem> | ||
| </artifactItems> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <configuration> | ||
| <skipTests>false</skipTests> | ||
| <systemPropertyVariables> | ||
| <tika.server.home>${tika.server.home}</tika.server.home> | ||
| </systemPropertyVariables> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </profile> | ||
| </profiles> | ||
| </project> |
217 changes: 217 additions & 0 deletions
217
tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TikaServerHttp2Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /* | ||
| * 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.tika.server.e2e; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.InputStreamReader; | ||
| import java.net.ServerSocket; | ||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assumptions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * End-to-end test verifying that tika-server-standard supports HTTP/2 (h2c cleartext). | ||
| * | ||
| * Starts the real fat-jar, sends a request using Java's HttpClient configured for HTTP/2, | ||
| * and asserts the response was served over HTTP/2. This validates the runtime classpath | ||
| * has the Jetty http2-server jar and CXF negotiates h2c correctly. | ||
| * | ||
| * Run with: mvn test -pl tika-e2e-tests/tika-server -Pe2e | ||
| * | ||
| * Inspired by Lawrence Moorehead's original contribution (elemdisc/tika PR#1, TIKA-4679). | ||
| */ | ||
| @Tag("E2ETest") | ||
| public class TikaServerHttp2Test { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(TikaServerHttp2Test.class); | ||
| private static final long SERVER_STARTUP_TIMEOUT_MS = 90_000; | ||
| /** Health-check polls root (/), which always returns 200 without requiring endpoint config. */ | ||
| private static final String HEALTH_PATH = "/"; | ||
|
|
||
| private Process serverProcess; | ||
| private int port; | ||
| private String endPoint; | ||
|
|
||
| @BeforeEach | ||
| void startServer() throws Exception { | ||
| port = findFreePort(); | ||
| endPoint = "http://localhost:" + port; | ||
|
|
||
| String serverHome = System.getProperty("tika.server.home"); | ||
| if (serverHome == null) { | ||
| // fall back to conventional location relative to this module | ||
| Path repoRoot = Paths.get("").toAbsolutePath(); | ||
| while (repoRoot != null && !repoRoot.resolve("tika-server").toFile().isDirectory()) { | ||
| repoRoot = repoRoot.getParent(); | ||
| } | ||
| if (repoRoot == null) { | ||
| throw new IllegalStateException("Cannot locate tika root. Pass -Dtika.server.home=/path/to/extracted-assembly"); | ||
| } | ||
| serverHome = repoRoot.resolve("tika-e2e-tests/tika-server/target/tika-server-dist").toAbsolutePath().toString(); | ||
| } | ||
|
|
||
| Path serverJar = Paths.get(serverHome, "tika-server.jar"); | ||
| Assumptions.assumeTrue(Files.exists(serverJar), | ||
| "tika-server.jar not found at " + serverJar + "; skipping HTTP/2 e2e test. " + | ||
| "Build with: mvn package -pl tika-server/tika-server-standard && " + | ||
| "mvn test -pl tika-e2e-tests/tika-server -Pe2e"); | ||
|
|
||
| log.info("Starting tika-server from: {}", serverJar); | ||
| ProcessBuilder pb = new ProcessBuilder( | ||
| "java", "-jar", "tika-server.jar", | ||
| "-p", String.valueOf(port), | ||
| "-h", "localhost" | ||
| ); | ||
| pb.directory(Paths.get(serverHome).toFile()); | ||
| pb.redirectErrorStream(true); | ||
| serverProcess = pb.start(); | ||
|
|
||
| // Drain output in background so the process doesn't block | ||
| Thread drainThread = new Thread(() -> { | ||
| try (BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(serverProcess.getInputStream(), UTF_8))) { | ||
| String line; | ||
| while ((line = reader.readLine()) != null) { | ||
| log.info("tika-server: {}", line); | ||
| } | ||
| } catch (Exception e) { | ||
| log.debug("Server output stream closed", e); | ||
| } | ||
| }); | ||
| drainThread.setDaemon(true); | ||
| drainThread.start(); | ||
|
|
||
| awaitServerStartup(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void stopServer() throws Exception { | ||
| if (serverProcess != null && serverProcess.isAlive()) { | ||
| serverProcess.destroy(); | ||
| if (!serverProcess.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)) { | ||
| serverProcess.destroyForcibly(); | ||
| serverProcess.waitFor(30, java.util.concurrent.TimeUnit.SECONDS); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testH2cTikaEndpoint() throws Exception { | ||
| HttpClient httpClient = HttpClient.newBuilder() | ||
| .version(HttpClient.Version.HTTP_2) | ||
| .build(); | ||
| HttpRequest request = HttpRequest.newBuilder() | ||
| .uri(URI.create(endPoint + "/tika")) | ||
| .header("Accept", "text/plain") | ||
| .GET() | ||
| .build(); | ||
|
|
||
| HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString(UTF_8)); | ||
|
|
||
| assertEquals(200, response.statusCode(), "Expected 200 from /tika"); | ||
| assertEquals(HttpClient.Version.HTTP_2, response.version(), | ||
| "Expected HTTP/2 protocol; server may be missing http2-server on classpath"); | ||
| log.info("HTTP/2 h2c verified: {} {}", response.statusCode(), response.version()); | ||
| } | ||
|
|
||
| @Test | ||
| void testH2cParseEndpoint() throws Exception { | ||
| HttpClient httpClient = HttpClient.newBuilder() | ||
| .version(HttpClient.Version.HTTP_2) | ||
| .build(); | ||
|
|
||
| // First: GET / to negotiate h2c upgrade, establishing an HTTP/2 connection | ||
| HttpRequest warmup = HttpRequest.newBuilder() | ||
| .uri(URI.create(endPoint + "/")) | ||
| .GET() | ||
| .build(); | ||
| httpClient.send(warmup, HttpResponse.BodyHandlers.discarding()); | ||
|
|
||
| // Now PUT /tika — the existing HTTP/2 connection is reused | ||
| byte[] body = "Hello, HTTP/2 world!".getBytes(UTF_8); | ||
| HttpRequest request = HttpRequest.newBuilder() | ||
| .uri(URI.create(endPoint + "/tika")) | ||
| .header("Content-Type", "text/plain") | ||
| .PUT(HttpRequest.BodyPublishers.ofByteArray(body)) | ||
| .build(); | ||
|
|
||
| HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString(UTF_8)); | ||
|
|
||
| assertEquals(200, response.statusCode(), "Expected 200 from /tika"); | ||
| assertEquals(HttpClient.Version.HTTP_2, response.version(), | ||
| "Expected HTTP/2 protocol on /tika endpoint"); | ||
| log.info("HTTP/2 parse endpoint verified: {} bytes returned over {}", response.body().length(), response.version()); | ||
| } | ||
|
|
||
| private void awaitServerStartup() throws Exception { | ||
| // Use HTTP/1.1 for the health-check poll so we don't depend on HTTP/2 during startup. | ||
| // Both connectTimeout and request timeout are set to avoid hanging when Jetty has bound | ||
| // the port but CXF has not yet finished initializing (accepts TCP but doesn't respond). | ||
| HttpClient pollClient = HttpClient.newBuilder() | ||
| .version(HttpClient.Version.HTTP_1_1) | ||
| .connectTimeout(Duration.ofSeconds(5)) | ||
| .build(); | ||
|
|
||
| Instant deadline = Instant.now().plusMillis(SERVER_STARTUP_TIMEOUT_MS); | ||
| while (Instant.now().isBefore(deadline)) { | ||
| if (!serverProcess.isAlive()) { | ||
| throw new IllegalStateException( | ||
| "tika-server process exited unexpectedly with code " + serverProcess.exitValue()); | ||
| } | ||
| try { | ||
| HttpRequest pollRequest = HttpRequest.newBuilder() | ||
| .uri(URI.create(endPoint + HEALTH_PATH)) | ||
| .timeout(Duration.ofSeconds(5)) | ||
| .GET() | ||
| .build(); | ||
| HttpResponse<Void> resp = pollClient.send(pollRequest, HttpResponse.BodyHandlers.discarding()); | ||
| if (resp.statusCode() == 200) { | ||
| log.info("tika-server ready on port {}", port); | ||
| return; | ||
| } | ||
| log.debug("Server returned {} on {}; still waiting...", resp.statusCode(), HEALTH_PATH); | ||
| } catch (Exception e) { | ||
| log.debug("Waiting for server on port {}: {}", port, e.getMessage()); | ||
| } | ||
| Thread.sleep(1000); | ||
| } | ||
| throw new IllegalStateException("tika-server did not start within " + SERVER_STARTUP_TIMEOUT_MS + " ms"); | ||
| } | ||
|
|
||
| private static int findFreePort() throws Exception { | ||
| try (ServerSocket s = new ServerSocket(0)) { | ||
| return s.getLocalPort(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.