owenb 2002/09/27 05:41:44
Added: java/src/org/apache/wsif/wsdl
AuthenticatingProxyWSDLLocatorImpl.java
Log:
New java.wsdl.xml.WSDLLocator implementation which allows a wsdl document
to be retrived through an authenticating proxy.
Revision Changes Path
1.1
xml-axis-wsif/java/src/org/apache/wsif/wsdl/AuthenticatingProxyWSDLLocatorImpl.java
Index: AuthenticatingProxyWSDLLocatorImpl.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "WSIF" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.wsif.wsdl;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import javax.wsdl.WSDLException;
/**
* Implementation of javax.wsdl.xml.WSDLLocator. This class can be used to
* locate a wsdl document behind an authenticating proxy. Only http and ftp
* urls for the wsdl location are supported.
*
* @author Owen Burroughs <[EMAIL PROTECTED]>
*/
public class AuthenticatingProxyWSDLLocatorImpl implements
javax.wsdl.xml.WSDLLocator {
private static final String PROXY_AUTH="Proxy-Authorization";
private static final char[] BASE64_CHARS = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
private static final char BASE64_PAD_CHAR = '=';
Reader baseReader = null;
Reader importReader = null;
String documentBase = "";
String importBase = "";
String wsdlLocation = "";
String username = null;
String password = null;
String authString = null;
/**
* Create an instance of WSIFWSDLLocatorImpl.
* @param wsdlLoc The uri for the base wsdl document
* @param un The username for proxy authentication
* @param passed The password for proxy authentication
*/
public AuthenticatingProxyWSDLLocatorImpl(String wsdlLoc, String un, String
passwd)
throws WSDLException {
if (wsdlLoc == null
|| (wsdlLoc.indexOf("http://") == -1
&& wsdlLoc.indexOf("ftp://") == -1)) {
throw new WSDLException(
WSDLException.OTHER_ERROR,
"Base wsdl location type not supported. The
AuthenticatingProxyWSDLLocatorImpl class "
+ "only supports http and ftp urls for base wsdl locations");
}
this.wsdlLocation = wsdlLoc;
this.username = un;
this.password = passwd;
}
/**
* Get a reader for the base wsdl document. Returns null if the document
* cannot be located.
* @return The reader or null if the import cannot be resolved
*/
public Reader getBaseReader() {
if (baseReader == null) {
try {
URL url = new URL(wsdlLocation);
URLConnection con = url.openConnection();
createAuthString();
if (authString != null) {
con.setRequestProperty(PROXY_AUTH, authString);
}
InputStream in = con.getInputStream();
if (in != null) {
baseReader = new InputStreamReader(in);
}
if (url != null)
documentBase = url.toString();
} catch (Exception e) {
documentBase = wsdlLocation;
}
}
return baseReader;
}
/**
* Get a reader for an imported wsdl document. Returns null if the import
document
* cannot be located.
* @param base The document base uri for the parent wsdl document
* @param relativeLocation The relative uri of the import wsdl document
* @return The reader or null if the import cannot be resolved
*/
public Reader getImportReader(String base, String relativeLocation) {
try {
URL contextURL = (base != null) ? new URL(base) : null;
URL url = new URL(contextURL, relativeLocation);
URLConnection con = url.openConnection();
createAuthString();
if (authString != null) {
con.setRequestProperty(PROXY_AUTH, authString);
}
InputStream in = con.getInputStream();
if (in != null) {
importReader = new InputStreamReader(in);
}
importBase = (url == null) ? relativeLocation : url.toString();
} catch (Exception e2) {
// we can't find the import so set the base as an empty string
importBase = "";
}
return importReader;
}
/**
* Get the document base uri for the base wsdl document
* @return The document base uri
*/
public String getBaseURI() {
return documentBase;
}
/**
* Get the document base uri for the last import document to be resolved
* by this locator. This is useful if resolving imports within imports.
* @return The document base uri
*/
public String getLatestImportURI() {
return importBase;
}
/**
* Generate the proxy authentication header value. Base64 encoding
* code is based on that from Apache Axis and Apache SOAP, written
* by TAMURA Kent <[EMAIL PROTECTED]>
*/
private void createAuthString() {
// Don't recreate the String if it has already been created
if (authString != null)
return;
// Unless all information is provided we can't create the String
if (username == null || password == null)
return;
byte[] data = null;
try {
data = (username + ":" + password).getBytes("8859_1");
} catch (UnsupportedEncodingException uee) {
return;
}
int len = data.length;
char[] out = new char[len / 3 * 4 + 4];
int readIndex = 0;
int writeIndex = 0;
int remainingBytes = len;
while (remainingBytes >= 3) {
int i =
((data[readIndex] & 0xff) << 16)
+ ((data[readIndex + 1] & 0xff) << 8)
+ (data[readIndex + 2] & 0xff);
out[writeIndex++] = BASE64_CHARS[i >> 18];
out[writeIndex++] = BASE64_CHARS[(i >> 12) & 0x3f];
out[writeIndex++] = BASE64_CHARS[(i >> 6) & 0x3f];
out[writeIndex++] = BASE64_CHARS[i & 0x3f];
readIndex += 3;
remainingBytes -= 3;
}
// Deal with adding padding characters if the number of bytes in the data
// array was not exactly divisible by 3.
if (remainingBytes == 1) {
int i = data[readIndex] & 0xff;
out[writeIndex++] = BASE64_CHARS[i >> 2];
out[writeIndex++] = BASE64_CHARS[(i << 4) & 0x3f];
out[writeIndex++] = BASE64_PAD_CHAR;
out[writeIndex++] = BASE64_PAD_CHAR;
} else if (remainingBytes == 2) {
int i =
((data[readIndex] & 0xff) << 8) + (data[readIndex + 1] & 0xff);
out[writeIndex++] = BASE64_CHARS[i >> 10];
out[writeIndex++] = BASE64_CHARS[(i >> 4) & 0x3f];
out[writeIndex++] = BASE64_CHARS[(i << 2) & 0x3f];
out[writeIndex++] = BASE64_PAD_CHAR;
}
String encoded = new String(out, 0, writeIndex);
authString = "Basic " + encoded;
}
/**
* Close any Reader objects that have been created
* @throws IOException If a call to close() on one of the Reader objects fails
*/
public void close() throws IOException {
if (baseReader != null) baseReader.close();
if (importReader != null) importReader.close();
}
}