Professional Documents
Culture Documents
TCS Public
Web Services
6. Conclusion
Internal Use
Web Services
Internal Use
Web Services
Internal Use
Web Services
Internal Use
Web Services
3.2 Simple Object Protocol Simple Object Access Protocol (SOAP) is a protocol for initiating conversations with a UDDI Service. SOAP makes object access simple by allowing applications to invoke object methods or functions, residing on remote servers. A SOAP application creates a request block in XML, supplying the data needed by the remote method as well as the location of the remote object itself. 3.3 WSDL Web Service Description Language (WSDL), the proposed standard for how a Web service is described, is an XML-based service IDL (Interface Definition Language) that defines the service interface and its implementation characteristics. WSDL is referenced by UDDI entries and describes the SOAP messages that define a particular Web service. 3.4 E-Business XML ebXML (e-business XML) defines core components, business processes, registry and repository, messaging services, trading partner agreements, and security.
A service provider creates a Web service The service provider uses WSDL to describe the service to a UDDI registry The service provider registers the service in a UDDI registry and/or ebXML registry/repository.
Another service or consumer locates and requests the registered service by querying UDDI and/or ebXML registries.
The requesting service or user writes an application to bind the registered service using SOAP in the case of UDDI and/or ebXML
Internal Use
Web Services
Write an endpoint implementation class. Compile the endpoint implementation class. Optionally generate portable artifacts required for web service execution. Package the web service as a WAR files and deploy it.
@WebMethod(operationName="add", action="urn:Add") public int add(int i, int j) { int k = i +j ; System.out.println(i + "+" + j +" = " + k);
return k;
Internal Use
Web Services
} }
Client Side Class package client; import javax.xml.ws.WebServiceRef; import com.techtip.jaxws.sample.CalculatorService; import com.techtip.jaxws.sample.Calculator;
public static void main(String[] args) { try { JAXWSClient client = new JAXWSClient(); client.doTest(args); } catch(Exception e) { e.printStackTrace(); } }
public void doTest(String[] args) { try { System.out.println( " Retrieving port from the service " + service);
Internal Use
Web Services
Calculator port = service.getCalculatorPort(); System.out.println( " Invoking add operation on the calculator port"); for (int i=0;i>10;i++) { int ret = port.add(i, 10); if(ret != (i + 10)) { System.out.println("Unexpected greeting " + ret); return; } System.out.println( " Adding : " + i + " + 10 = " + ret); } } catch(Exception e) { e.printStackTrace(); } }
Output :
runtest-jaxws: Executing appclient with client class as client.JAXWSClient ] Retrieving port from the service com.techtip.jaxws.sample.CalculatorService@162522b Invoking add operation on the calculator port Adding : 0 + 10 = 10 Adding : 1 + 10 = 11 Adding : 2 + 10 = 12 Adding : 3 + 10 = 13 Adding : 4 + 10 = 14 Adding: 5 + 10 = 15 Adding : 6 + 10 = 16
Internal Use
Web Services
public class Client { public static void main(String [] args) { try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPBody body = message.getSOAPBody(); Name bodyName = soapFactory.createName("getActiveStations", "", "urn:ActiveStations"); body.addBodyElement(bodyName); System.out.print("\nPrinting the message that is being sent: \n\n"); message.writeTo(System.out); System.out.println("\n\n"); URL endpoint = new URL ("http://opendap.coops.nos.noaa.gov/axis/services/ActiveStations"); SOAPMessage response = connection.call(message, endpoint); connection.close(); System.out.println("\n\nIterating through the response object to get the values:\n\n"); SOAPBody responseBody = response.getSOAPBody(); //Checking for errors if (responseBody.hasFault()) { SOAPFault newFault = responseBody.getFault(); System.out.println("SAOP FAULT:\n"); System.out.println("code = " + newFault.getFaultCodeAsName());
Internal Use
10
Web Services
System.out.println("message = " + newFault.getFaultString()); System.out.println("actor = " + newFault.getFaultActor()); }else{ Iterator iterator = responseBody.getChildElements(); Iterator iterator2, iterator3, iterator4, iterator5; String tagName = null; SOAPElement se = null; Name attributeName = null; if (iterator.hasNext()){ se = (SOAPElement)iterator.next();//Stations Tag iterator2 = se.getChildElements(); se = (SOAPElement)iterator2.next();//Station Tag iterator2 = se.getChildElements(); while (iterator2.hasNext()){ se = (SOAPElement)iterator2.next(); tagName = se.getElementName().getLocalName(); System.out.println("\n\n********************************************* *********"); System.out.println("Station Name\t ID\t\t Latitude\t Longitude\t State\t Date"); if(tagName!=null && tagName.equals("station")){ //attributes for a station attributeName = soapFactory.createName("name"); System.out.print(se.getAttributeValue(attributeName));//Station Name attributeName = soapFactory.createName("ID"); System.out.print("\t" + se.getAttributeValue(attributeName));//Station ID iterator3 = se.getChildElements(); se = (SOAPElement)iterator3.next();//Metadata Tag iterator4 = se.getChildElements(); se = (SOAPElement)iterator4.next();//Location Tag iterator5 = se.getChildElements(); while (iterator5.hasNext()){ se = (SOAPElement)iterator5.next(); tagName = se.getElementName().getLocalName(); if(tagName!=null){ //The name of the tag in this case is 'lat' if(tagName.equals("lat")){ System.out.print("\t\t" + se.getValue());//Latitude }else if(tagName.equals("long")){ System.out.print("\t" + se.getValue()); //longitude
Internal Use
11
Web Services
} else if(tagName.equals("state")){ System.out.print("\t" + se.getValue()); //State } } } se = (SOAPElement)iterator4.next();//Date tagName = se.getElementName().getLocalName(); if(tagName!=null){ System.out.println("\t" + se.getValue()); } System.out.println("Parameters\t DCP\t SensorId\t Status"); while (iterator3.hasNext()){ se = (SOAPElement)iterator3.next(); attributeName = soapFactory.createName("name"); System.out.print(se.getAttributeValue(attributeName)); attributeName = soapFactory.createName("DCP"); System.out.print("\t" + se.getAttributeValue(attributeName)); attributeName = soapFactory.createName("sensorID"); System.out.print("\t" + se.getAttributeValue(attributeName)); attributeName = soapFactory.createName("status"); System.out.println("\t" + se.getAttributeValue(attributeName)); } } } } } } catch (SOAPException e) { System.err.println("ERROR: ******* " + e.toString()); } catch (IOException io) { System.err.println("ERROR: ******* " + io.toString()); } } }
Output
/**************************************************** SAMPLE RUN >java Client Sample Output (The entire output is omitted for it is too long) ****************************************************** Station Name ID Latitude Longitude State Date Nikiski 9455760 60 41.0 N 151 23.9 W 1971-05-21 Parameters Winds DCP 1 SensorId C1 Status 1
AK
Internal Use
12
Web Services
1 1 1 1
D1 E1 F1 NT
1 0 1 1
****************************************************** Station Name ID Latitude Longitude State Date Anchorage 9455920 61 14.3 N 149 53.4 W AK 1964-04-24 Parameters Winds Air Temp Water Temp Air Pressure Water Level DCP 1 1 1 1 1 SensorId C1 D1 E1 F1 NT Status 1 1 1 1 1
********************************************************/
Conclusion
Web Services Plays a vital role in the distributed web application by making secure machine to machine interaction .It makes more flexible and portable for application users.
Internal Use
13