|
|
| |
Invoking Web Services Without Using the WSDL File
This Appendix shows an example of a dynamic client application that does not use the WSDL file when it invokes a WebLogic Web Service. In particular, the example invokes a message-style Web service and sends data to WebLogic Server.
Dynamic client applications that do not use the WSDL of the Web service are dynamic in every way, because they can invoke a Web service without knowing either the interface of the Web service, or the JavaBean interface of return values and parameters, or even the number and signatures of the methods that make up the Web service.
The example uses the URL http://www.myHost.com:7001/msg/sendMsg to invoke the Web Service. Because the example shows a dynamic client application that does not use the WSDL of the Web service, the preceding URL is for the Web service itself, rather than the URL for the WSDL of the Web service.
The procedure after the example discusses relevant sections of the example as part of the basic steps you follow to create this client.
import java.util.Properties; import java.net.URL; import javax.naming.Context; import javax.naming.InitialContext;
import weblogic.soap.WebServiceProxy; import weblogic.soap.SoapMethod; import weblogic.soap.SoapType; import weblogic.soap.codec.CodecFactory; import weblogic.soap.codec.SoapEncodingCodec; import weblogic.soap.codec.LiteralCodec;
public class ProducerClient{
public static void main( String[] arg ) throws Exception{
CodecFactory factory = CodecFactory.newInstance(); factory.register( new SoapEncodingCodec() ); factory.register( new LiteralCodec() );
WebServiceProxy proxy = WebServiceProxy.createService( new URL( "http://www.myHost.com:7001/msg/sendMsg" ) ); proxy.setCodecFactory( factory ); proxy.setVerbose( true );
SoapType param = new SoapType( "message", String.class ); proxy.addMethod( "send", null, new SoapType[]{ param } ); SoapMethod method = proxy.getMethod( "send" );
String toSend = arg.length == 0 ? "No arg to send" : arg[0]; Object result = method.invoke( new Object[]{ toSend } ); } }
Follow these steps to create a dynamic Java client that does not use WSDL to invoke a message-style WebLogic Web Service that sends data to WebLogic Server:
For detailed information on this step, refer to Downloading the Java Client JAR File from the Web Services Home Page.
CodecFactory factory = CodecFactory.newInstance(); factory.register( new SoapEncodingCodec() ); factory.register( new LiteralCodec() );
WebServiceProxy proxy = WebServiceProxy.createService( new URL( "http://www.myHost.com:7001/msg/sendMsg" ) ); proxy.setCodecFactory( factory ); proxy.setVerbose( true );
SoapType param = new SoapType( "message", String.class ); proxy.addMethod( "send", null, new SoapType[]{ param } ); SoapMethod method = proxy.getMethod( "send" );
String toSend = arg.length == 0 ? "No arg to send" : arg[0]; Object result = method.invoke( new Object[]{ toSend } );
The following more complex example shows how to use a send method that accepts a org.w3c.dom.Document, org.w3c.dom.DocumentFragment, or org.w3c.dom.Element data type as its parameter. The example shows how to set literal encoding on this flavor of the send method.
import java.util.Properties;
import java.net.URL; import java.io.File;
import javax.naming.Context; import javax.naming.InitialContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document; import org.w3c.dom.Element;
import weblogic.apache.xml.serialize.OutputFormat; import weblogic.apache.xml.serialize.XMLSerializer;
import weblogic.apache.xerces.dom.DocumentImpl;
import weblogic.soap.WebServiceProxy; import weblogic.soap.SoapMethod; import weblogic.soap.SoapType;
import weblogic.soap.codec.CodecFactory; import weblogic.soap.codec.SoapEncodingCodec; import weblogic.soap.codec.LiteralCodec;
public class ProducerClient{ public static void main(String[] args) throws Exception{ String url = "http://localhost:7001"; // Parse the arguments list if (args.length != 2) { System.out.println("Usage: java examples.webservices.message.ProducerClient http://hostname:port \"message\""); return; } else if (args.length == 2) { url = args[0]; }
CodecFactory factory = CodecFactory.newInstance(); factory.register(new SoapEncodingCodec()); factory.register(new LiteralCodec());
URL newURL = new URL(url + "/msg/sendMsg");
WebServiceProxy proxy = WebServiceProxy.createService(newURL); proxy.setCodecFactory(factory); proxy.setVerbose(true); SoapType param = new SoapType( "message", Document.class ); proxy.addMethod( "send", null, new SoapType[]{ param } );
SoapMethod method = proxy.getMethod("send");
// Print out proxy to make sure method signature looks good System.out.println("Proxy:"+proxy);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //Obtain an instance of a DocumentBuilder from the factory. DocumentBuilder db = dbf.newDocumentBuilder(); //Parse the document. Document w3cDoc = db.parse(new File("/test/fdr_nodtd.xml"));
//Class parserClass = Class.forName("org.jdom.adapters.XercesDOMAdapter"); //DOMAdapter da = (DOMAdapter)parserClass.newInstance(); //Document w3cDoc = da.getDocument(new File("/test/fdr_nodtd.xml"),false);
// Print out XML just to make sure the document was read successfully OutputFormat of = new OutputFormat(); of.setEncoding("UTF-8"); of.setLineWidth(40); of.setIndent(4); XMLSerializer xs = new XMLSerializer(System.out,of); xs.serialize(w3cDoc);
System.out.println("Before Invoke"); Object result = method.invoke( new Object[]{w3cDoc} ); System.out.println("Done"); } }
|
Copyright © 2001 BEA Systems, Inc. All rights reserved.
|