You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2013/06/17 20:48:18 UTC

svn commit: r1493879 - /cxf/branches/2.5.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java

Author: dkulp
Date: Mon Jun 17 18:48:18 2013
New Revision: 1493879

URL: http://svn.apache.org/r1493879
Log:
Merged revisions 1493874 via  git cherry-pick from
https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes

........
  r1493874 | dkulp | 2013-06-17 14:43:26 -0400 (Mon, 17 Jun 2013) | 19 lines

  Merged revisions 1493868 via  git cherry-pick from
  https://svn.apache.org/repos/asf/cxf/branches/2.7.x-fixes

  ........
    r1493868 | dkulp | 2013-06-17 14:29:34 -0400 (Mon, 17 Jun 2013) | 11 lines

    Merged revisions 1493835 via  git cherry-pick from
    https://svn.apache.org/repos/asf/cxf/trunk

    ........
      r1493835 | dkulp | 2013-06-17 12:21:35 -0400 (Mon, 17 Jun 2013) | 3 lines

      [CXF-5078] Cache the wsdl into a CachedOutputStream before writing out to real http stream to avoid holding the lock for a long time.
      Patch from John Bellassai applied

    ........

  ........

........

Modified:
    cxf/branches/2.5.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java

Modified: cxf/branches/2.5.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java?rev=1493879&r1=1493878&r2=1493879&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java (original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java Mon Jun 17 18:48:18 2013
@@ -20,6 +20,7 @@
 package org.apache.cxf.frontend;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Map;
 import java.util.logging.Level;
@@ -34,7 +35,9 @@ import org.w3c.dom.Document;
 import org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
@@ -93,14 +96,48 @@ public class WSDLGetInterceptor extends 
         String baseUri = (String)message.get(Message.REQUEST_URL);
         String ctx = (String)message.get(Message.PATH_INFO);
         
-        
-        //cannot have two wsdl's being written for the same endpoint at the same
-        //time as the addresses may get mixed up
-        synchronized (message.getExchange().getEndpoint()) {
-            Map<String, String> map = UrlUtilities.parseQueryString(query);
-            if (isRecognizedQuery(map, baseUri, ctx, 
-                                  message.getExchange().getEndpoint().getEndpointInfo())) {
-                
+        boolean setOutMessageToNull = false;
+        try {
+            CachedOutputStream cos = null;
+
+            //cannot have two wsdl's being written for the same endpoint at the same
+            //time as the addresses may get mixed up
+            synchronized (message.getExchange().getEndpoint()) {
+                Map<String, String> map = UrlUtils.parseQueryString(query);
+                if (isRecognizedQuery(map, baseUri, ctx, 
+                                      message.getExchange().getEndpoint().getEndpointInfo())) {
+
+                    setOutMessageToNull = true;
+                    try {
+                        Document doc = getDocument(message,
+                                      baseUri,
+                                      map,
+                                      ctx,
+                                      message.getExchange().getEndpoint().getEndpointInfo());
+                        String enc = null;
+                        try {
+                            enc = doc.getXmlEncoding();
+                        } catch (Exception ex) {
+                            //ignore - not dom level 3
+                        }
+                        if (enc == null) {
+                            enc = "utf-8";
+                        }
+
+                        cos = new CachedOutputStream();
+                        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(cos,
+                                                                                 enc);
+                        StaxUtils.writeNode(doc, writer, true);
+                        writer.flush();
+                        writer.close();
+
+                    } catch (XMLStreamException e) {
+                        throw new Fault(e);
+                    }
+                }
+            }
+
+            if (cos != null) {
                 try {
                     Conduit c = message.getExchange().getDestination().getBackChannel(message, null, null);
                     Message mout = new MessageImpl();
@@ -109,47 +146,34 @@ public class WSDLGetInterceptor extends 
                     mout.put(Message.CONTENT_TYPE, "text/xml");
                     c.prepare(mout);
                     OutputStream os = mout.getContent(OutputStream.class);
-                    Document doc = getDocument(message,
-                                  baseUri,
-                                  map,
-                                  ctx,
-                                  message.getExchange().getEndpoint().getEndpointInfo());
-                    String enc = null;
-                    try {
-                        enc = doc.getXmlEncoding();
-                    } catch (Exception ex) {
-                        //ignore - not dom level 3
-                    }
-                    if (enc == null) {
-                        enc = "utf-8";
-                    }
 
-                    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os,
-                                                                             enc);
-                    StaxUtils.writeNode(doc, writer, true);
+                    InputStream cis = cos.getInputStream();
+                    IOUtils.copyAndCloseInput(cis, os);
                     message.getInterceptorChain().abort();
+
                     try {
-                        writer.flush();
-                        writer.close();
                         os.flush();
                         os.close();
                     } catch (IOException ex) {
                         LOG.log(Level.FINE, "Failure writing full wsdl to the stream", ex);
                         //we can ignore this.   Likely, whatever has requested the WSDL
-                        //has closed the connection before reading the entire wsdl.  
+                        //has closed the connection before reading the entire wsdl.
                         //WSDL4J has a tendency to not read the closing tags and such
-                        //and thus can sometimes hit this.   In anycase, it's 
+                        //and thus can sometimes hit this.   In any case, it's
                         //pretty much ignorable and nothing we can do about it (cannot
                         //send a fault or anything anyway
                     }
+
+                    cos.close();
                 } catch (IOException e) {
                     throw new Fault(e);
-                } catch (XMLStreamException e) {
-                    throw new Fault(e);
-                } finally {
-                    message.getExchange().setOutMessage(null);
                 }
             }
+
+        } finally {
+            if (setOutMessageToNull) {
+                message.getExchange().setOutMessage(null);
+            }
         }
     }
     public Document getDocument(Message message,
@@ -171,3 +195,4 @@ public class WSDLGetInterceptor extends 
     }
 
 }
+