130 likes | 322 Views
SOAP on a Rope. This is a subtitle. Project Overview. Understand SOAP over HTTP Understand how Axis implements SOAP services over HTTP Become SOAP/acronym-compliant: WSDL JAX-RPC WSDL2Java Java2WSDL jws Xerces and Crimson Interface with existing SOAP services
E N D
SOAP on a Rope This is a subtitle
Project Overview • Understand SOAP over HTTP • Understand how Axis implements SOAP services over HTTP • Become SOAP/acronym-compliant: • WSDL • JAX-RPC • WSDL2Java • Java2WSDL • jws • Xerces and Crimson • Interface with existing SOAP services • Interface with your own SOAP service
SOAP over HTTP • Defacto standard is to issue call via POST • XML request is the body of the POST • Content-type: “text/xml” seems to be the only mandatory header (the only common one anyway) • Any noticed variation from this is welcomed (it may be service-dependent) • GET will sometimes work; don’t count on it
Apache Axis and SOAP • Axis is a SOAP engine • Runs alone or under a servlet engine • Extensive WSDL support (really nice…) • Support for “drop in” services (jws) • TCP/IP packet monitoring tools • Sample programs to get you started
Axis as a SOAP Engine • Validates POST request • Returns WSDL for service (if requested) • You never author the WSDL for the service you wrote • Handles SOAP request • Parses the XML request (in the body of the POST) • Invokes handler code, passing correct parameters • Returns results as XML using standard schemas defined by W3C (NOT a Java object) • XML is in the body of the HTTP request • Client does what it wants with the XML • Java code
Axis as a SOAP Engine (con’t) • Deploys service in a WAR just like JSPs • It is nothing more than Java code – SoapService.jws • No need to compile it; it is compiled by Axis in the container • Errors/bugs don’t show up until you run it • No need to roll JBoss (No JNDI in this lab)
Acronym Compliance • WSDL: Web Services Description Language • XML file that describes your services and how to interface with them • JAX-RPC: Java’s libraries for abstracting SOAP • Requires you to know the expected object types and how to form them in the XML • GREAT for primitive types • Like JDBC: give it the XML “driver” and you’re going to the show • It is an XML API, nothing more
Acronym Compliance (con’t) • Xerces and Crimson • XML Parsers that are run “under-the-hood” • Axis can use crimson, but prefers Xerces • Turns Java objects into XML and XML into Java objects • Even does the correct type casting • Allows for pure java method-driven development; you never see ANY XML tags • Logically equivalent (like Tomcat and Jetty)
Acronym Compliance (part III) • WSDL2Java: One of your best friends • Axis method that parses a WSDL and creates Java stubs for you to use • Drill through the stubs to see what “setters” and “getters” to use • Results in a full API • Java2WSDL: Lazy enforcer • Takes your service and returns a WSDL for clients to use to interface with your service • http://cs462-wendel.cs.byu.edu/SoapServices/CoolSoapService.jws?wsdl returns the WSDL for my CoolSoapService (which is nothing more than Java code) • We love Axis
Acronym Compliance (part IV) • JWS: Java Web Services • Servlets in disguise (JSP revisited) • You got it: it is the handler code that composes your service • You got it again: you have everything Java has to offer at your disposal – JavaMail, RMI, EJB services, the works • Needs deployment descriptor • “drop in” .jws file needs no descriptor (wsdd) – Axis produces it for you • .jws file will be sufficient for this project
Interfacing With SOAP Services • Many ways to do it, with HTTP and Axis in hand: • Strict HTTP POST (see exampleentitybean) • Use the API created for you by WSDL2Java • Figure out their WSDL and issue via JAX-RPC
HTTP Post theURL = "http://api.google.com/search/beta2"; //this is the service’s URL . . URL = new URL(theURL); URLConnection = URL . openConnection ( ); // Establish the connection URLConnection . setDoOutput ( true ); // Set up the connection for POST URLConnection . setDoInput ( true ); // Set up the connection to read results URLConnection.setUseCaches ( false ); // Let that side know what is coming URLConnection.setRequestProperty ( "Content-Type", "text/xml; charset=utf-8;"); URLConnection.setRequestProperty ( "SOAPAction", "urn:GoogleSearchAction"); . . . // Grab the output stream and convert it to a PrintWriter OutputStream URLConnectionOutputStream = URLConnection . getOutputStream ( ); PrintWriter PrintWriter = new PrintWriter ( URLConnectionOutputStream ) ; PrintWriter . println ( xml ); PrintWriter . flush ( ); PrintWriter . close ( ); InputStream URLConnectionInputStream = URLConnection . getInputStream ( ); BufferedReader BufferedReader = new BufferedReader ( new InputStreamReader ( URLConnectionInputStream ) ) ; // Read the results of the POST String line; while ((line = BufferedReader.readLine()) != null) { result += line + "\n"; //System . out . println ( line ); } URLConnectionInputStream . close (); System.err.println( "HERE ARE THE RESULTS:\n" + result );
JAX-RPC • public class ExampleClient • { • public static void main(String [] args) throws Exception • { • Options options = new Options(args); • args = options.getRemainingArgs(); • String directive = "doSoapSearches"; • String searchterm = args[0]; • String format = args[1]; • if (args == null || args.length < 2) { • System.err.println("Usage: ExampleClient <searchterm> <xml|html>"); • return; • } • String endpoint = "http://dng6.cs.byu.edu:" + options.getPort() + • "/axis/SoapClient.jws"; • System.out.println("endpoint: " + endpoint ); • System.out.println("format: " + format ); • System.out.println("searchterm: " + searchterm ); • Service service = new Service(); • Call call = (Call) service.createCall(); • call.setTargetEndpointAddress( new java.net.URL(endpoint) ); • call.setOperationName( directive ); • call.addParameter( "searchTerm", XMLType.XSD_STRING, ParameterMode.IN ); • call.addParameter( "format", XMLType.XSD_STRING, ParameterMode.IN ); • call.setReturnType( XMLType.XSD_STRING ); • String ret = (String) call.invoke( new Object [] { searchterm, format }); • System.out.println("Got result\n: " + ret); • } • }