11.01.2015 Views

Enterprise Application Integration

Enterprise Application Integration

Enterprise Application Integration

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SOAP WSDL WS with .NET WS in Java<br />

Part V<br />

Web Services<br />

133


SOAP WSDL WS with .NET WS in Java<br />

Web Services<br />

• services which communicate with clients<br />

based on messages in XML format SOAP<br />

• description of web service in XML format<br />

WSDL (Web Services Description<br />

Language)<br />

• search desired service with XML format<br />

UDDI (Universal Description, Discovery,<br />

and <strong>Integration</strong>)<br />

• potential: leading integration infrastructure<br />

(“least common denominator”)<br />

Client SOAP Server<br />

Web−Service Protocol Stack<br />

Discovery<br />

Description<br />

Packing<br />

Transmission<br />

UDDI<br />

WSDL<br />

SOAP<br />

HTTP, SMTP, FTP<br />

134


SOAP WSDL WS with .NET WS in Java<br />

1. SOAP<br />

• XML format for transmitting web-service calls and their answers<br />

SOAP Envelope<br />

SOAP Header<br />

Header Block<br />

Header Block<br />

SOAP Body<br />

Message Body<br />

136


SOAP WSDL WS with .NET WS in Java<br />

Details about SOAP<br />

• on error: special fault answer ( ˆ= exception)<br />

• header: optional access control, transaction processing,. . .<br />

• web services are typically stateless<br />

• thus: state (e.g. session id) as parameter (if needed)<br />

• clumsy XML handling is hidden by proxy classes<br />

• implementations: e.g. Apache Axis (→ Tomcat), JAX-WS<br />

137


SOAP WSDL WS with .NET WS in Java<br />

Example: SOAP Request<br />

<br />

<br />

<br />

<br />

48149<br />

<br />

<br />

<br />

(based on: E. Cerami: Web Services, O’Reilly, 2002)<br />

138


SOAP WSDL WS with .NET WS in Java<br />

Example: SOAP Answer<br />

<br />

<br />

<br />

<br />

23<br />

<br />

<br />

<br />

139


SOAP WSDL WS with .NET WS in Java<br />

SOAP Message Formats<br />

• document: SOAP body is an arbitrary XML document (default)<br />

• rpc: SOAP body contains method name and parameters<br />

• literal: data structured according to XML schema (XSD)(default)<br />

• encoded: uses predefined XML tags for usual basic types<br />

(e.g. int, double) as well as arrays and structs<br />

• typical combinations: document/literal (recommended) and<br />

rpc/encoded<br />

• no distributed object model (in contrast to e.g. CORBA)<br />

140


SOAP WSDL WS with .NET WS in Java<br />

2. WSDL<br />

• XML format for describing web services<br />

<br />

<br />

<br />

<br />

<br />

<br />

data types<br />

messages<br />

operations<br />

transmission protocols<br />

address<br />

142


SOAP WSDL WS with .NET WS in Java<br />

Example: WSDL Description of the Wheather Web Service<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

143


SOAP WSDL WS with .NET WS in Java<br />

WSDL Description of the Wheather Web Service (continued)<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

WSDL File for Weather Service<br />

<br />

<br />

<br />

<br />

<br />

144


SOAP WSDL WS with .NET WS in Java<br />

3. Web Services with .NET<br />

• web-service class inherits from<br />

System.Web.Services.WebService<br />

• it offers properties Session, <strong>Application</strong>, Context, User<br />

(→ state)<br />

• reference to code behind in .asmx-Datei<br />

<br />

• deployed in MS Internet Information Server (IIS)<br />

• annotations [WebService], [WebMethod], . . .<br />

• (e.g.) Visual Studio generates client-proxy class from WSDL<br />

description<br />

• web-service names cannot be overloaded<br />

• proxy transforms fault answer into SoapException<br />

146


SOAP WSDL WS with .NET WS in Java<br />

Example: Adder Web Service with .NET<br />

using System.Web.Services;<br />

using System.Web.Services.Protocols; ...<br />

namespace AdderService {<br />

/// This web service adds two numbers.<br />

[WebService(Namespace = "http://localhost/")]<br />

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br />

public class Adder : System.Web.Services.WebService {<br />

}<br />

}<br />

[WebMethod(Description="This web service adds two numbers.")]<br />

public int Add(int[] a) {<br />

int sum = 0;<br />

foreach(int x in a) sum += x;<br />

return sum;}<br />

147


SOAP WSDL WS with .NET WS in Java<br />

Example: Web Service Client with .NET<br />

using AdderService; ...<br />

namespace AdderClientNSp {<br />

public partial class AdderClient : System.Web.UI.Page {<br />

}<br />

}<br />

public void Add(object sender, EventArgs ev) {<br />

int[] val = new int[2];<br />

val[0] = Convert.ToInt32(x.Text);<br />

val[1] = Convert.ToInt32(y.Text);<br />

// ProjectName.WebReferenceName.ServiceClass<br />

Adderservice.AdderWebService.Adder service =<br />

new Adderservice.AdderWebService.Adder();<br />

result.Text = "Result of .NET: " + service.Add(val);<br />

Adderservice.AdderServiceEJB.AdderIFService service2 =<br />

new Adderservice.AdderServiceEJB.AdderIFService();<br />

try { result.Text += ", Java Result: " + service2.Add(val);}<br />

catch (Exception e){Console.WriteLine(e.Message);}<br />

}<br />

148


SOAP WSDL WS with .NET WS in Java<br />

Transformation of .NET Types and XML-Schema Types<br />

.NET Type XML-Schema Type<br />

System.String string<br />

System.Boolean boolean<br />

System.Int16 short<br />

System.Int32 int<br />

System.Int64 long<br />

System.Double double<br />

System.Byte unsignedbyte<br />

. . . . . .<br />

149


SOAP WSDL WS with .NET WS in Java<br />

4. Web Services in Java<br />

Example: Wheather Web Service in Java<br />

@Stateless<br />

@WebService<br />

public class WeatherService{<br />

@WebMethod<br />

public int getWeather(String zipcode) {<br />

return 23;}<br />

}<br />

• deployed (e.g. in web container Tomcat)<br />

• annotations @WebService and @WebMethod of classes and<br />

methods, respectively<br />

• precondition: stateless (!) session bean (or servlet)<br />

151


SOAP WSDL WS with .NET WS in Java<br />

Additional Annotations<br />

• annotation @OneWay for method with void result<br />

• class annotations for determining the message format:<br />

• @SOAPBinding(style= ..., use = ...)<br />

• style: SOAPBinding.Style.RPC or<br />

SOAPBinding.Style.DOCUMENT<br />

• use: SOAPBinding.Use.LITERAL or<br />

SOAPBinding.Style.ENCODED<br />

152


SOAP WSDL WS with .NET WS in Java<br />

Java Client with Dependency Injection<br />

Example: stateless session bean as client of a web service<br />

@Stateless<br />

@Resource(name="service/Adder",<br />

type ="javax.jws.WebService",<br />

mappedName="AdderService")<br />

public class AdderClient implements SomeInterface{<br />

}<br />

public int add(int x, int y){<br />

int[] val = {x,y};<br />

@WebServiceRef(name="java:comp/env/service/Adder")<br />

Adder service;<br />

return service.add(val);}<br />

}<br />

153


SOAP WSDL WS with .NET WS in Java<br />

Example: Session Bean as Web Service<br />

package ejb;<br />

import javax.jws.WebService;<br />

...<br />

@PermitAll<br />

@Stateless<br />

@Remote(AdderIF.class)<br />

@WebService(endpointInterface = "ejb.AdderIF",<br />

name = "AdderIF")<br />

@WebContext(contextRoot="/add4WS", secureWSDLAccess=false)<br />

public class Adder implements AdderIF{<br />

}<br />

public int add(int x, int y) throws RemoteException{<br />

return x+y;}<br />

154


SOAP WSDL WS with .NET WS in Java<br />

Example: Interface of Session Bean<br />

package ejb;<br />

import javax.jws.WebService;<br />

...<br />

@WebService<br />

@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)<br />

public interface AdderIF extends Remote {<br />

}<br />

// rename due to .NET naming conventions<br />

@WebMethod(operationName="Add")<br />

public int add(int x, int y) throws RemoteException;<br />

155


SOAP WSDL WS with .NET WS in Java<br />

Example: Java Servlet as Web Service Client<br />

package servlets;<br />

import webservicesEJB.*;<br />

...<br />

public class AdderServlet extends HttpServlet {<br />

protected void doGet(HttpServletRequest req, HttpServletResponse resp)<br />

throws ServletException, IOException {<br />

resp.setContentType("text/html");<br />

int x = Integer.parseInt(req.getParameter("x"));<br />

int y = Integer.parseInt(req.getParameter("y"));<br />

int result = 0;<br />

try {<br />

AdderService adderService = new AdderService();<br />

AdderServiceSOAP adder = adderService.getAdderServiceSOAP();<br />

result = adder.add(x,y);}<br />

catch (Exception e) {e.printStackTrace();}<br />

// output of result ...<br />

}}<br />

156


SOAP WSDL WS with .NET WS in Java<br />

Creating Proxy Classes<br />

• the proxy classes for accessing a web service (including .NET)<br />

generated by (e.g.) wsimport<br />

• wsimport is contained in JAX-WS<br />

• execution via command line:<br />

1. (under Windows:) Start → Execute, then Open: cmd<br />

2. in the appearing window: move to the desired directory using cd<br />

3. wsimport -Xendorsed -keep -extension <br />

4. e.g. wsimport ...<br />

http://localhost:8080/add4WS/Adderwsdl<br />

5. copy the generated directory into the src directory of the eclipse<br />

web-service client projects<br />

157

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!