1 / 26

Web Services

Web Services.  Web Services are the basic fundamental building blocks of invoking features that can be accessed by an application program.

Download Presentation

Web Services

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Services  Web Services are the basic fundamental building blocks of invoking features that can be accessed by an application program. The accessibility to provide the features across different platforms that are developed using different languages, and can be used by any remote application, over the internet and avoid securely restrictions.

  2. SOAP web services • SOAP is essentially an XML-based protocol for invoking remote methods.(verb oriented vs. noun oriented REST WS) • Communication between the service and the application is through a standard format called XML (eXtensible Markup Language) which is universal and is accepted on any platform across the distributed network.

  3. SOAP (Simple Object Access Protocol) SOAP is a protocol based on HTTP protocol that by means of which Web Services is enabled; it is the format that is acclaimed universally due to its nature of standardization and rules that it implements. . • http://cwiki.apache.org/GMOxDOC20/simple-web-service-with-jax-ws.html

  4. UDDI (Universal Discription, Discovery and Integration): • UDDI is specifically a governing body that monitors the publication and discovery of the web services implementation with respect to message communication between the application and also at the enterprise level. • Web service is deployed on Web server (Apache Tomcat, IIS) or Application server(Java EE, Glassfish, WebLogic)

  5. Simple calculation web service (add, subtract) @webservice

  6. WSDL (Web Service Description Language): • WSDL is an XML-based language that describes the interface to SOAP services • It consists of the description about the web service, which is basically a file with .wsdl extension that you can find in your application folder.

  7. WSDL • basics: written in XML, describe web services, locate web services WSDL structure • <types> data types used by the WS • <message> messages (I/O parameters) • <portType> set of operations(Interface) • <binding> communication protocols used by the WS

  8. WSDL structure (types) WSDL structure (types) <wsdl:types> <s:schema targetNamespace="http://ws.apache.org/axis2"> <s:element name="add"> <s:complexType> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="a" type="xs:int"/> <s:element minOccurs="1" maxOccurs="1" name="b" type="xs:int"/> </s:sequence> </s:complexType> </s:element> </s:schema> ... </wsdl:types>

  9. WSDL structure (message) <wsdl:message name="addRequest"> <wsdl:part name="parameters" element="ns:add"/> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part name="parameters" element="ns:addResponse"/> </wsdl:message>

  10. WSDL structure (portType) <wsdl:portType name="CalculatorPortType"> <wsdl:operation name="add"> <wsdl:input message="ns:addRequest" wsaw:Action="urn:add"/> <wsdl:output message="ns:addResponse" wsaw:Action="urn:addResponse"/> </wsdl:operation> <wsdl:operation name="subtract"> <wsdl:input message="ns:subtractRequest" wsaw:Action="urn:subtract"/> <wsdl:output message="ns:subtractResponse" wsaw:Action="urn:subtractResponse"/> </wsdl:operation> </wsdl:portType>

  11. Simple Object Access Protocol: structureSOAP <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header> ... </soap:Header> <soap:Body> ... </soap:Body> </soap:Envelope>

  12. SOAP structure (request) <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soapenvelope" xmlns:axis="http://ws.apache.org/axis2"> <soap:Header/> <soap:Body> <axis:add> <axis:param0>45</axis:param0> <axis:param1>12</axis:param1> </axis:add> </soap:Body> </soap:Envelope>

  13. SOAP structure (response) <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soapenvelope" xmlns:axis="http://ws.apache.org/axis2"> <soap:Header/> <soap:Body> <axis:addResponse> <axis:return>57</axis:return> </axis:addResponse> </soap:Body> </soap:Envelope>

  14. A complete example of JAX-WS Webmethod int add(int a, int b), the SOAP request and response for this method might look like the following: SOAP request <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <m:add xmlns:m="http://example/"> <a>1</a> <b>2</b> </m:add> </soapenv:Body> </soapenv:Envelope>

  15. SOAP response <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <m:addResponse xmlns:m="http://example/"> <result>3</result> </m:addResponse> </soapenv:Body> </soapenv:Envelope>

  16. names a <service>, specifies a <port> (URI) at which it is available and refers to a binding for the port • <binding> specifies the style of interaction (e.g. RPC) transport protocol used (e.g. HTTP) <operation>s defined along with encodingStyle for their <input> and <output> • <portType> specifies a set of named <operation>s which refer to the <message>s used by each <operation> for <input> and <output> • a <message> describes a one-way message (request or response) consists of a number of <part>s referring to parameters or return values, each of which has a type (e.g. xsd:string) • <types> describes all data types used between client and server (XML schema is default type system)

  17. Add service WSDL <?xml version="1.0" standalone="yes"?> <definitions targetNamespace="http://example/" name="AddService" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:m="http://example/"> <types> <xsd:schema version="1.0" targetNamespace="http://example/"> <xsd:element name="add" type="m:add"/> <xsd:complexType name="add"> <xsd:sequence> <xsd:element name="a" type="xsd:int"/> <xsd:element name="b" type="xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:element name="addResponse" type="m:addResponse"/> <xsd:complexType name="addResponse"/> <xsd:sequence> <xsd:element name="result" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </types>

  18. <binding name="AddPortBinding" type="m:Add"> <soap:binding transport="http://schemas.xmlsoap.org/wsdl/soap/" style="document"/> <operation name="add"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding>

  19. Service definition <service name="AddService"> <port name="AddPort" binding="m:AddPortBinding"> <soap:address location="http://example/add-service/"/> </port> </service> </definitions>

  20. Calculator interface The Calculator interface defines the Service Endpoint Interface (SEI) for the Web Service. http://cwiki.apache.org/GMOxDOC20/simple-web-service-with-jax-ws.html Calculator.java package org.apache.geronimo.samples.jws; import javax.jws.* @WebService(name="CalculatorPortType", targetNamespace = "http://jws.samples.geronimo.apache.org") public interface Calculator { @WebMethod public int add(@WebParam(name = "value1") int value1, @WebParam(name = "value2") int value2);}

  21. The CalculatorService class implements the Web Service business logic. It implements all the methods defined in the SEI. The class does not need to implement the Calculator interface but must reference it through the @WebService.endpointInterface annotation. This class will be exposed as a Servlet through web.xml file even though it does not extend the javax.servlet.Servlet class.The context variable marked with the @Resource annotation will be injected at runtime. The WebServiceContext can be used to obtain the message context and security information relative to the call.

  22. CalculatorService.java package org.apache.geronimo.samples.jws; import javax.annotation.Resource;import javax.jws.WebService; import javax.xml.ws.WebServiceContext; @WebService(serviceName = "Calculator", portName="CalculatorPort", endpointInterface = "org.apache.geronimo.samples.jws.Calculator", targetNamespace = "http://jws.samples.geronimo.apache.org", wsdlLocation = "WEB-INF/wsdl/CalculatorService.wsdl") public class CalculatorService implements Calculator @Resource private WebServiceContext context; public int add(int value1, int value2) { System.out.println("User Principal: " + context.getUserPrincipal()); return value1 + value2; }} The web.xml descriptor is used to deploy the Web Service. If the web.xml descriptor is not provided, it will be automatically generated during deployment.

  23. JSP-based JAX-WS client The add.jsp is a basic client for the CalculatorService Web Service. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="javax.naming.InitialContext,javax.xml.ws.Service,org.apache.geronimo.samples.jws.Calculator"%> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Apache Geronimo Sample Application - JAX-WS Calculator</title> <meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type"> </head>

  24. <BODY> <font face="Verdana, Helvetica, Arial"> <form action="add.jsp"> Value 1: <input type="text" name="value1"> Value 2: <input type="text" name="value2"> <input type="submit" value="Add"> </form> <br> <% String value1 = request.getParameter( "value1" ); String value2 = request.getParameter( "value2" ); if (value1 != null && value1.trim().length() > 0 && value2 != null && value2.trim().length() > 0) { try { int v1 = Integer.parseInt(value1); int v2 = Integer.parseInt(value2); InitialContext ctx = new InitialContext(); Service service = (Service)ctx.lookup("java:comp/env/services/Calculator"); Calculator calc = service.getPort(Calculator.class); int sum = calc.add(v1, v2); out.println("Result: " + v1 + " + " + v2 + " = " + sum); } catch ( Exception e ) { out.println("Error: " + e.getMessage()); } ); }%> </body> </html>

  25. <message name="add"> <part name="parameters" element="m:add"/> </message> <message name="addResponse"> <part name="parameters" element="m:addResponse"/> </message> <portType name="Add"> <operation name="add"> <input message="m:add"/> <output message="m:addResponse"/> </operation> </portType>

More Related