Author: kstam
Date: Wed Jun 22 23:46:11 2011
New Revision: 1138696

URL: http://svn.apache.org/viewvc?rev=1138696&view=rev
Log:
juddi-479, our own implementation of the WSDLLocator including test.

Added:
    
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/WSDLLocatorTest.java
Modified:
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/ReadWSDL.java
    
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.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=1138696&r1=1138695&r2=1138696&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
 Wed Jun 22 23:46:11 2011
@@ -14,6 +14,8 @@
  */
 package org.apache.juddi.v3.client.mapping;
 
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 
 import javax.wsdl.Definition;
@@ -22,6 +24,8 @@ import javax.wsdl.factory.WSDLFactory;
 import javax.wsdl.xml.WSDLLocator;
 import javax.wsdl.xml.WSDLReader;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.juddi.v3.client.ClassUtil;
 
 import com.ibm.wsdl.factory.WSDLFactoryImpl;
@@ -31,13 +35,21 @@ import com.ibm.wsdl.factory.WSDLFactoryI
  */
 public class ReadWSDL {
        
+       private final Log log = LogFactory.getLog(this.getClass());
+       
        public Definition readWSDL(String fileName) throws WSDLException {
        
+               Definition wsdlDefinition = null;
                WSDLFactory factory = WSDLFactoryImpl.newInstance();
                WSDLReader reader = factory.newWSDLReader();
                URL url = ClassUtil.getResource(fileName, this.getClass());
-               WSDLLocator locator = new WSDLLocatorImpl(url);
-               Definition wsdlDefinition = reader.readWSDL(locator);
+               try {
+                       URI uri = url.toURI();
+                       WSDLLocator locator = new WSDLLocatorImpl(uri);
+                       wsdlDefinition = reader.readWSDL(locator);
+               } catch (URISyntaxException e) {
+                       log.error(e.getMessage(),e);
+               }
                return wsdlDefinition;
        }
        

Modified: 
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=1138696&r1=1138695&r2=1138696&view=diff
==============================================================================
--- 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java
 (original)
+++ 
juddi/trunk/juddi-client/src/main/java/org/apache/juddi/v3/client/mapping/WSDLLocatorImpl.java
 Wed Jun 22 23:46:11 2011
@@ -13,12 +13,13 @@
  * 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.URI;
 import java.net.URL;
 
 import javax.wsdl.xml.WSDLLocator;
@@ -27,123 +28,112 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.xml.sax.InputSource;
 
-
+/**
+ * Implementation of the interface {@link WSDLLocatorImpl}. 
+ *  
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ *
+ */
 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;
-      }
 
+       private final Log log = LogFactory.getLog(this.getClass());
+       private InputStream inputStream = null;
+       private URI baseURI;
+       private String latestImportURI;
+       /**
+        * Constructor taking the URI to the WSDL. This class implements the 
{@link WSDLLocatorImpl}
+        * Interface.
+        * 
+        * @param baseURI - URI of the WSDL
+        */
+       public WSDLLocatorImpl(URI baseURI) {
+               this.baseURI = baseURI;
+       }
+       /**
+        * @see WSDLLocatorImpl.getBaseInputSource
+        */
+       public InputSource getBaseInputSource() {
+               InputSource inputSource = null;
+               try {
+                       inputStream = baseURI.toURL().openStream();
+                       inputSource =new InputSource(inputStream);
+               } catch (IOException e) {
+                       log.error(e.getMessage(),e);
+               }
+               return inputSource;
+       }
+       /**
+        * Internal method to normalize the importUrl. The importLocation can 
be relative to
+        * the parentLocation. 
+        * 
+        * @param parentLocation
+        * @param importLocation
+        * @return
+        */
+       protected URL constructImportUrl(String parentLocation, String 
importLocation) {
+               URL importUrl = null;
+               try {
+                       URI importLocationURI = new URI(importLocation);
+                       if (importLocationURI.getScheme()!=null || 
parentLocation==null) {
+                               importUrl = importLocationURI.toURL();
+                       } else {
+                               String parentDir = parentLocation.substring(0, 
parentLocation.lastIndexOf("/"));
+                               URI uri = new URI(parentDir + "/" + 
importLocation);
+                               importUrl = uri.normalize().toURL();
+                       }
+               } catch (Exception e) {
+                       log.error(e.getMessage(),e);
+               }
+               log.debug("importUrl: " + importUrl.toExternalForm());
+               return importUrl;
+       }
+
+       /**
+        * @see WSDLLocatorImpl.getImportInputSource
+        */
+       public InputSource getImportInputSource(String parentLocation, String 
importLocation)
+       {
+               InputSource inputSource = null;
+               try {
+                       URL importUrl = constructImportUrl(parentLocation, 
importLocation);
+                       InputStream inputStream = importUrl.openStream();
+                       inputSource = new InputSource(inputStream);
+                       latestImportURI = importUrl.toExternalForm();
+               } catch (Exception e) {
+                       log.error(e.getMessage(),e);
+               }
+               return inputSource;
+       }
+       /**
+        * @see WSDLLocatorImpl.getBaseURI
+        */
+       public String getBaseURI() {
+               String baseURIStr = null;
+               try {
+                       baseURIStr = baseURI.toURL().toExternalForm();
+               } catch (IOException e) {
+                       log.error(e.getMessage(),e);
+               }
+               return baseURIStr;
+       }
+       /**
+        * @see WSDLLocatorImpl.getLatestImportURI
+        */
+       public String getLatestImportURI() {
+               return latestImportURI;
+       }
+       /**
+        * @see WSDLLocatorImpl.close
+        */
        public void close() {
-               if (is!=null)
+               if (inputStream!=null) {
                        try {
-                               is.close();
+                               inputStream.close();
                        } catch (IOException e) {
                                log.error(e.getMessage(),e);
                        }
-               
+               }
        }
-   }
+
+}
 

Added: 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/WSDLLocatorTest.java
URL: 
http://svn.apache.org/viewvc/juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/WSDLLocatorTest.java?rev=1138696&view=auto
==============================================================================
--- 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/WSDLLocatorTest.java
 (added)
+++ 
juddi/trunk/juddi-client/src/test/java/org/apache/juddi/v3/client/mapping/WSDLLocatorTest.java
 Wed Jun 22 23:46:11 2011
@@ -0,0 +1,91 @@
+/*
+ * 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.io.File;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.juddi.v3.client.ClassUtil;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:[email protected]";>Kurt T Stam</a>
+ */
+public class WSDLLocatorTest {
+
+       @Test
+       public void testResolveAbsoluteURL() {
+               WSDLLocatorImpl wsdlLocatorImpl = new WSDLLocatorImpl(null);
+               URL url = wsdlLocatorImpl.constructImportUrl(null, 
"http://localhost/file.wsdl";);
+               Assert.assertEquals("http://localhost/file.wsdl";, 
url.toExternalForm());
+       }
+       
+       @Test
+       public void testResolveAbsoluteURL2() {
+               WSDLLocatorImpl wsdlLocatorImpl = new WSDLLocatorImpl(null);
+               URL url = wsdlLocatorImpl.constructImportUrl("should get 
ignored", "http://localhost/file.wsdl";);
+               Assert.assertEquals("http://localhost/file.wsdl";, 
url.toExternalForm());
+       }
+       
+       @Test
+       public void testResolveInCurrentDir() {
+               WSDLLocatorImpl wsdlLocatorImpl = new WSDLLocatorImpl(null);
+               URL wsdlFile = ClassUtil.getResource("wsdl/HelloWorld.wsdl", 
this.getClass());
+               if (wsdlFile==null) Assert.fail("Can not find HelloWorld.wsdl 
file");
+               String wsdlFileStr = wsdlFile.toExternalForm();
+               String wsdlDir = 
wsdlFileStr.substring(0,wsdlFileStr.lastIndexOf(File.separator));
+               URL url = wsdlLocatorImpl.constructImportUrl(wsdlFileStr, 
"child.wsdl");
+               Assert.assertEquals(wsdlDir + "/child.wsdl", 
url.toExternalForm());
+       }
+       
+       @Test
+       public void testResolveInCurrentDir2() {
+               WSDLLocatorImpl wsdlLocatorImpl = new WSDLLocatorImpl(null);
+               URL wsdlFile = ClassUtil.getResource("wsdl/HelloWorld.wsdl", 
this.getClass());
+               if (wsdlFile==null) Assert.fail("Can not find HelloWorld.wsdl 
file");
+               String wsdlFileStr = wsdlFile.toExternalForm();
+               String wsdlDir = 
wsdlFileStr.substring(0,wsdlFileStr.lastIndexOf(File.separator));
+               URL url = wsdlLocatorImpl.constructImportUrl(wsdlFileStr, 
"./child.wsdl");
+               Assert.assertEquals(wsdlDir + "/child.wsdl", 
url.toExternalForm());
+       }
+       
+       @Test
+       public void testResolveInParentDir() {
+               WSDLLocatorImpl wsdlLocatorImpl = new WSDLLocatorImpl(null);
+               URL wsdlFile = ClassUtil.getResource("wsdl/HelloWorld.wsdl", 
this.getClass());
+               if (wsdlFile==null) Assert.fail("Can not find HelloWorld.wsdl 
file");
+               String wsdlFileStr = wsdlFile.toExternalForm();
+               String wsdlDir = 
wsdlFileStr.substring(0,wsdlFileStr.lastIndexOf("/"));
+               wsdlDir = wsdlDir.substring(0, wsdlDir.lastIndexOf("/"));
+               URL url = wsdlLocatorImpl.constructImportUrl(wsdlFileStr, 
"../child.wsdl");
+               Assert.assertEquals(wsdlDir + "/child.wsdl", 
url.toExternalForm());
+       }
+       
+       @Test
+       public void testResolveInParentDir2() throws URISyntaxException {
+               WSDLLocatorImpl wsdlLocatorImpl = new WSDLLocatorImpl(null);
+               URL wsdlFile = ClassUtil.getResource("wsdl/HelloWorld.wsdl", 
this.getClass());
+               if (wsdlFile==null) Assert.fail("Can not find HelloWorld.wsdl 
file");
+               String wsdlFileStr = wsdlFile.toExternalForm();
+               String wsdlDir = 
wsdlFileStr.substring(0,wsdlFileStr.lastIndexOf("/"));
+               wsdlDir = wsdlDir.substring(0, wsdlDir.lastIndexOf("/"));
+               wsdlDir = wsdlDir.substring(0, wsdlDir.lastIndexOf("/"));
+               URL url = wsdlLocatorImpl.constructImportUrl(wsdlFileStr, 
"../../child.wsdl");
+               Assert.assertEquals(wsdlDir + "/child.wsdl", 
url.toExternalForm());
+       }
+       
+}



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

Reply via email to