You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by "vp340 (via GitHub)" <gi...@apache.org> on 2026/10/05 20:10:16 UTC

[PR] alternative solution CXF-9251 [cxf]

vp340 opened a new pull request, #3541:
URL: https://github.com/apache/cxf/pull/3541

   alternative solution CXF-9251
   
   **_2nd Scenario (log delayed + memory leak) (still present also in 4.0.11)_**
   So I only have time to do a quick test.
   I setup my java client to reset on purpose the connection and print the resulting response in InputStream (incoming response). 
   Here the result:
   <img width="1783" height="269" alt="image" src="https://github.com/user-attachments/assets/bdffa579-2deb-407d-a858-1be74189aafe" />
   
   So I think that those bytes that are in the CachedOutputStream **should** be log because this is proof  we actually sent them. 
   A owner of his server should be able to see what his service produce and sent onto the http socket... for audit purpose!
   Whether the client processes those bytes or discards them afterward is outside our scope, but on the server side, having a complete audit trail I think it'is crucial.
   
   For example (a little bit borderline) a malicious client could call and reset the connection to receive the first bytes of the response and have no response trace on the server side. 
   
   The logging with this solution is:
   
   > 05/10/2026 21:53:09.226 INFO  [http-nio-8080-exec-2] org.apache.cxf.services.ExampleServicePortType_v1.RESP_OUT - RESP_OUT
       Address: http://localhost:8080/example/cxf/ExampleService_v1
       Content-Type: text/xml
       ResponseCode: 200
       ExchangeId: 0357f765-bb44-4680-aaf4-84f8ee4ce68c
       ServiceName: ExampleService_v1
       PortName: ExampleServicePortType_v1
       PortTypeName: ExampleServicePortType_v1
       Headers: {}
       Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.or...etc...
   05/10/2026 21:53:09.230 WARN  [http-nio-8080-exec-2] org.apache.cxf.phase.PhaseInterceptorChain - Interceptor for {http://ws.schema/example/v1}ExampleService_v1#{http://ws.schema/example/v1}pluto has thrown exception, unwinding now
   org.apache.cxf.interceptor.Fault: java.io.IOException: Connection reset by peer
   	at org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor.writeParts(A


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4189016085


##########
rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java:
##########
@@ -71,4 +73,60 @@ public void writeCacheTo(StringBuilder out, String charsetName, long limit) thro
         super.writeCacheTo(out, charsetName, limit);
         skipFlushingFlowThroughStream = false;
     }
+
+
+    /**
+     * We override the write() methods in order to catch some error that would not
+     * close the CachedOutputStream (ex. IOException "Connection reset by peer".
+     * Causing ghost or delayed OUT log and possible memory-leak due to DelayedCachedOutputStreamCleaner
+     */
+    @Override
+    public void write(byte[] b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        try {
+            super.write(b, off, len);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void close() throws IOException {

Review Comment:
   Could we please do this in postClose()  (as in https://github.com/apache/cxf/pull/3536, it is guaranteed to be called on close):
   
   ```
    @Override
       protected void postClose() throws IOException {
           if (closed.compareAndSet(false, true)) {
               getFlowThroughStream().close();
           }
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "vp340 (via GitHub)" <gi...@apache.org>.
vp340 commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6005873514

   > > > @reta if U want to move here the test that U did in the other PR feel free to commit. If U want I can try add more tests too.
   > > 
   > > 
   > > Yes, I will push tests only, thanks @vp340
   > 
   > Meanwhile I also add two new test cases to replicate the issue:
   > 
   > * shouldNotLogTwice (even if close() method is called multiple times)
   > * shouldLogEvenIfIOException (even if an IOException is thrown and there is only a partial write on the http socket)
   
   Also assert the memory leak gone in the two tests. 
   See U tomorrow.
   Thanks again @reta 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta merged PR #3541:
URL: https://github.com/apache/cxf/pull/3541


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "vp340 (via GitHub)" <gi...@apache.org>.
vp340 commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4189299157


##########
rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java:
##########
@@ -71,4 +73,60 @@ public void writeCacheTo(StringBuilder out, String charsetName, long limit) thro
         super.writeCacheTo(out, charsetName, limit);
         skipFlushingFlowThroughStream = false;
     }
+
+
+    /**
+     * We override the write() methods in order to catch some error that would not
+     * close the CachedOutputStream (ex. IOException "Connection reset by peer".
+     * Causing ghost or delayed OUT log and possible memory-leak due to DelayedCachedOutputStreamCleaner
+     */
+    @Override
+    public void write(byte[] b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        try {
+            super.write(b, off, len);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void close() throws IOException {

Review Comment:
   I put the if (closed.compareAndSet(false, true)) in the close() method in order to close only once the Cos (and so log only once even if someone call again close() ).
   
   You want to add that control to prevent also the closure of getFlowThroughStream() (in that case we could do only if (!closed.get()) ) ...or U want to put there instead the transition of closed from false -> true ? In this case can U explain me why (out of curiosity :)  )



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "vp340 (via GitHub)" <gi...@apache.org>.
vp340 commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4190317583


##########
rt/features/logging/src/test/java/org/apache/cxf/ext/logging/LoggingOutInterceptorTest.java:
##########
@@ -0,0 +1,324 @@
+/**
+ * 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.cxf.ext.logging;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.event.LogEvent;
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.io.DelayedCachedOutputStreamCleaner;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertThrows;
+
+public class LoggingOutInterceptorTest {
+    private Bus bus;
+    private DelayedCachedOutputStreamCleaner cleaner;
+    private TestEventSender sender;
+    private LoggingOutInterceptor interceptor; 
+    private Message message;
+    
+    @Before
+    public void setUp() {
+        bus = BusFactory.getDefaultBus(true);
+        cleaner = bus.getExtension(DelayedCachedOutputStreamCleaner.class);
+        sender = new TestEventSender();
+        interceptor = new LoggingOutInterceptor(sender);
+        message = new MessageImpl();
+        message.setExchange(new ExchangeImpl());
+    }
+    
+    @After
+    public void tearDown() {
+        assertThat(cleaner.size(), equalTo(0));
+    }
+
+    @Test
+    public void shouldLogMultipartPayload() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadOnClean() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        os.close();
+
+        // We did not close the cached stream, should be subject of cleanup
+        assertThat(cleaner.size(), equalTo(1));
+
+        cleaner.forceClean();
+        assertThat(cleaner.size(), equalTo(0));
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadCachedOutputStream() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final CachedOutputStream os = new CachedOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnClose() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void close() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.close());
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnFlush() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void flush() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.flush());
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadClosedTwice() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+
+        cached.close();
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnWrite() throws IOException {

Review Comment:
   all good to meπŸ‘ 



##########
rt/features/logging/src/test/java/org/apache/cxf/ext/logging/LoggingOutInterceptorTest.java:
##########
@@ -0,0 +1,324 @@
+/**
+ * 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.cxf.ext.logging;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.event.LogEvent;
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.io.DelayedCachedOutputStreamCleaner;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertThrows;
+
+public class LoggingOutInterceptorTest {
+    private Bus bus;
+    private DelayedCachedOutputStreamCleaner cleaner;
+    private TestEventSender sender;
+    private LoggingOutInterceptor interceptor; 
+    private Message message;
+    
+    @Before
+    public void setUp() {
+        bus = BusFactory.getDefaultBus(true);
+        cleaner = bus.getExtension(DelayedCachedOutputStreamCleaner.class);
+        sender = new TestEventSender();
+        interceptor = new LoggingOutInterceptor(sender);
+        message = new MessageImpl();
+        message.setExchange(new ExchangeImpl());
+    }
+    
+    @After
+    public void tearDown() {
+        assertThat(cleaner.size(), equalTo(0));
+    }
+
+    @Test
+    public void shouldLogMultipartPayload() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadOnClean() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        os.close();
+
+        // We did not close the cached stream, should be subject of cleanup
+        assertThat(cleaner.size(), equalTo(1));
+
+        cleaner.forceClean();
+        assertThat(cleaner.size(), equalTo(0));
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadCachedOutputStream() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final CachedOutputStream os = new CachedOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnClose() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void close() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.close());
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnFlush() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void flush() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.flush());
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadClosedTwice() throws IOException {

Review Comment:
   yes cached.close(); if it wasn't for the atomic boolean closed would produce a second log. But closed is set to true in cached.write(...) when exception thrown... ok for me πŸ‘ 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4190167997


##########
rt/features/logging/src/test/java/org/apache/cxf/ext/logging/LoggingOutInterceptorTest.java:
##########
@@ -0,0 +1,324 @@
+/**
+ * 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.cxf.ext.logging;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.event.LogEvent;
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.io.DelayedCachedOutputStreamCleaner;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertThrows;
+
+public class LoggingOutInterceptorTest {
+    private Bus bus;
+    private DelayedCachedOutputStreamCleaner cleaner;
+    private TestEventSender sender;
+    private LoggingOutInterceptor interceptor; 
+    private Message message;
+    
+    @Before
+    public void setUp() {
+        bus = BusFactory.getDefaultBus(true);
+        cleaner = bus.getExtension(DelayedCachedOutputStreamCleaner.class);
+        sender = new TestEventSender();
+        interceptor = new LoggingOutInterceptor(sender);
+        message = new MessageImpl();
+        message.setExchange(new ExchangeImpl());
+    }
+    
+    @After
+    public void tearDown() {
+        assertThat(cleaner.size(), equalTo(0));
+    }
+
+    @Test
+    public void shouldLogMultipartPayload() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadOnClean() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        os.close();
+
+        // We did not close the cached stream, should be subject of cleanup
+        assertThat(cleaner.size(), equalTo(1));
+
+        cleaner.forceClean();
+        assertThat(cleaner.size(), equalTo(0));
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadCachedOutputStream() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final CachedOutputStream os = new CachedOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnClose() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void close() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.close());
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnFlush() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void flush() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.flush());
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadClosedTwice() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+
+        cached.close();
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnWrite() throws IOException {

Review Comment:
   This covers `shouldLogEvenIfIOException`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6006138854

   > Also assert the memory leak gone in the two tests.
   > See U tomorrow.
   > Thanks again @reta
   
   Thanks to you @vp340 , test cases added, I removed the ones you added for LogOutputStream - we had these covered (attached pointers)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "vp340 (via GitHub)" <gi...@apache.org>.
vp340 commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6004022196

   @reta if U want to move here the test that U did in the other PR feel free to commit. If U want I can try add more tests too.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "vp340 (via GitHub)" <gi...@apache.org>.
vp340 commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6024127706

   Thanks a lot @reta for merging. 
   For me it means a lot. Glad to have helped.
   
   Out of curiosity ... what kind of SDLC strategy do you use in such a big project?
   In the Jira ticket I saw that the fix version for CXF-9251 are listed as 3.6.14 ; 4.2.5 ; 4.1.10  (so I assume 4.0.X is no longer support?) .
   Is there maybe already a date when they will be released?
   
   No pressure at all here... just wanted to ask :) 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "vp340 (via GitHub)" <gi...@apache.org>.
vp340 commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6005329359

   > > @reta if U want to move here the test that U did in the other PR feel free to commit. If U want I can try add more tests too.
   > 
   > Yes, I will push tests only, thanks @vp340
   
   Meanwhile I also add two new test cases to replicate the issue:
   - shouldNotLogTwice (even if close() method is called multiple times)
   - shouldLogEvenIfIOException (even if an IOException is thrown and there is only a partial write on the http socket)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6026441961

   > Thanks a lot @reta for merging.
   > For me it means a lot. Glad to have helped.
   
   Thank you @vp340 
   
   > Out of curiosity ... what kind of SDLC strategy do you use in such a big project?
   
   The 4.2.4 / 4.1.9 / 3.6.13 releases are under vote right now, I would say next releases should be expected in months from now (tentatively / rough estimate), thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4190165768


##########
rt/features/logging/src/test/java/org/apache/cxf/ext/logging/LoggingOutInterceptorTest.java:
##########
@@ -0,0 +1,324 @@
+/**
+ * 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.cxf.ext.logging;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.ext.logging.event.LogEvent;
+import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.io.DelayedCachedOutputStreamCleaner;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.equalToIgnoringCase;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertThrows;
+
+public class LoggingOutInterceptorTest {
+    private Bus bus;
+    private DelayedCachedOutputStreamCleaner cleaner;
+    private TestEventSender sender;
+    private LoggingOutInterceptor interceptor; 
+    private Message message;
+    
+    @Before
+    public void setUp() {
+        bus = BusFactory.getDefaultBus(true);
+        cleaner = bus.getExtension(DelayedCachedOutputStreamCleaner.class);
+        sender = new TestEventSender();
+        interceptor = new LoggingOutInterceptor(sender);
+        message = new MessageImpl();
+        message.setExchange(new ExchangeImpl());
+    }
+    
+    @After
+    public void tearDown() {
+        assertThat(cleaner.size(), equalTo(0));
+    }
+
+    @Test
+    public void shouldLogMultipartPayload() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadOnClean() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        os.close();
+
+        // We did not close the cached stream, should be subject of cleanup
+        assertThat(cleaner.size(), equalTo(1));
+
+        cleaner.forceClean();
+        assertThat(cleaner.size(), equalTo(0));
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadCachedOutputStream() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final CachedOutputStream os = new CachedOutputStream();
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        cached.close();
+        os.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnClose() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void close() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.close());
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadWithExceptionOnFlush() throws IOException {
+        message.put(Message.ENDPOINT_ADDRESS, "http://localhost:9001/");
+        message.put(Message.REQUEST_URI, "/api");
+
+        final StringBuilder buf = content();
+        String ct = "multipart/related; type=\"application/xop+xml\"; "
+                + "boundary=\"----=_Part_0_2180223.1203118300920\"";
+
+        final byte[] bytes = buf.toString().getBytes(StandardCharsets.UTF_8);
+        final OutputStream os = new ByteArrayOutputStream() {
+            public void flush() throws IOException {
+                throw new IOException("Simulated");
+            }
+        };
+        message.setContent(OutputStream.class, os);
+        message.put(Message.CONTENT_TYPE, ct);
+
+        interceptor.setInMemThreshold(1);
+        interceptor.addBinaryContentMediaTypes("application/xop+xml");
+        interceptor.setLogMultipart(true);
+        interceptor.setLogBinary(true);
+        interceptor.handleMessage(message);
+
+        final OutputStream cached = message.getContent(OutputStream.class);
+        cached.write(bytes, 0, bytes.length);
+        assertThrows(IOException.class, () -> cached.flush());
+        cached.close();
+
+        assertThat(sender.getEvents(), hasSize(1));
+        final LogEvent event = sender.getEvents().get(0);
+
+        assertThat(event.getPayload(), equalToIgnoringCase(buf.toString()));
+    }
+    
+    @Test
+    public void shouldLogMultipartPayloadClosedTwice() throws IOException {

Review Comment:
   This covers `shouldNotLogTwice`
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4189780304


##########
rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java:
##########
@@ -71,4 +73,60 @@ public void writeCacheTo(StringBuilder out, String charsetName, long limit) thro
         super.writeCacheTo(out, charsetName, limit);
         skipFlushingFlowThroughStream = false;
     }
+
+
+    /**
+     * We override the write() methods in order to catch some error that would not
+     * close the CachedOutputStream (ex. IOException "Connection reset by peer".
+     * Causing ghost or delayed OUT log and possible memory-leak due to DelayedCachedOutputStreamCleaner
+     */
+    @Override
+    public void write(byte[] b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        try {
+            super.write(b, off, len);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void close() throws IOException {

Review Comment:
   Oh I see, you want to close() to be skipped altogether (not just postClose()), it make sense I think, thank you



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on code in PR #3541:
URL: https://github.com/apache/cxf/pull/3541#discussion_r4189024090


##########
rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingOutputStream.java:
##########
@@ -71,4 +73,60 @@ public void writeCacheTo(StringBuilder out, String charsetName, long limit) thro
         super.writeCacheTo(out, charsetName, limit);
         skipFlushingFlowThroughStream = false;
     }
+
+
+    /**
+     * We override the write() methods in order to catch some error that would not
+     * close the CachedOutputStream (ex. IOException "Connection reset by peer".
+     * Causing ghost or delayed OUT log and possible memory-leak due to DelayedCachedOutputStreamCleaner
+     */
+    @Override
+    public void write(byte[] b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(byte[] b, int off, int len) throws IOException {
+        try {
+            super.write(b, off, len);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void write(int b) throws IOException {
+        try {
+            super.write(b);
+        } catch (RuntimeException | IOException ex) {
+            handleIoException();
+            throw ex;
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        // Ensure closing only one time
+        if (closed.compareAndSet(false, true)) {
+            super.close();
+        }
+    }
+
+    // See CXF-9251 ... introduced due to IOException "Connection reset by peer"
+    private void handleIoException() {
+        try {
+            // Close this CachedOutputStream
+            // Write method maybe called more than one time... but we already consume the stream the first time
+            // So additional call to this.close would produce nothing
+            this.close();
+        } catch (Exception ignored) {
+            // ignore

Review Comment:
   Let's just not ignored this one but add it to suppressed exception list



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] alternative solution CXF-9251 [cxf]

Posted by "reta (via GitHub)" <gi...@apache.org>.
reta commented on PR #3541:
URL: https://github.com/apache/cxf/pull/3541#issuecomment-6005045080

   > @reta if U want to move here the test that U did in the other PR feel free to commit. If U want I can try add more tests too.
   
   Yes, I will push tests only, thanks @vp340 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@cxf.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org