You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by to...@apache.org on 2002/05/07 21:52:02 UTC
cvs commit: xml-axis/java/src/org/apache/axis/wsdl/toJava SymbolTable.java Emitter.java
tomj 02/05/07 12:52:02
Modified: java/src/org/apache/axis/utils XMLUtils.java
java/src/org/apache/axis/wsdl WSDL2Java.java
java/src/org/apache/axis/wsdl/toJava SymbolTable.java
Emitter.java
Log:
- Correctly support username/password protected WSDL files in Emitter.
XMLUtils.newDocument now takes a username and password, if they are
set and we are using the http or https protocol, we go and fetch the document
ourselves so we can provide the HTTP Basic Authentication header
- Set the global Authenticator only if WSDL2Java is invoked from the
command line. Since this is a global setting, programs that embed Axis
would be hosed if they used this mechanism.
- Rationalize the SymbolTable getURL() function.
Do not try and open the URL, this will fail with a password
protected URL and later code will fail if the URL is missing.
Revision Changes Path
1.48 +96 -6 xml-axis/java/src/org/apache/axis/utils/XMLUtils.java
Index: XMLUtils.java
===================================================================
RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/XMLUtils.java,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -r1.47 -r1.48
--- XMLUtils.java 1 May 2002 20:54:04 -0000 1.47
+++ XMLUtils.java 7 May 2002 19:52:02 -0000 1.48
@@ -56,10 +56,8 @@
package org.apache.axis.utils ;
import org.apache.axis.Constants;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
import org.w3c.dom.Attr;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
@@ -72,8 +70,7 @@
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
-import org.xml.sax.helpers.DefaultHandler;
-
+import sun.misc.BASE64Encoder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -85,6 +82,10 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
import java.util.Stack;
public class XMLUtils {
@@ -252,7 +253,22 @@
}
public static Document newDocument(String uri) {
- return XMLUtils.newDocument(new InputSource(uri));
+ // call the authenticated version as there might be
+ // username/password info embeded in the uri.
+ return XMLUtils.newDocument(uri, null, null);
+ }
+
+ /**
+ * Create a new document from the given URI, use the username and password
+ * if the URI requires authentication.
+ */
+ public static Document newDocument(String uri, String username, String password) {
+ try {
+ return XMLUtils.newDocument(XMLUtils.getInputSourceFromURI(uri, username, password));
+ } catch (Exception e) {
+ log.error(JavaUtils.getMessage("exception00"), e);
+ }
+ return null;
}
private static String privateElementToString(Element element,
@@ -471,4 +487,78 @@
throw new SAXException(message);
}
}
- }
+
+
+ /**
+ * Utility to get the bytes at a protected uri
+ *
+ * This will retrieve the URL if a username and password are provided.
+ * The java.net.URL class does not do Basic Authentication, so we have to
+ * do it manually in this routine.
+ *
+ * If no username is provided, we create an InputSource from the uri
+ * and let the InputSource go fetch the contents.
+ *
+ * @param uri the resource to get
+ * @param username basic auth username
+ * @param password basic auth password
+ */
+ private static InputSource getInputSourceFromURI(String uri,
+ String username,
+ String password)
+ throws Exception {
+
+ URL wsdlurl = null;
+ try {
+ wsdlurl = new URL(uri);
+ } catch (MalformedURLException e) {
+ // we can't process it, it might be a 'simple' foo.wsdl
+ // let InputSource deal with it
+ return new InputSource(uri);
+ }
+
+ // if no authentication, just let InputSource deal with it
+ if (username == null && wsdlurl.getUserInfo() == null) {
+ return new InputSource(uri);
+ }
+
+ // if this is not an HTTP{S} url, let InputSource deal with it
+ if (!wsdlurl.getProtocol().startsWith("http")) {
+ return new InputSource(uri);
+ }
+
+ URLConnection connection = wsdlurl.openConnection();
+ // Does this work for https???
+ if (!(connection instanceof HttpURLConnection)) {
+ // can't do http with this URL, let InputSource deal with it
+ return new InputSource(uri);
+ }
+ HttpURLConnection uconn = (HttpURLConnection) connection;
+ String userinfo = wsdlurl.getUserInfo();
+ uconn.setRequestMethod("GET");
+ uconn.setAllowUserInteraction(false);
+ uconn.setDefaultUseCaches(false);
+ uconn.setDoInput(true);
+ uconn.setDoOutput(false);
+ uconn.setInstanceFollowRedirects(true);
+ uconn.setUseCaches(false);
+ BASE64Encoder enc = new BASE64Encoder();
+ // username/password info in the URL overrides passed in values
+ if (userinfo != null) {
+ uconn.setRequestProperty("Authorization",
+ "Basic " + enc.encode(userinfo.getBytes("ISO-8859-1")));
+ } else if (username != null) {
+ String auth = username;
+ if (password != null) {
+ auth = username + ":" + password;
+ }
+ uconn.setRequestProperty("Authorization",
+ "Basic " +
+ enc.encode((auth).getBytes("ISO-8859-1")));
+ }
+ uconn.connect();
+
+ return new InputSource(uconn.getInputStream());
+ }
+
+}
1.26 +34 -25 xml-axis/java/src/org/apache/axis/wsdl/WSDL2Java.java
Index: WSDL2Java.java
===================================================================
RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/wsdl/WSDL2Java.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- WSDL2Java.java 7 May 2002 16:08:50 -0000 1.25
+++ WSDL2Java.java 7 May 2002 19:52:02 -0000 1.26
@@ -114,10 +114,6 @@
public static final byte REQUEST_SCOPE = 0x10;
public static final byte SESSION_SCOPE = 0x11;
- // Username and password for Authentication
- protected String username = null;
- protected String password = null;
-
// The emitter framework Emitter class.
protected Emitter emitter;
// Timeout, in milliseconds, to let the Emitter do its work
@@ -427,19 +423,19 @@
}
public String getUsername() {
- return username;
+ return emitter.getUsername();
}
public void setUsername(String username) {
- this.username = username;
+ emitter.setUsername(username);
}
public String getPassword() {
- return password;
+ return emitter.getPassword();
}
public void setPassword(String password) {
- this.password = password;
+ emitter.setPassword(password);
}
//
@@ -451,7 +447,7 @@
* Returns an object which contains of information on all generated files
* including the class name, filename and a type string.
*
- * @return A org.apache.axis.wsdl.toJava.GeneratedFileInfo object
+ * @return An org.apache.axis.wsdl.toJava.GeneratedFileInfo object
* @see org.apache.axis.wsdl.toJava.GeneratedFileInfo
*/
public GeneratedFileInfo getGeneratedFileInfo()
@@ -489,8 +485,6 @@
public void emit(String wsdlURL)
throws Exception {
- Authenticator.setDefault(new DefaultAuthenticator(this));
-
// We run the actual Emitter in a thread that we can kill
WSDLRunnable runnable = new WSDLRunnable(emitter, wsdlURL);
Thread wsdlThread = new Thread(runnable);
@@ -690,14 +684,23 @@
printUsage();
}
+ // set the namespace map if provided
if (!namespaceMap.isEmpty()) {
wsdl2java.setNamespaceMap(namespaceMap);
}
-
+
+ // Set type mapping version
+ wsdl2java.setTypeMappingVersion(typeMappingVersion);
+
// Set username and password if provided in URL
wsdl2java.checkForAuthInfo(wsdlURI);
- wsdl2java.setTypeMappingVersion(typeMappingVersion);
+ // register a default authenticator to handle proxys
+ Authenticator.setDefault(
+ new DefaultAuthenticator(wsdl2java.getUsername(),
+ wsdl2java.getPassword()));
+
+ // Do the work
wsdl2java.emit(wsdlURI);
// everything is good
@@ -791,6 +794,10 @@
System.exit(1);
}
+ /**
+ * Extract the username and password info (if any) from a URL
+ *
+ */
private void checkForAuthInfo(String uri) {
URL url = null;
try {
@@ -803,26 +810,28 @@
if (userInfo != null) {
int i = userInfo.indexOf(':');
if (i >= 0) {
- this.username = userInfo.substring(0,i);
- this.password = userInfo.substring(i+1);
+ setUsername(userInfo.substring(0,i));
+ setPassword(userInfo.substring(i+1));
} else {
- this.username = userInfo;
+ setUsername(userInfo);
}
}
}
- private class DefaultAuthenticator extends Authenticator {
- private WSDL2Java wsdl2java;
+ /**
+ * This class is used by WSDL2Java main() only
+ * Supports the http.proxyUser and http.proxyPassword properties.
+ */
+ public static class DefaultAuthenticator extends Authenticator {
+ private String user;
+ private String password;
- DefaultAuthenticator(WSDL2Java wsdl2java) {
- this.wsdl2java = wsdl2java;
+ DefaultAuthenticator(String user, String pass) {
+ this.user = user;
+ this.password = pass;
}
protected PasswordAuthentication getPasswordAuthentication() {
- // First check command line options
- String user = wsdl2java.getUsername();
- String password = wsdl2java.getPassword();
-
- // if we didn't get them, check the system properties
+ // if user and password weren't provided, check the system properties
if (user == null) {
user = System.getProperty("http.proxyUser","");
}
1.56 +15 -29 xml-axis/java/src/org/apache/axis/wsdl/toJava/SymbolTable.java
Index: SymbolTable.java
===================================================================
RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/wsdl/toJava/SymbolTable.java,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -r1.55 -r1.56
--- SymbolTable.java 1 May 2002 17:17:15 -0000 1.55
+++ SymbolTable.java 7 May 2002 19:52:02 -0000 1.56
@@ -462,46 +462,32 @@
private static URL getURL(URL contextURL, String spec) throws IOException {
URL url = null;
try {
+ // See if we have a good URL
url = new URL(contextURL, spec);
-
- try {
- url.openStream();
- }
- catch (IOException ioe) {
- throw new MalformedURLException();
- }
}
catch (MalformedURLException me)
{
- url = new URL("file", "", spec);
-
- try {
- url.openStream();
- }
- catch (IOException ioe) {
- if (contextURL != null) {
- String contextFileName = contextURL.getFile();
- String parentName = new File(contextFileName).getParent();
- if (parentName != null) {
- url = new URL(new URL("file", "", parentName + '/'),
- spec);
- try {
- url.openStream();
- }
- catch (IOException secondioe) {
- throw new FileNotFoundException(url.toString());
- }
- }
- else {
- throw new FileNotFoundException(url.toString());
- }
+ if (contextURL == null) {
+ url = new URL("file", "", spec);
+ } else {
+ // get the parent directory of the contextURL, and append
+ // the spec string to the end.
+ String contextFileName = contextURL.getFile();
+ String parentName = new File(contextFileName).getParent();
+ if (parentName != null) {
+ url = new URL(new URL("file", "", parentName + '/'), spec);
}
else {
throw new FileNotFoundException(url.toString());
}
}
}
+
+ // Everything is OK with this URL, although a file url constructed
+ // above may not exist. This will be caught later when the URL is
+ // accessed.
return url;
+
} // getURL
/**
* Recursively find all xsd:import'ed objects and call populate for each one.
1.34 +33 -1 xml-axis/java/src/org/apache/axis/wsdl/toJava/Emitter.java
Index: Emitter.java
===================================================================
RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/wsdl/toJava/Emitter.java,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- Emitter.java 7 May 2002 16:08:50 -0000 1.33
+++ Emitter.java 7 May 2002 19:52:02 -0000 1.34
@@ -114,6 +114,8 @@
protected String currentWSDLURI = null;
protected String NStoPkgFilename = "NStoPkg.properties";
protected File NStoPkgFile = null;
+ protected String username = null;
+ protected String password = null;
/**
@@ -149,7 +151,8 @@
if (bVerbose)
System.out.println(JavaUtils.getMessage("parsing00", uri));
- Document doc = XMLUtils.newDocument(uri);
+ Document doc = XMLUtils.newDocument(uri, username, password);
+
if (doc == null) {
throw new IOException(JavaUtils.getMessage("cantGetDoc00", uri));
}
@@ -462,6 +465,33 @@
this.NStoPkgFile = NStoPkgFile;
} // setNStoPkg
+ /**
+ * Get the username needed to access the WSDL
+ */
+ public String getUsername() {
+ return username;
+ }
+
+ /**
+ * Set the username needed to access the WSDL
+ */
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ /**
+ * Get the password needed to access the WSDL
+ */
+ public String getPassword() {
+ return password;
+ }
+
+ /**
+ * Set the username needed to access the WSDL
+ */
+ public void setPassword(String password) {
+ this.password = password;
+ }
///////////////////////////////////////////////////
//
// Implementation
@@ -536,6 +566,8 @@
public String getWSDLURI() {
return currentWSDLURI;
}
+
+
//
// Utility methods