[wise-commits] wise SVN: r579 - in core/trunk/core/src: main/java/org/jboss/wise/core/client/builder and 4 other directories.

wise-commits at lists.jboss.org wise-commits at lists.jboss.org
Thu Jul 4 06:25:51 EDT 2013


Author: alessio.soldano at jboss.com
Date: 2013-07-04 06:25:51 -0400 (Thu, 04 Jul 2013)
New Revision: 579

Added:
   core/trunk/core/src/main/java/org/jboss/wise/core/client/WSDLParser.java
   core/trunk/core/src/test/resources/AddNumbersMultiplePorts.wsdl
Modified:
   core/trunk/core/src/main/java/org/jboss/wise/core/client/builder/WSDynamicClientBuilder.java
   core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSDynamicClientImpl.java
   core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSServiceImpl.java
   core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/builder/ReflectionBasedWSDynamicClientBuilder.java
   core/trunk/core/src/test/java/org/jboss/wise/core/client/impl/reflection/WSServiceImplTest.java
Log:
[WISE-207] Add option for skipping non-SOAP WSDL ports


Added: core/trunk/core/src/main/java/org/jboss/wise/core/client/WSDLParser.java
===================================================================
--- core/trunk/core/src/main/java/org/jboss/wise/core/client/WSDLParser.java	                        (rev 0)
+++ core/trunk/core/src/main/java/org/jboss/wise/core/client/WSDLParser.java	2013-07-04 10:25:51 UTC (rev 579)
@@ -0,0 +1,200 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2013, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.wise.core.client;
+
+import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
+import static org.jboss.wsf.spi.util.StAXUtils.attributeAsQName;
+import static org.jboss.wsf.spi.util.StAXUtils.match;
+import static org.jboss.wsf.spi.util.StAXUtils.nextElement;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.jboss.wise.core.exception.WiseRuntimeException;
+import org.jboss.wsf.spi.util.StAXUtils;
+
+
+/**
+ * WSDL parsing utilities
+ * 
+ * @author alessio.soldano at jboss.com
+ * @since 04-Jul-2013
+ * 
+ */
+public class WSDLParser {
+    
+    private static final String WSDL_NS = "http://schemas.xmlsoap.org/wsdl/";
+    private static final String SOAP_NS = "http://schemas.xmlsoap.org/wsdl/soap/";
+    private static final String SOAP12_NS = "http://schemas.xmlsoap.org/wsdl/soap12/";
+    private static final String DEFINITIONS = "definitions";
+    private static final String SERVICE = "service";
+    private static final String PORT = "port";
+    private static final String ADDRESS = "address";
+    private static final String NAME = "name";
+    private static final String TARGET_NAMESPACE = "targetNamespace";
+
+    public static Set<String> searchNonSoapServices(String wsdlUrl) throws WiseRuntimeException {
+	try {
+	    return searchNonSoapServices(new URL(wsdlUrl));
+	} catch (MalformedURLException mue) {
+	    throw new WiseRuntimeException(mue);
+	}
+    }
+    
+    public static Set<String> searchNonSoapServices(URL wsdlUrl) throws WiseRuntimeException {
+	Set<String> excludedPorts = new HashSet<String>();
+	InputStream is = null;
+	try {
+	    is = wsdlUrl.openStream();
+	    XMLStreamReader xmlr = StAXUtils.createXMLStreamReader(is);
+	    parse(xmlr, wsdlUrl, excludedPorts);
+	    return excludedPorts;
+	} catch (Exception e) {
+	    throw new WiseRuntimeException("Failed to read: " + wsdlUrl, e);
+	} finally {
+	    try {
+		if (is != null)
+		    is.close();
+	    } catch (IOException e) {
+	    } // ignore
+	}
+    }
+    
+    private static void parse(XMLStreamReader reader, URL wsdlUrl, Set<String> excludedPorts) throws XMLStreamException, WiseRuntimeException
+    {
+       int iterate;
+       try
+       {
+          iterate = reader.nextTag();
+       }
+       catch (XMLStreamException e)
+       {
+          // skip non-tag elements
+          iterate = reader.nextTag();
+       }
+       switch (iterate)
+       {
+          case END_ELEMENT : {
+             // we're done
+             break;
+          }
+          case START_ELEMENT : {
+
+             if (match(reader, WSDL_NS, DEFINITIONS))
+             {
+                String targetNS = reader.getAttributeValue(null, TARGET_NAMESPACE);
+                parseDefinitions(reader, targetNS, wsdlUrl, excludedPorts);
+             }
+             else
+             {
+                throw new WiseRuntimeException("Unexpected element '" + reader.getLocalName() + "' found parsing " + wsdlUrl.toExternalForm());
+             }
+          }
+       }
+    }
+    
+    private static void parseDefinitions(XMLStreamReader reader, String targetNS, URL wsdlUrl, Set<String> excludedPorts) throws XMLStreamException, WiseRuntimeException
+    {
+       while (reader.hasNext())
+       {
+          switch (nextElement(reader))
+          {
+             case XMLStreamConstants.END_ELEMENT : {
+                if (match(reader, WSDL_NS, DEFINITIONS))
+                {
+                   return;
+                }
+                continue;
+             }
+             case XMLStreamConstants.START_ELEMENT : {
+                if (match(reader, WSDL_NS, SERVICE)) {
+                   parseService(reader, targetNS, wsdlUrl, excludedPorts);
+                }
+                continue;
+             }
+          }
+       }
+       throw new WiseRuntimeException("Reached end of XML document unexpectedly: " + wsdlUrl.toExternalForm());
+    }
+    
+    private static void parseService(XMLStreamReader reader, String targetNS, URL wsdlUrl, Set<String> excludedPorts) throws XMLStreamException
+    {
+       while (reader.hasNext())
+       {
+          switch (nextElement(reader))
+          {
+             case XMLStreamConstants.END_ELEMENT : {
+                if (match(reader, WSDL_NS, SERVICE))
+                {
+                   return;
+                }
+                continue;
+             }
+             case XMLStreamConstants.START_ELEMENT : {
+                if (match(reader, WSDL_NS, PORT)) {
+                   QName name = attributeAsQName(reader, null, NAME, targetNS);
+                   if(!isSoapPort(reader, wsdlUrl)) {
+                       excludedPorts.add(name.getLocalPart());
+                   }
+                }
+                continue;
+             }
+          }
+       }
+       throw new WiseRuntimeException("Reached end of XML document unexpectedly: " + wsdlUrl.toExternalForm());
+    }
+    
+    private static boolean isSoapPort(XMLStreamReader reader, URL wsdlUrl) throws XMLStreamException
+    {
+       while (reader.hasNext())
+       {
+          switch (nextElement(reader))
+          {
+             case XMLStreamConstants.END_ELEMENT : {
+                if (match(reader, WSDL_NS, PORT))
+                {
+                   return false;
+                }
+                continue;
+             }
+             case XMLStreamConstants.START_ELEMENT : {
+                if (match(reader, SOAP_NS, ADDRESS) || match(reader, SOAP12_NS, ADDRESS)) {
+                   return true;
+                }
+                continue;
+             }
+          }
+       }
+       throw new WiseRuntimeException("Reached end of XML document unexpectedly: " + wsdlUrl.toExternalForm());
+    }
+
+}

Modified: core/trunk/core/src/main/java/org/jboss/wise/core/client/builder/WSDynamicClientBuilder.java
===================================================================
--- core/trunk/core/src/main/java/org/jboss/wise/core/client/builder/WSDynamicClientBuilder.java	2013-07-03 22:01:47 UTC (rev 578)
+++ core/trunk/core/src/main/java/org/jboss/wise/core/client/builder/WSDynamicClientBuilder.java	2013-07-04 10:25:51 UTC (rev 579)
@@ -151,6 +151,14 @@
      * @return {@link WSDynamicClient}
      */
     public WSDynamicClientBuilder verbose(boolean bool);
+    
+    /**
+     * if it set to true non-SOAP wsdl ports will be excluded
+     * 
+     * @param bool
+     * @return {@link WSDynamicClient}
+     */
+    public WSDynamicClientBuilder excludeNonSOAPPorts(boolean exclude);
 
     /**
      * Sets the PrintStream to use for status feedback. The simplest example
@@ -192,6 +200,8 @@
     public boolean isKeepSource();
 
     public boolean isVerbose();
+    
+    public boolean isExcludeNonSOAPPorts();
 
     public PrintStream getMessageStream();
 

Modified: core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSDynamicClientImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSDynamicClientImpl.java	2013-07-03 22:01:47 UTC (rev 578)
+++ core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSDynamicClientImpl.java	2013-07-04 10:25:51 UTC (rev 579)
@@ -23,7 +23,6 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.lang.annotation.Annotation;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -32,6 +31,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.xml.ws.WebServiceClient;
@@ -42,6 +42,7 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.log4j.Logger;
 import org.jboss.wise.core.client.SpiLoader;
+import org.jboss.wise.core.client.WSDLParser;
 import org.jboss.wise.core.client.WSDynamicClient;
 import org.jboss.wise.core.client.WSEndpoint;
 import org.jboss.wise.core.client.WSMethod;
@@ -79,6 +80,8 @@
     private final CopyOnWriteArrayList<String> classNames = new CopyOnWriteArrayList<String>();
 
     private final Map<String, WSService> servicesMap = Collections.synchronizedMap(new HashMap<String, WSService>());
+    
+    private Set<String> excludedPorts;
 
     private final Smooks smooksInstance;
 
@@ -129,6 +132,10 @@
 	    throw new WiseRuntimeException("Problem consuming wsdl:" + builder.getWsdlURL(), e);
 	}
 	this.initClassLoader(outputDir);
+	
+	if (builder.isExcludeNonSOAPPorts()) {
+	    excludedPorts = WSDLParser.searchNonSoapServices(builder.getNormalizedWsdlUrl());
+	}
 
 	this.processServices();
     }
@@ -159,7 +166,7 @@
 		    "Error occurred while setting up classloader for generated class in directory: " + outputDir, e);
 	}
     }
-
+    
     /**
      * {@inheritDoc}
      * 
@@ -167,16 +174,15 @@
      */
     public synchronized Map<String, WSService> processServices() throws IllegalStateException {
 	ClassLoader oldLoader = SecurityActions.getContextClassLoader();
-
 	try {
 	    SecurityActions.setContextClassLoader(this.getClassLoader());
 	    for (String className : classNames) {
 		try {
 		    Class<?> clazz = JavaUtils.loadJavaType(className, this.getClassLoader());
-		    Annotation annotation = clazz.getAnnotation(WebServiceClient.class);
+		    WebServiceClient annotation = clazz.getAnnotation(WebServiceClient.class);
 		    if (annotation != null) {
 			WSService service = createService(clazz);
-			servicesMap.put(((WebServiceClient) annotation).name(), service);
+			servicesMap.put(annotation.name(), service);
 		    }
 		} catch (Exception e) {
 		    e.printStackTrace();
@@ -193,7 +199,7 @@
     }
     
     protected WSService createService(Class<?> clazz) throws InstantiationException, IllegalAccessException {
-	return new WSServiceImpl(clazz, this.getClassLoader(), clazz.newInstance(), userName, password, this.maxThreadPoolSize);
+	return new WSServiceImpl(clazz, this.getClassLoader(), clazz.newInstance(), userName, password, excludedPorts, this.maxThreadPoolSize);
     }
 
     /**

Modified: core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSServiceImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSServiceImpl.java	2013-07-03 22:01:47 UTC (rev 578)
+++ core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/WSServiceImpl.java	2013-07-04 10:25:51 UTC (rev 579)
@@ -26,6 +26,8 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
+
 import javax.xml.ws.WebEndpoint;
 import net.jcip.annotations.Immutable;
 import net.jcip.annotations.ThreadSafe;
@@ -46,6 +48,7 @@
     private final String userName;
     private final String password;
     private final Map<String, WSEndpoint> endpoints = Collections.synchronizedMap(new HashMap<String, WSEndpoint>());
+    private final Set<String> excludedPorts;
     protected final int maxThreadPoolSize;
 
     /**
@@ -61,6 +64,7 @@
                           Object service,
                           String userName,
                           String password,
+                          Set<String> excludedPorts,
                           int maxThreadPoolSize ) {
         super();
         this.serviceClass = serviceClass;
@@ -68,6 +72,7 @@
         this.service = service;
         this.userName = userName;
         this.password = password;
+        this.excludedPorts = excludedPorts;
         endpoints.clear();
         this.processEndpoints();
         this.maxThreadPoolSize = maxThreadPoolSize;
@@ -89,7 +94,7 @@
 
         for (Method method : this.getServiceClass().getMethods()) {
             WebEndpoint annotation = method.getAnnotation(WebEndpoint.class);
-            if (annotation != null) {
+            if (annotation != null && (excludedPorts == null || !excludedPorts.contains(annotation.name()))) {
                 WSEndpoint ep;
                 try {
                     if (method.getParameterTypes().length == 0) // required to support JAX-WS 2.1, as you get 2 @WebEndpoint ->

Modified: core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/builder/ReflectionBasedWSDynamicClientBuilder.java
===================================================================
--- core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/builder/ReflectionBasedWSDynamicClientBuilder.java	2013-07-03 22:01:47 UTC (rev 578)
+++ core/trunk/core/src/main/java/org/jboss/wise/core/client/impl/reflection/builder/ReflectionBasedWSDynamicClientBuilder.java	2013-07-04 10:25:51 UTC (rev 579)
@@ -89,6 +89,9 @@
 
     @GuardedBy("this")
     private boolean verbose;
+    
+    @GuardedBy("this")
+    private boolean excludeNonSoapPorts;
 
     @GuardedBy("this")
     private String normalizedWsdlUrl;
@@ -129,14 +132,16 @@
 	if (this.getMaxThreadPoolSize() < 1) {
 	    throw new IllegalStateException("MaxThreadPoolSize cannot be less than 1");
 	}
-	String wsdlUrl = this.getWsdlURL();
+	final String wsdlUrl = this.getWsdlURL();
+	final String nwu;
 	if (userName != null || (StringUtils.trimToNull(wsdlUrl) != null && Connection.isLocalAddress(wsdlUrl))) {
-	    this.setNormalizedWsdlUrl(this.transferWSDL(userName, password, clientSpecificTmpDir));
+	    nwu = this.transferWSDL(userName, password, clientSpecificTmpDir);
 	} else {
-	    this.setNormalizedWsdlUrl(wsdlUrl);
+	    nwu = wsdlUrl;
 	}
+	this.setNormalizedWsdlUrl(nwu);
 
-	if (this.getNormalizedWsdlUrl() == null || this.getNormalizedWsdlUrl().trim().length() == 0) {
+	if (nwu == null || nwu.trim().length() == 0) {
 	    throw new IllegalStateException("wsdlURL cannot be null");
 	}
 
@@ -334,6 +339,15 @@
     /**
      * {@inheritDoc}
      * 
+     * @see org.jboss.wise.core.client.builder.WSDynamicClientBuilder#isExcludeNonSOAPPorts()
+     */
+    public synchronized boolean isExcludeNonSOAPPorts() {
+	return this.excludeNonSoapPorts;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
      * @see org.jboss.wise.core.client.builder.WSDynamicClientBuilder#keepSource(boolean)
      */
     public synchronized WSDynamicClientBuilder keepSource(boolean bool) {
@@ -372,6 +386,16 @@
     }
 
     /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.wise.core.client.builder.WSDynamicClientBuilder#excludeNonSOAPPorts(boolean)
+     */
+    public synchronized WSDynamicClientBuilder excludeNonSOAPPorts(boolean exclude) {
+	this.excludeNonSoapPorts = exclude;
+	return this;
+    }
+
+    /**
      * @return normalizedWsdlUrl
      */
     public synchronized String getNormalizedWsdlUrl() {

Modified: core/trunk/core/src/test/java/org/jboss/wise/core/client/impl/reflection/WSServiceImplTest.java
===================================================================
--- core/trunk/core/src/test/java/org/jboss/wise/core/client/impl/reflection/WSServiceImplTest.java	2013-07-03 22:01:47 UTC (rev 578)
+++ core/trunk/core/src/test/java/org/jboss/wise/core/client/impl/reflection/WSServiceImplTest.java	2013-07-04 10:25:51 UTC (rev 579)
@@ -53,7 +53,7 @@
     @Test
     public void shouldProcessEndPoint() throws Exception {
         URLClassLoader loader = new URLClassLoader(new URL[] {}, Thread.currentThread().getContextClassLoader());
-        WSService service = new WSServiceImpl(WSServiceImplTest.class, loader, this, null, null, 10);
+        WSService service = new WSServiceImpl(WSServiceImplTest.class, loader, this, null, null, null, 10);
         Map<String, WSEndpoint> endpoints = service.processEndpoints();
         assertThat(endpoints.keySet(), hasItem("EndPoint1"));
         assertThat(endpoints.keySet(), hasItem("EndPoint2"));

Added: core/trunk/core/src/test/resources/AddNumbersMultiplePorts.wsdl
===================================================================
--- core/trunk/core/src/test/resources/AddNumbersMultiplePorts.wsdl	                        (rev 0)
+++ core/trunk/core/src/test/resources/AddNumbersMultiplePorts.wsdl	2013-07-04 10:25:51 UTC (rev 579)
@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<definitions name="AddNumbers"  targetNamespace="http://duke.example.org" xmlns:tns="http://duke.example.org"
+    xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/">
+	<types>
+            <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://duke.example.org">
+
+                <complexType name="addNumbersResponse">
+                    <sequence>
+                        <element name="return" type="xsd:int" />
+                    </sequence>
+                </complexType>
+                <element name="addNumbersResponse" type="tns:addNumbersResponse" />
+
+                <complexType name="addNumbers">
+                    <sequence>
+                        <element name="arg0" type="xsd:int" />
+                        <element name="arg1" type="xsd:int" />
+                    </sequence>
+                </complexType>
+                <element name="addNumbers" type="tns:addNumbers" />
+
+                <element name="AddNumbersFault" type="tns:AddNumbersFault" />
+                <complexType name="AddNumbersFault">
+                    <sequence>
+                        <element name="faultInfo" type="xsd:string" />
+                        <element name="message" type="xsd:string" />
+                    </sequence>
+                </complexType>
+                <complexType name="oneWayInt">
+                    <sequence>
+                        <element name="arg0" type="xsd:int" />
+                    </sequence>
+                </complexType>
+                <element name="oneWayInt" type="tns:oneWayInt" />
+            </xsd:schema>
+	</types>
+	<message name="addNumbers">
+            <part name="parameters" element="tns:addNumbers" />
+	</message>
+	<message name="addNumbersResponse">
+            <part name="result" element="tns:addNumbersResponse" />
+	</message>
+	<message name="addNumbersFault">
+            <part name="AddNumbersFault" element="tns:AddNumbersFault" />
+	</message>
+	<message name="oneWayInt">
+            <part name="parameters" element="tns:oneWayInt" />
+	</message>
+	<portType name="AddNumbersPortType">
+            <operation name="addNumbers">
+                <input message="tns:addNumbers" name="add"/>
+                <output message="tns:addNumbersResponse" name="addResponse"/>
+                <fault name="addNumbersFault" message="tns:addNumbersFault"/>
+            </operation>
+            <operation name="oneWayInt">
+                <input message="tns:oneWayInt" />
+            </operation>
+	</portType>
+	<binding name="AddNumbersBinding" type="tns:AddNumbersPortType">
+            <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
+            <operation name="addNumbers">
+                <soap:operation soapAction="" />
+                <input>
+                    <soap:body use="literal" />
+                </input>
+                <output>
+                    <soap:body use="literal" />
+                </output>
+                <fault name="addNumbersFault">
+                    <soap:fault name="addNumbersFault" use="literal" />
+                </fault>
+            </operation>
+            <operation name="oneWayInt">
+                <soap:operation soapAction="" />
+                <input>
+                    <soap:body use="literal" />
+                </input>
+            </operation>
+	</binding>
+	<binding name="AddNumbersBindingHttp" type="tns:AddNumbersPortType">
+            <http:binding verb="POST"/>
+            <operation name="addNumbers">
+                <http:operation location="/AddNumbers"/>
+                <input>
+                    <mime:content type="application/x-www-form-urlencoded"/>
+                </input>
+                <output>
+                    <mime:mimeXml part="Body"/>
+                </output>
+            </operation>
+	</binding>
+	<service name="AddNumbersService">
+            <port name="AddNumbersPort" binding="tns:AddNumbersBinding">
+                <soap:address location="http://localhost:9090" />
+            </port>
+            <port name="AddNumbersPortHttp" binding="tns:AddNumbersBindingHttp">
+                <http:address location="http://localhost:9090" />
+            </port>
+	</service>
+</definitions>



More information about the wise-commits mailing list