Repository navigation
CAMEL-26110: camel-as2 - do not pool a connection the partner closed #28362
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
Merged
davsclaus
merged 5 commits into
apache:main
from
seshuthota:CAMEL-26110-as2-connection-close
Oct 27, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f7f55f2
CAMEL-26110: camel-as2 - do not pool a connection the partner closed
seshuthota a10f2ce
Merge branch 'main' into CAMEL-26110-as2-connection-close
seshuthota 871ce73
CAMEL-26110: camel-as2 - read the response body before the connection…
seshuthota 682f259
CAMEL-26110: camel-as2 - parse the buffered response content type len…
seshuthota d2d1ed5
CAMEL-26110: camel-as2 - buffer only a response entity the parser lef…
davsclaus 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
256 changes: 256 additions & 0 deletions
256
...s2-api/src/test/java/org/apache/camel/component/as2/api/AS2ClientConnectionReuseTest.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,256 @@ | ||
| /* | ||
| * 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.as2.api; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.InetAddress; | ||
| import java.net.ServerSocket; | ||
| import java.net.Socket; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.util.Locale; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.camel.util.IOHelper; | ||
| import org.apache.hc.core5.http.ClassicHttpResponse; | ||
| import org.apache.hc.core5.http.ContentType; | ||
| import org.apache.hc.core5.http.HttpException; | ||
| import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
| import org.apache.hc.core5.http.io.entity.StringEntity; | ||
| import org.apache.hc.core5.http.message.BasicClassicHttpRequest; | ||
| import org.apache.hc.core5.http.protocol.HttpCoreContext; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| /** | ||
| * The client keeps a connection in its pool only when the partner's response allows it: a connection the partner closes | ||
| * must not be leased again, or the next send writes on a closed socket (CAMEL-26110). | ||
| */ | ||
| class AS2ClientConnectionReuseTest { | ||
|
|
||
| private static final int SENDS = 3; | ||
|
|
||
| private ExecutorService executor; | ||
| private ServerSocket serverSocket; | ||
| private final AtomicInteger connections = new AtomicInteger(); | ||
|
|
||
| @AfterEach | ||
| void stopPartner() { | ||
| IOHelper.close(serverSocket); | ||
| if (executor != null) { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void connectionClosedByThePartnerIsNotReused() throws Exception { | ||
| startPartner("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 2\r\n\r\nok", true); | ||
|
davsclaus marked this conversation as resolved.
Resolved
|
||
|
|
||
| assertEachSendSucceeds(); | ||
| // the sends follow each other within the pool's 2s stale check: each needs a new connection | ||
| assertEquals(SENDS, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void bodyLargerThanTheInputBufferIsFullyReadWhenThePartnerClosesTheConnection() throws Exception { | ||
| // the body is far beyond the 8 KB input buffer: it must be read before the connection is released and closed | ||
| String body = "0123456789abcdef".repeat(4096); | ||
| startPartner("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: " + body.length() + "\r\n\r\n" + body, true); | ||
|
|
||
| assertEachSendSucceeds(body); | ||
| assertEquals(SENDS, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void chunkedBodyLargerThanTheInputBufferIsFullyReadWhenThePartnerClosesTheConnection() throws Exception { | ||
| String body = "0123456789abcdef".repeat(4096); | ||
| startPartner("HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n" | ||
| + Integer.toHexString(body.length()) + "\r\n" + body + "\r\n0\r\n\r\n", | ||
| true); | ||
|
|
||
| assertEachSendSucceeds(body); | ||
| assertEquals(SENDS, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void largeBodyOfAKeptAliveConnectionIsReadSoTheConnectionIsReused() throws Exception { | ||
| String body = "0123456789abcdef".repeat(4096); | ||
| startPartner("HTTP/1.1 200 OK\r\nContent-Length: " + body.length() + "\r\n\r\n" + body, false); | ||
|
|
||
| assertEachSendSucceeds(body); | ||
| assertEquals(1, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void truncatedBodyFailsTheSendAndTheConnectionIsNotReused() throws Exception { | ||
| // the partner announces 100 bytes, sends 10 and hangs up, without saying Connection: close | ||
| startPartner("HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\n0123456789", true); | ||
| AS2ClientConnection clientConnection = newClientConnection(); | ||
|
|
||
| for (int i = 0; i < 2; i++) { | ||
| assertThrows(IOException.class, () -> clientConnection.send(newRequest(), as2Context())); | ||
| } | ||
| assertEquals(2, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void bodyThatStallsFailsTheSendAndTheConnectionIsNotReused() throws Exception { | ||
| // the partner sends 10 of 100 bytes and keeps the connection open: the read times out with it still open | ||
| startPartner("HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\n0123456789", false); | ||
| AS2ClientConnection clientConnection = newClientConnection(Duration.ofSeconds(1)); | ||
|
|
||
| for (int i = 0; i < 2; i++) { | ||
| assertThrows(IOException.class, () -> clientConnection.send(newRequest(), as2Context())); | ||
| } | ||
| assertEquals(2, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void unknownCharsetInTheResponseFailsTheSendAsAnHttpExceptionAndTheConnectionIsNotReused() throws Exception { | ||
| startPartner("HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=no-such-charset\r\nContent-Length: 2\r\n\r\nok", | ||
| false); | ||
| AS2ClientConnection clientConnection = newClientConnection(); | ||
|
|
||
| for (int i = 0; i < 2; i++) { | ||
| assertThrows(HttpException.class, () -> clientConnection.send(newRequest(), as2Context())); | ||
| } | ||
| assertEquals(2, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void http10ConnectionWithoutKeepAliveIsNotReused() throws Exception { | ||
| startPartner("HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nok", true); | ||
|
|
||
| assertEachSendSucceeds(); | ||
| assertEquals(SENDS, connections.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void keptAliveConnectionIsReused() throws Exception { | ||
| startPartner("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", false); | ||
|
|
||
| assertEachSendSucceeds(); | ||
| assertEquals(1, connections.get()); | ||
| } | ||
|
|
||
| private void assertEachSendSucceeds() throws Exception { | ||
| assertEachSendSucceeds("ok"); | ||
| } | ||
|
|
||
| private void assertEachSendSucceeds(String expectedBody) throws Exception { | ||
| AS2ClientConnection clientConnection = newClientConnection(); | ||
| for (int i = 0; i < SENDS; i++) { | ||
| ClassicHttpResponse response = (ClassicHttpResponse) clientConnection.send(newRequest(), as2Context()); | ||
| assertEquals(200, response.getCode(), "send " + i); | ||
| // read the response like AS2ClientManager does, so a reused connection starts clean | ||
| assertEquals(expectedBody, EntityUtils.toString(response.getEntity()), "send " + i); | ||
| } | ||
| } | ||
|
|
||
| private AS2ClientConnection newClientConnection() throws IOException { | ||
| return newClientConnection(Duration.ofSeconds(5)); | ||
| } | ||
|
|
||
| private AS2ClientConnection newClientConnection(Duration socketTimeout) throws IOException { | ||
| return new AS2ClientConnection( | ||
| "1.1", "Camel AS2 Client Endpoint", "example.org", "localhost", serverSocket.getLocalPort(), | ||
| socketTimeout, Duration.ofSeconds(5), 5, Duration.ofMinutes(15), null, null); | ||
| } | ||
|
|
||
| private static BasicClassicHttpRequest newRequest() { | ||
| BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); | ||
| request.setEntity(new StringEntity("EDI message", ContentType.TEXT_PLAIN)); | ||
| return request; | ||
| } | ||
|
|
||
| private static HttpCoreContext as2Context() { | ||
| HttpCoreContext context = HttpCoreContext.create(); | ||
| context.setAttribute(AS2ClientManager.FROM, "mrAS@example.org"); | ||
| context.setAttribute(AS2ClientManager.AS2_FROM, "878051556"); | ||
| context.setAttribute(AS2ClientManager.AS2_TO, "878051556"); | ||
| context.setAttribute(AS2ClientManager.SUBJECT, "Test Case"); | ||
| return context; | ||
| } | ||
|
|
||
| /** | ||
| * A partner that gives every request the same response, and closes the connection after it when told to. | ||
| */ | ||
| private void startPartner(String response, boolean closeAfterResponse) throws IOException { | ||
| serverSocket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress()); | ||
| executor = Executors.newCachedThreadPool(); | ||
| executor.execute(() -> { | ||
| while (!serverSocket.isClosed()) { | ||
| try { | ||
| Socket socket = serverSocket.accept(); | ||
| executor.execute(() -> answer(socket, response, closeAfterResponse)); | ||
| } catch (IOException e) { | ||
| // closed | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void answer(Socket socket, String response, boolean closeAfterResponse) { | ||
| try (socket) { | ||
| InputStream in = new BufferedInputStream(socket.getInputStream()); | ||
| OutputStream out = socket.getOutputStream(); | ||
| boolean firstRequest = true; | ||
| do { | ||
| long contentLength = -1; | ||
| String line = readLine(in); | ||
| if (line == null) { | ||
| // the client's startup check connects without sending a request | ||
| return; | ||
| } | ||
| if (firstRequest) { | ||
| connections.incrementAndGet(); | ||
| firstRequest = false; | ||
| } | ||
| for (line = readLine(in); line != null && !line.isEmpty(); line = readLine(in)) { | ||
| if (line.toLowerCase(Locale.ROOT).startsWith("content-length:")) { | ||
| contentLength = Long.parseLong(line.substring(line.indexOf(':') + 1).trim()); | ||
| } | ||
| } | ||
| in.skipNBytes(Math.max(contentLength, 0)); | ||
| out.write(response.getBytes(StandardCharsets.ISO_8859_1)); | ||
| out.flush(); | ||
| } while (!closeAfterResponse); | ||
| } catch (IOException e) { | ||
| // connection closed | ||
| } | ||
| } | ||
|
|
||
| private static String readLine(InputStream in) throws IOException { | ||
| ByteArrayOutputStream line = new ByteArrayOutputStream(); | ||
| int b; | ||
| while ((b = in.read()) != -1 && b != '\n') { | ||
| if (b != '\r') { | ||
| line.write(b); | ||
| } | ||
| } | ||
| return b == -1 && line.size() == 0 ? null : line.toString(StandardCharsets.ISO_8859_1); | ||
| } | ||
| } | ||
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.