Search This Blog

Tuesday, March 18, 2008

How to use WSDLReader for getting a WSDL File that needs authentication

package helloworldws;
import HTTPClient.AuthorizationInfo;
import HTTPClient.AuthorizationPrompter;
import HTTPClient.NVPair;
import java.util.Map;
import javax.wsdl.Definition;
import javax.wsdl.extensions.ExtensionRegistry;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import oracle.j2ee.ws.wsdl.xml.WSDLReaderImpl;

public class Test1 {
public Test1() {
}
public static void main(String[] args) throws Exception {
Test1 test1 = new Test1();
//--Get the WSDLFactory. You must specifiy Oracle's implementation class name
WSDLFactory wsdlFactory = WSDLFactory.newInstance("oracle.webservices.wsdl.WSDLFactoryImpl");
//--Create a reader and register the standard extensions
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
System.out.println(wsdlReader);
((WSDLReaderImpl)wsdlReader).getConnectionConfig().setAuthorizationPrompter(new MyAuthPrompter("oc4jadmin","welcome1"));
ExtensionRegistry extensionRegistry = wsdlFactory.newPopulatedExtensionRegistry();
wsdlReader.setExtensionRegistry(extensionRegistry);
//--Set a sixty-second timeout for reading the WSDL definition
System.setProperty(oracle.webservices.wsdl.WSDLFactoryImpl.WSDL_READ_TIMEOUT, "60" );
//--Read a WSDL file, including any imports
Definition def = wsdlReader.readWSDL("http://xxx.xxx.oracle.com:8888/SCRATCH-HelloWorldWS-context-root/MyWebService1SoapHttpPort?WSDL");
//--You can now explore the WSDL definition, for example,
Map services = def.getServices();
String targetNamespace = def.getTargetNamespace();
System.out.println(targetNamespace);
}
}
class MyAuthPrompter implements AuthorizationPrompter
{
private String pa_name, pa_pass;

MyAuthPrompter(String pa_name, String pa_pass)
{
this.pa_name = pa_name;
this.pa_pass = pa_pass;
}

public NVPair getUsernamePassword(AuthorizationInfo challenge, boolean forProxy) {
return new NVPair(pa_name, pa_pass);
}
}