You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by jp...@apache.org on 2013/07/26 15:54:23 UTC

svn commit: r1507315 - in /cxf/trunk: ./ rt/frontend/simple/src/main/java/org/apache/cxf/frontend/ systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/

Author: jpell
Date: Fri Jul 26 13:54:23 2013
New Revision: 1507315

URL: http://svn.apache.org/r1507315
Log:
CXF-5151 added support for gzip ?wsdl

Added:
    cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetOutInterceptor.java
      - copied unchanged from r1507220, cxf/branches/2.7.x-fixes/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetOutInterceptor.java
Modified:
    cxf/trunk/   (props changed)
    cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java
    cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java
    cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/Server.java

Propchange: cxf/trunk/
------------------------------------------------------------------------------
  Merged /cxf/branches/2.7.x-fixes:r1507220

Modified: cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java?rev=1507315&r1=1507314&r2=1507315&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java (original)
+++ cxf/trunk/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetInterceptor.java Fri Jul 26 13:54:23 2013
@@ -19,73 +19,35 @@
 
 package org.apache.cxf.frontend;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.util.Iterator;
 import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
 
 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.common.util.UrlUtils;
-import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.interceptor.Fault;
-import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.interceptor.Interceptor;
+import org.apache.cxf.interceptor.MessageSenderInterceptor;
+import org.apache.cxf.interceptor.OutgoingChainInterceptor;
+import org.apache.cxf.interceptor.StaxOutInterceptor;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.service.model.EndpointInfo;
-import org.apache.cxf.staxutils.StaxUtils;
-import org.apache.cxf.transport.Conduit;
+import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
 
-/**
- * 
- */
 public class WSDLGetInterceptor extends AbstractPhaseInterceptor<Message> {
     public static final WSDLGetInterceptor INSTANCE = new WSDLGetInterceptor();
-       
-    private static final Logger LOG = LogUtils.getL7dLogger(WSDLGetInterceptor.class);
-    
+    public static final String DOCUMENT_HOLDER = WSDLGetInterceptor.class.getName() + ".documentHolder";
+
     public WSDLGetInterceptor() {
         super(Phase.READ);
         getAfter().add(EndpointSelectionInterceptor.class.getName());
     }
-    
-    public void doOutput(Message message, String base, Document doc, OutputStream out)
-        throws WSDLQueryException {
-        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(out,
-                                                                 enc);
-        try {
-            StaxUtils.writeNode(doc, writer, true);
-            writer.flush();
-        } catch (XMLStreamException e) {
-            throw new WSDLQueryException(new org.apache.cxf.common.i18n.Message("COULD_NOT_PROVIDE_WSDL",
-                                                                                LOG,
-                                                                                base), e);
-        } finally {
-            StaxUtils.close(writer);
-        }
-    }
-
 
     public void handleMessage(Message message) throws Fault {
         String method = (String)message.get(Message.HTTP_REQUEST_METHOD);
@@ -93,106 +55,61 @@ public class WSDLGetInterceptor extends 
         if (!"GET".equals(method) || StringUtils.isEmpty(query)) {
             return;
         }
+
         String baseUri = (String)message.get(Message.REQUEST_URL);
         String ctx = (String)message.get(Message.PATH_INFO);
-        
-        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);
+        // 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())) {
+                Document doc = getDocument(message, baseUri, map, ctx, 
+                                           message.getExchange().getEndpoint().getEndpointInfo());
+
+                Endpoint e = message.getExchange().get(Endpoint.class);
+                Message mout = new MessageImpl();
+                mout.setExchange(message.getExchange());
+                mout = e.getBinding().createMessage(mout);
+                mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(message
+                    .getExchange()));
+                message.getExchange().setOutMessage(mout);
+
+                mout.put(DOCUMENT_HOLDER, doc);
+
+                // FIXME - how can I change this to provide a different interceptor chain that just has the
+                // stax, gzip and message sender components.
+                Iterator<Interceptor<? extends Message>> iterator = mout.getInterceptorChain().iterator();
+                while (iterator.hasNext()) {
+                    Interceptor<? extends Message> inInterceptor = iterator.next();
+                    if (!inInterceptor.getClass().equals(StaxOutInterceptor.class)
+                        && !inInterceptor.getClass().equals(GZIPOutInterceptor.class)
+                        && !inInterceptor.getClass().equals(MessageSenderInterceptor.class)) {
+                        mout.getInterceptorChain().remove(inInterceptor);
                     }
                 }
-            }
-
-            if (cos != null) {
-                try {
-                    Conduit c = message.getExchange().getDestination().getBackChannel(message);
-                    Message mout = new MessageImpl();
-                    mout.setExchange(message.getExchange());
-                    message.getExchange().setOutMessage(mout);
-                    mout.put(Message.CONTENT_TYPE, "text/xml");
-                    c.prepare(mout);
-                    OutputStream os = mout.getContent(OutputStream.class);
-
-                    InputStream cis = cos.getInputStream();
-                    IOUtils.copyAndCloseInput(cis, os);
-                    message.getInterceptorChain().abort();
-
-                    try {
-                        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.
-                        //WSDL4J has a tendency to not read the closing tags and such
-                        //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);
-                }
-            }
+                mout.getInterceptorChain().add(WSDLGetOutInterceptor.INSTANCE);
 
-        } finally {
-            if (setOutMessageToNull) {
-                message.getExchange().setOutMessage(null);
+                // skip the service executor and goto the end of the chain.
+                message.getInterceptorChain().doInterceptStartingAt(
+                        message,
+                        OutgoingChainInterceptor.class.getName());
             }
         }
     }
-    public Document getDocument(Message message,
-                                String base,
-                                Map<String, String> params,
-                                String ctxUri,
+
+    private Document getDocument(Message message, String base, Map<String, String> params, String ctxUri,
                                 EndpointInfo endpointInfo) {
         return new WSDLGetUtils().getDocument(message, base, params, ctxUri, endpointInfo);
     }
-    public boolean isRecognizedQuery(Map<String, String> map,
-                                     String baseUri,
-                                     String ctx, 
+
+    private boolean isRecognizedQuery(Map<String, String> map, String baseUri, String ctx,
                                      EndpointInfo endpointInfo) {
-        if (map.containsKey("wsdl")
-            || map.containsKey("xsd")) {
+
+        if (map.containsKey("wsdl") || map.containsKey("xsd")) {
             return true;
         }
         return false;
     }
-
-}
-
+}
\ No newline at end of file

Modified: cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java?rev=1507315&r1=1507314&r2=1507315&view=diff
==============================================================================
--- cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java (original)
+++ cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java Fri Jul 26 13:54:23 2013
@@ -38,6 +38,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Logger;
+import java.util.zip.GZIPInputStream;
 
 import javax.xml.namespace.QName;
 import javax.xml.ws.AsyncHandler;
@@ -111,6 +112,7 @@ public class ClientServerTest extends Ab
         assertTrue("server did not launch correctly", launchServer(Server.class, true));
         createStaticBus(url.toString());
     }
+    
     @Test
     public void testCXF2419() throws Exception {
         org.apache.cxf.hello_world.elrefs.SOAPService serv 
@@ -823,12 +825,15 @@ public class ClientServerTest extends Ab
     @Test
     public void testGetWSDL() throws Exception {
         String url = "http://localhost:" + PORT + "/SoapContext/SoapPort?wsdl";
-        HttpURLConnection httpConnection = getHttpConnection(url);    
+        HttpURLConnection httpConnection = getHttpConnection(url);
+
+        // just testing that GZIP is not enabled by default
+        httpConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
         httpConnection.connect();        
         
         assertEquals(200, httpConnection.getResponseCode());
     
-        assertEquals("text/xml", httpConnection.getContentType());
+        assertEquals("text/xml;charset=utf-8", httpConnection.getContentType().toLowerCase());
         assertEquals("OK", httpConnection.getResponseMessage());
         
         InputStream in = httpConnection.getInputStream();
@@ -836,8 +841,23 @@ public class ClientServerTest extends Ab
         
         Document doc = StaxUtils.read(in);
         assertNotNull(doc);
-       
-                
+    }
+    
+    @Test
+    public void testGetWSDLWithGzip() throws Exception {
+        String url = "http://localhost:" + PORT + "/SoapContext/SoapPortWithGzip?wsdl";
+        HttpURLConnection httpConnection = getHttpConnection(url);
+        httpConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
+        httpConnection.connect();
+        assertEquals(200, httpConnection.getResponseCode());
+        assertEquals("text/xml;charset=utf-8", httpConnection.getContentType().toLowerCase());
+        assertEquals("OK", httpConnection.getResponseMessage());
+        assertEquals("gzip", httpConnection.getContentEncoding());
+        InputStream in = httpConnection.getInputStream();
+        assertNotNull(in);
+        GZIPInputStream inputStream = new GZIPInputStream(in);
+        Document doc = StaxUtils.read(inputStream);
+        assertNotNull(doc);
     }
     
     @Test

Modified: cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/Server.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/Server.java?rev=1507315&r1=1507314&r2=1507315&view=diff
==============================================================================
--- cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/Server.java (original)
+++ cxf/trunk/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/Server.java Fri Jul 26 13:54:23 2013
@@ -32,6 +32,8 @@ import org.apache.cxf.annotations.UseAsy
 import org.apache.cxf.jaxws.EndpointImpl;
 import org.apache.cxf.jaxws.ServerAsyncResponse;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
+import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
 import org.apache.cxf.wsdl.interceptors.URIMappingInterceptor;
 import org.apache.hello_world_soap_http.BaseGreeterImpl;
 import org.apache.hello_world_soap_http.DocLitBareGreeterImpl;
@@ -71,6 +73,13 @@ public class Server extends AbstractBusT
         Endpoint ep = Endpoint.publish(address, implementor);
         ((EndpointImpl)ep).getService().getInInterceptors().add(new URIMappingInterceptor());
         eps.add(ep);
+        
+        implementor = new GreeterImpl();
+        address = "http://localhost:" + PORT + "/SoapContext/SoapPortWithGzip";
+        Endpoint ep2 = Endpoint.publish(address, implementor);
+        ((EndpointImpl)ep2).getService().getInInterceptors().add(new GZIPInInterceptor());
+        ((EndpointImpl)ep2).getService().getOutInterceptors().add(new GZIPOutInterceptor());
+        eps.add(ep2);
 
         implementor = new RefGreeterImpl();
         address = "http://localhost:" + PORT + "/SoapContext/SoapPort2";
@@ -153,7 +162,7 @@ public class Server extends AbstractBusT
                 portName = "SoapPort",
                 endpointInterface = "org.apache.cxf.hello_world.elrefs.Greeter",
                 targetNamespace = "http://apache.org/hello_world_soap_http",
-                wsdlLocation = "testutils/hello_world_ref.wsdl")
+                wsdlLocation = "testutils/hello_world_ref.wsdl")    
     public class RefGreeterImpl implements org.apache.cxf.hello_world.elrefs.Greeter {
         public String greetMe(String requestType) {
             return "Hello " + requestType;