This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e75396f  CAMEL-16197: camel-cxf - Should ensure CXF attachment that 
are cached to temp disk should be closed/deleted when UoW is done so CXF does 
not leak files. (#5326)
e75396f is described below

commit e75396f721955b2abf3be482c850963120f960b3
Author: Manuel <48989438+mash-...@users.noreply.github.com>
AuthorDate: Sat Apr 10 07:57:02 2021 +0200

    CAMEL-16197: camel-cxf - Should ensure CXF attachment that are cached to 
temp disk should be closed/deleted when UoW is done so CXF does not leak files. 
(#5326)
---
 .../apache/camel/component/cxf/CxfConsumer.java    | 29 -----------------
 .../apache/camel/component/cxf/CxfProducer.java    | 35 ---------------------
 .../camel/component/cxf/DefaultCxfBinding.java     | 36 ++++++++++++++++++++++
 3 files changed, 36 insertions(+), 64 deletions(-)

diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
index 0469fb1..58096c7 100644
--- 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfConsumer.java
@@ -16,13 +16,10 @@
  */
 package org.apache.camel.component.cxf;
 
-import java.io.InputStream;
 import java.lang.reflect.Method;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.activation.DataHandler;
 import javax.xml.ws.WebFault;
 
 import org.w3c.dom.Element;
@@ -30,22 +27,18 @@ import org.w3c.dom.Element;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.ExchangeTimedOutException;
-import org.apache.camel.ExtendedExchange;
 import org.apache.camel.Processor;
 import org.apache.camel.Suspendable;
 import org.apache.camel.component.cxf.common.message.CxfConstants;
 import org.apache.camel.component.cxf.interceptors.UnitOfWorkCloserInterceptor;
 import org.apache.camel.component.cxf.util.CxfUtils;
 import org.apache.camel.support.DefaultConsumer;
-import org.apache.camel.support.SynchronizationAdapter;
-import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.cxf.continuations.Continuation;
 import org.apache.cxf.continuations.ContinuationProvider;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.frontend.ServerFactoryBean;
 import org.apache.cxf.interceptor.Fault;
-import org.apache.cxf.message.Attachment;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.FaultMode;
 import org.apache.cxf.message.Message;
@@ -310,28 +303,6 @@ public class CxfConsumer extends DefaultConsumer 
implements Suspendable {
             // put the context into camelExchange
             camelExchange.setProperty(CxfConstants.JAXWS_CONTEXT, context);
 
-            // add UoW done to avoid CXF file leaks
-            camelExchange.adapt(ExtendedExchange.class).addOnCompletion(new 
SynchronizationAdapter() {
-                @Override
-                public void onDone(org.apache.camel.Exchange exchange) {
-                    // CXF may leak temporary cached attachments to temp 
folder that has not been in use
-                    Collection<Attachment> atts = 
cxfExchange.getInMessage().getAttachments();
-                    if (atts != null) {
-                        for (Attachment att : atts) {
-                            DataHandler dh = att.getDataHandler();
-                            if (dh != null) {
-                                try {
-                                    InputStream is = dh.getInputStream();
-                                    IOHelper.close(is);
-                                } catch (Exception e) {
-                                    // ignore
-                                }
-                            }
-                        }
-                    }
-                }
-            });
-
             // we want to handle the UoW
             try {
                 CxfConsumer.this.createUoW(camelExchange);
diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java
index b100d45..464e16f 100644
--- 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfProducer.java
@@ -25,21 +25,16 @@ import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
-import javax.activation.DataHandler;
 import javax.xml.namespace.QName;
 import javax.xml.ws.Holder;
 import javax.xml.ws.handler.MessageContext.Scope;
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
-import org.apache.camel.ExtendedExchange;
 import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.attachment.AttachmentMessage;
 import org.apache.camel.component.cxf.common.message.CxfConstants;
 import org.apache.camel.support.DefaultAsyncProducer;
-import org.apache.camel.support.SynchronizationAdapter;
 import org.apache.camel.support.service.ServiceHelper;
-import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.cxf.Bus;
 import org.apache.cxf.binding.soap.model.SoapHeaderInfo;
@@ -117,8 +112,6 @@ public class CxfProducer extends DefaultAsyncProducer {
             invocationContext.put(Client.RESPONSE_CONTEXT, responseContext);
             invocationContext.put(Client.REQUEST_CONTEXT, 
prepareRequest(camelExchange, cxfExchange));
 
-            addAttachmentFileCloseUoW(camelExchange);
-
             CxfClientCallback cxfClientCallback = new 
CxfClientCallback(callback, camelExchange, cxfExchange, boi, endpoint);
             // send the CXF async request
             client.invoke(cxfClientCallback, boi, getParams(endpoint, 
camelExchange),
@@ -157,8 +150,6 @@ public class CxfProducer extends DefaultAsyncProducer {
         invocationContext.put(Client.REQUEST_CONTEXT, 
prepareRequest(camelExchange, cxfExchange));
 
         try {
-            addAttachmentFileCloseUoW(camelExchange);
-
             // send the CXF request
             client.invoke(boi, getParams(endpoint, camelExchange),
                     invocationContext, cxfExchange);
@@ -189,32 +180,6 @@ public class CxfProducer extends DefaultAsyncProducer {
         }
     }
 
-    private void addAttachmentFileCloseUoW(Exchange camelExchange) {
-        // add UoW done to avoid CXF file leaks
-        camelExchange.adapt(ExtendedExchange.class).addOnCompletion(new 
SynchronizationAdapter() {
-            @Override
-            public void onDone(org.apache.camel.Exchange exchange) {
-                // CXF may leak temporary cached attachments to temp folder 
that has not been in use
-                AttachmentMessage am = 
exchange.getMessage(AttachmentMessage.class);
-                if (am != null) {
-                    Map<String, DataHandler> atts = am.getAttachments();
-                    if (atts != null) {
-                        for (DataHandler dh : atts.values()) {
-                            if (dh != null) {
-                                try {
-                                    InputStream is = dh.getInputStream();
-                                    IOHelper.close(is);
-                                } catch (Exception e) {
-                                    // ignore
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-        });
-    }
-
     protected Map<String, Object> prepareRequest(Exchange camelExchange, 
org.apache.cxf.message.Exchange cxfExchange)
             throws Exception {
 
diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
index ffcfb27..ff87f1e 100644
--- 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
@@ -23,6 +23,7 @@ import java.lang.reflect.Modifier;
 import java.nio.charset.Charset;
 import java.security.Principal;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -31,6 +32,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
 
+import javax.activation.DataHandler;
 import javax.security.auth.Subject;
 import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
@@ -46,6 +48,7 @@ import org.w3c.dom.Node;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.ExchangePropertyKey;
+import org.apache.camel.ExtendedExchange;
 import org.apache.camel.attachment.AttachmentMessage;
 import org.apache.camel.attachment.DefaultAttachment;
 import org.apache.camel.component.cxf.common.header.CxfHeaderHelper;
@@ -54,6 +57,8 @@ import org.apache.camel.component.cxf.util.ReaderInputStream;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategyAware;
 import org.apache.camel.support.ExchangeHelper;
+import org.apache.camel.support.SynchronizationAdapter;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.cxf.attachment.AttachmentImpl;
 import org.apache.cxf.binding.soap.Soap11;
@@ -201,6 +206,36 @@ public class DefaultCxfBinding implements CxfBinding, 
HeaderFilterStrategyAware
                         createCamelAttachment(attachment));
             }
         }
+        addAttachmentFileCloseUoW(camelExchange, cxfExchange);
+    }
+
+    /**
+     * CXF may cache attachments in the filesystem temp folder. The files may 
leak if they were not used. Add a cleanup
+     * handler to remove the attachments after message processing.
+     *
+     * @param camelExchange
+     * @param cxfExchange
+     */
+    private void addAttachmentFileCloseUoW(Exchange camelExchange, 
org.apache.cxf.message.Exchange cxfExchange) {
+        camelExchange.adapt(ExtendedExchange.class).addOnCompletion(new 
SynchronizationAdapter() {
+            @Override
+            public void onDone(org.apache.camel.Exchange exchange) {
+                Collection<Attachment> atts = 
cxfExchange.getInMessage().getAttachments();
+                if (atts != null) {
+                    for (Attachment att : atts) {
+                        DataHandler dh = att.getDataHandler();
+                        if (dh != null) {
+                            try {
+                                InputStream is = dh.getInputStream();
+                                IOHelper.close(is);
+                            } catch (Exception e) {
+                                // ignore
+                            }
+                        }
+                    }
+                }
+            }
+        });
     }
 
     private DefaultAttachment createCamelAttachment(Attachment attachment) {
@@ -319,6 +354,7 @@ public class DefaultCxfBinding implements CxfBinding, 
HeaderFilterStrategyAware
                         createCamelAttachment(attachment));
             }
         }
+        addAttachmentFileCloseUoW(camelExchange, cxfExchange);
     }
 
     /**

Reply via email to