Author: kstam
Date: Thu Jun  2 14:59:41 2011
New Revision: 1130591

URL: http://svn.apache.org/viewvc?rev=1130591&view=rev
Log:
JUDDI-479 support reading the WSDLfile out of filesystem or jar. We need the 
WSDLLocatorImpl to handle reading from a jar

Added:
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java
    
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ReadWSDLTest.java
Modified:
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ReadWSDL.java

Modified: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ReadWSDL.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ReadWSDL.java?rev=1130591&r1=1130590&r2=1130591&view=diff
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ReadWSDL.java
 (original)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ReadWSDL.java
 Thu Jun  2 14:59:41 2011
@@ -19,6 +19,7 @@ import java.net.URL;
 import javax.wsdl.Definition;
 import javax.wsdl.WSDLException;
 import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.xml.WSDLLocator;
 import javax.wsdl.xml.WSDLReader;
 
 import org.apache.juddi.v3.client.ClassUtil;
@@ -31,12 +32,16 @@ import com.ibm.wsdl.factory.WSDLFactoryI
 public class ReadWSDL {
        
        public Definition readWSDL(String fileName) throws WSDLException {
+       
                WSDLFactory factory = WSDLFactoryImpl.newInstance();
                WSDLReader reader = factory.newWSDLReader();
-               URL wsdlURL = ClassUtil.getResource(fileName, this.getClass());
-               Definition wsdlDefinition = 
reader.readWSDL(wsdlURL.toExternalForm());
+               URL url = ClassUtil.getResource(fileName, this.getClass());
+               WSDLLocator locator = new WSDLLocatorImpl(url);
+               Definition wsdlDefinition = reader.readWSDL(locator);
                return wsdlDefinition;
        }
        
        
+       
+       
 }

Added: 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java?rev=1130591&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java
 (added)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java
 Thu Jun  2 14:59:41 2011
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2001-2011 The Apache Software Foundation.
+ * 
+ * Licensed 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.juddi.v3.client.mapping;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.wsdl.xml.WSDLLocator;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.xml.sax.InputSource;
+
+
+public class WSDLLocatorImpl implements WSDLLocator {
+       
+         private final Log log = LogFactory.getLog(this.getClass());
+      private URL wsdlURL;
+      private String latestImportURI;
+      private InputStream is = null;
+
+      public WSDLLocatorImpl(URL wsdlFile)
+      {
+         if (wsdlFile == null)
+            throw new IllegalArgumentException("WSDL file argument cannot be 
null");
+
+         this.wsdlURL = wsdlFile;
+      }
+
+      public InputSource getBaseInputSource()
+      {
+         log.debug("getBaseInputSource [wsdlUrl=" + wsdlURL + "]");
+         try
+         {
+            is = wsdlURL.openStream();
+            if (is == null)
+               throw new IllegalArgumentException("Cannot obtain wsdl from [" 
+ wsdlURL + "]");
+
+            return new InputSource(is);
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException("Cannot access wsdl from [" + wsdlURL + 
"], " + e.getMessage());
+         }
+      }
+
+      public String getBaseURI()
+      {
+         return wsdlURL.toExternalForm();
+      }
+
+      public InputSource getImportInputSource(String parent, String resource)
+      {
+         log.debug("getImportInputSource [parent=" + parent + ",resource=" + 
resource + "]");
+
+         URL parentURL = null;
+         try
+         {
+            parentURL = new URL(parent);
+         }
+         catch (MalformedURLException e)
+         {
+            log.error("Not a valid URL: " + parent);
+            return null;
+         }
+
+         String wsdlImport = null;
+         String external = parentURL.toExternalForm();
+
+         // An external URL
+         if (resource.startsWith("http://";) || resource.startsWith("https://";))
+         {
+            wsdlImport = resource;
+         }
+
+         // Absolute path
+         else if (resource.startsWith("/"))
+         {
+            String beforePath = external.substring(0, 
external.indexOf(parentURL.getPath()));
+            wsdlImport = beforePath + resource;
+         }
+
+         // A relative path
+         else
+         {
+            String parentDir = external.substring(0, 
external.lastIndexOf("/"));
+
+            // remove references to current dir
+            while (resource.startsWith("./"))
+               resource = resource.substring(2);
+
+            // remove references to parentdir
+            while (resource.startsWith("../"))
+            {
+               parentDir = parentDir.substring(0, parentDir.lastIndexOf("/"));
+               resource = resource.substring(3);
+            }
+
+            wsdlImport = parentDir + "/" + resource;
+         }
+
+         try
+         {
+            log.debug("Resolved to: " + wsdlImport);
+            InputStream is = new URL(wsdlImport).openStream();
+            if (is == null)
+               throw new IllegalArgumentException("Cannot import wsdl from [" 
+ wsdlImport + "]");
+
+            latestImportURI = wsdlImport;
+            return new InputSource(is);
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException("Cannot access imported wsdl [" + 
wsdlImport + "], " + e.getMessage());
+         }
+      }
+
+      public String getLatestImportURI()
+      {
+         return latestImportURI;
+      }
+
+       public void close() {
+               if (is!=null)
+                       try {
+                               is.close();
+                       } catch (IOException e) {
+                               log.error(e.getMessage(),e);
+                       }
+               
+       }
+   }
+

Added: 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ReadWSDLTest.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ReadWSDLTest.java?rev=1130591&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ReadWSDLTest.java
 (added)
+++ 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/ReadWSDLTest.java
 Thu Jun  2 14:59:41 2011
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2001-2009 The Apache Software Foundation.
+ * 
+ * Licensed 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.juddi.v3.client.mapping;
+
+import java.net.URISyntaxException;
+
+import javax.wsdl.Definition;
+import javax.wsdl.WSDLException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class ReadWSDLTest {
+
+       @Test
+       public void readFromFile() throws WSDLException, URISyntaxException {
+               
+               ReadWSDL readWSDL = new ReadWSDL();
+               Definition definition = 
readWSDL.readWSDL("wsdl/HelloWorld.wsdl");
+               Assert.assertNotNull(definition);
+       }
+       
+       @Test
+       public void readFromJar() throws WSDLException, URISyntaxException {
+               
+               ReadWSDL readWSDL = new ReadWSDL();
+               Definition definition = 
readWSDL.readWSDL("uddi_v3_service.wsdl");
+               Assert.assertNotNull(definition);
+       }
+       
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to