J2EE Web Services. J2EE Web Services , Richard Monson-Haefel, Addison-Wesley, 2004 Enterprise Java Beans, 4 th Edition , Richard Monson-Haefel, O’Reilly, 2004. J2EE/JAX-RPC Web Services. Two main interfaces Service interface acts as a factory for endpoint stub extends javax.xml.rpc.Service
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
F I N
Plus de contenu associé
Transcription de la présentation
J2EE Web Services J2EE Web Services, Richard Monson-Haefel, Addison-Wesley, 2004 Enterprise Java Beans, 4th Edition, Richard Monson-Haefel, O’Reilly, 2004 J2EE Web Services
J2EE/JAX-RPC Web Services • Two main interfaces • Service interface • acts as a factory for endpoint stub • extends javax.xml.rpc.Service • Endpoint interface • provides operations • Two types of endpoints • Java Service/Servlet Endpoints (JSE) • deployed within a J2EE Web Container • implemented as a normal class, but operates within a Servlet • EJB Endpoints • deployed within an J2EE EJB Container • implemented as a Stateless Session EJB J2EE Web Services
Major J2EE/JAX-RPC Elements • WSDL • implementation/platform neutral definition of Service/Endpoint interface • Service/Endpoint Java Interfaces • client or server-specific Java classes that are used by client or server implementation • can be statically defined during development or dynamically generated at runtime • J2EE Containers • provides a standard deployment structure • deploy Web Service/Endpoint implementations and/or their clients • generate server-side skeletons and client-side stubs for participants • provide normal WEB and/or EJB environment for client/server participants to operate • Deployment Descriptors • define the web service to either the client or server J2EE Container • identify the WSDL and Java classes relevant to a service • provide a mapping between the WSDL and Java elemments J2EE Web Services
J2EE/JAX-RPC Environment WSDL Document defines the schema for XML-based messages Web Container JAX-RPC Servlet JSE Endpoint HTTP 1.1/SOAP Web Service Client Web Container EJB Container HTTP 1.1/SOAP JAX-RPC Servlet EJB Endpoint JAX-RPC Servlet is notional/probable for EJB Endpoints, but not required J2EE Web Services
WSDL The Web Services Description Language J2EE Web Services
WSDL Overview • Web Services Description Language (WSDL) • Uses XMLSchema to describe a web service • Describes service • Input/output • Message encoding • Transport required • Address information • Initially developed by IBM, Ariba, Microsoft • Given a WSDL description, automated tools can generate stubs to call the service J2EE Web Services
WSDL (Cont) • WSDL is a generic mechanism that can describe web services that are implemented in any protocol i.e. it is abstract • The WS-I basic Profile 1.0 endorses SOAP 1.1 over HTTP J2EE Web Services
WSDL and JAX/RPC • Generated Stubs • Network Stubs are generated at deployment time by a JAX-RPC compiler • WSDL portType => Endpoint Interface • WSDL binding and port => Stub • Endpoint Interface • Java RMI Remote interface (extends java.rmi.Remote) • Interface Name comes from portType name attribute J2EE Web Services
WSDL Structure <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://wsj2ee.corej2ee/SimplePingJSE" xmlns:impl=http://wsj2ee.corej2ee/SimplePingJSE xmlns:intf="http://wsj2ee.corej2ee/SimplePingJSE …….. <types> ……… <xsd:complexType name=“….” Generated code using ‘impl’ Namespace will be in package corej2ee.wsj2ee Declare XML Namespaces Used Custom XML Schema types used ……. <wsdl:message name="echoResponse"> <wsdl:part name="echoReturn" type="xsd:string"/> </wsdl:message> ……………….. Describe structure of messages that will be passed in method calls. Could be custom/complex XML Schema types J2EE Web Services
WSDL Structure (Cont) <wsdl:binding name="SimplePingJSEEndpointSoapBinding" type="impl:SimplePingJSEEndpoint"> <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="ping"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="pingRequest"> <wsdlsoap:body use="literal" namespace="http://wsj2ee.corej2ee/SimplePingJSE"/> Defines protocols and Encoding style used <wsdl:service name="SimplePingJSEEndpointService"> <wsdl:port name="SimplePingJSEEndpoint" binding="impl:SimplePingJSEEndpointSoapBinding"> <wsdlsoap:address location="http://localhost:8080/wsj2eeSimplePing/SimplePing"/> </wsdl:port> </wsdl:service> Internet address of SOAP service J2EE Web Services
Sample WSDL Document (SimplePing.wsdl) ………… <wsdl:message name="echoResponse"> <wsdl:part name="echoReturn" type="xsd:string"/> Defines method that returns a string </wsdl:message> <wsdl:message name="echoRequest"> <wsdl:part name="in0" type="xsd:string"/> Defines method that takes in a string </wsdl:message> …………….. <wsdl:portType name="SimplePingEndpoint"> public interface SimplePingEndpoint ……………… <wsdl:operation name="echo" parameterOrder="in0"> <wsdl:input name="echoRequest" message="impl:echoRequest"/> <wsdl:output name="echoResponse" message="impl:echoResponse"/> </wsdl:operation> …………………… J2EE Web Services
SimplePingEndpoint.java(Endpoint Interface) public interface SimplePingEndpoint extends java.rmi.Remote { /** simple test method to call without args or return value */ void ping() throws RemoteException; /** simple test method that returns the string arcgument that is passed */ String echo(String message) throws RemoteException; /** simple test method that returns a string based on internal & inputs */ String speak(String message) throws RemoteException; } J2EE Web Services
WSDL document for Stub Generation • <wsdl:binding name="SimplePingEndpointSoapBinding" type="impl:SimplePingEndpoint"> • <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> • <wsdl:operation name="echo"> • <wsdlsoap:operation soapAction=""/> • <wsdl:input name="echoRequest"> • <wsdlsoap:body use="literal" namespace="http://wsj2ee.corej2ee/SimplePing"/> • </wsdl:input> • <wsdl:output name="echoResponse"> • <wsdlsoap:body use="literal" namespace="http://wsj2ee.corej2ee/SimplePing"/> • </wsdl:output> • </wsdl:operation> • </wsdl:binding> • Binding states that the messages passed in the echo method are encoded using rpc/literal encoding. • Generated sub implements the Endpoint interface and contains code to generate the correctly-formatted SOAP messages. J2EE Web Services
Service Interface (SimplePingEndpointService.java) • Serves as a factory for the stub • Used to get an instance at run-time • getSimplePingEndpoint() returns a stub implementation • J2EE components obtain reference to Service via jndi java:comp/env lookup public interface SimplePingEndpointService extends javax.xml.rpc.Service { public java.lang.String getSimplePingEndpointAddress(); public corej2ee.examples.wsj2ee.simple.wsimpl.SimplePingEndpoint getSimplePingEndpoint() throws javax.xml.rpc.ServiceException; public corej2ee.examples.wsj2ee.simple.wsimpl.SimplePingEndpoint getSimplePingEndpoint(java.net.URL portAddress) throws javax.xml.rpc.ServiceException; } J2EE Web Services
J2EE Web Service Client public class SimplePingClientServlet extends HttpServlet { ………………….. Context jndi = new InitialContext(); jseService_ = (SimplePingEndpointService)jndi.lookup("java:comp/env/service/SimplePingJSE"); ………………… protected void invokeService(SimplePingEndpointService service, String message, PrintWriter out) throws ServletException { SimplePingEndpoint stub = service.getSimplePingEndpoint(); out.println("<pre>"); out.println("executing echo(" + message + ")"); String reply = stub.echo(message); out.println("result=" + reply); out.println("</pre>"); J2EE Web Services
Build System Targets • Converting Java to WSDL • Converting WSDL to Java (endpoint and stub) J2EE Web Services
Service Interface • only necessary in clients • may be statically defined/compiled into client • may be dynamically created when service referenced • used to get a handle/stub (“Port”) to remote service • extends javax.xml.rpc.Service package corej2ee.examples.wsj2ee.simple.wsclient; import corej2ee.examples.wsj2ee.simple.wsclient.SimplePingJSEPort; public interface SimplePingJSEService extends javax.xml.rpc.Service { public java.lang.String getSimplePingJSEPortAddress(); public SimplePingJSEPort getSimplePingJSEPort() throws javax.xml.rpc.ServiceException; public SimplePingJSEPort getSimplePingJSEPort( java.net.URL portAddress) throws javax.xml.rpc.ServiceException; } J2EE Web Services
Endpoint/Port Interface • Defines WS operations that will be publicly available • Referred to as an “Endpoint” on server side and “Port” on client side • Interface required to extend java.rmi.Remote • Methods required to throw java.rmi.RemoteException //server-side hand-authored endpoint interface package corej2ee.examples.wsj2ee.simple.ws; import java.rmi.Remote; import java.rmi.RemoteException; public interface SimplePingEndpoint extends Remote { void ping() throws RemoteException; String echo(String message) throws RemoteException; String speak(String message) throws RemoteException; } J2EE Web Services
Endpoint/Port Interface (cont.) //client-side wsdl-generated port interface package corej2ee.examples.wsj2ee.simple.wsclient; public interface SimplePingJSEPort extends java.rmi.Remote { public void ping() throws java.rmi.RemoteException; public java.lang.String echo( java.lang.String in0) throws java.rmi.RemoteException; public java.lang.String speak(java.lang.String in0) throws java.rmi.RemoteException; } J2EE Web Services
JSE Endpoint Implement • Provides implementation for public operations • Extends Endpoint interface package corej2ee.examples.wsj2ee.simple.impl; import java.rmi.RemoteException; import java.util.Date; import javax.xml.rpc.ServiceException; import javax.xml.rpc.server.ServletEndpointContext; import corej2ee.examples.wsj2ee.simple.ws.SimplePingEndpoint; public class SimplePingImpl implements SimplePingEndpoint { public void ping() {} public String echo(String message) { return message; } public String speak(String message) { return new Date() + ":hello from JSE Web Service"; } } J2EE Web Services
JSE Optionally Implements ServiceLifecycle • Provides • getHttpSession() • getServletContext() • getMessageContext() • getUserPrincipal() • isUserInRole() import javax.xml.rpc.server.ServiceLifecycle; public class SimplePingImpl implements … , ServiceLifecycle { public void destroy() {} /** @param context is a javax.xml.rpc.ServletEndpointContext */ public void init(Object context) throws ServiceException { ServletEndpointContext ctx = (ServletEndpointContext) context; ctx.getServletContext().log( "SimplePingImpl.init(context=" + context + ")"); } } J2EE Web Services
EJB Endpoint Implementation • Provides the implementation of public operations • Optionally extends Endpoint interface • this example uses a wsdl-generated port interface for the endpoint interface package corej2ee.examples.wsj2ee.simple.ejb; import java.util.Date; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import corej2ee.examples.wsj2ee.simple.ws.SimplePingEJBPort; public class SimplePingEJB implements SessionBean, SimpleEJBPingPort { public void ping() {} public String echo(String message) { return message; } public String speak(String message) { return new Date() + ":hello from EJB Web Service"; } public void ejbCreate() {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbRemove() {} public void setSessionContext(SessionContext ctx) {} } J2EE Web Services
Component Deployment Descriptor • Web Services are deployed within pre-existing J2EE container types • Java Service/Servlet Endpoints (JSE) are deployed within a WEB/Servlet Container • EJB Endpoints are deployed within an EJB container • Web Services enjoy the same resources made available to their predecessors • JSE Endpoints may make use of HttpSessions, etc. • EJB Endpoints may make use of transactions, etc. • Pre-existing deployment descriptors (web.xml and ejb-jar.xml) have been modified to handle some Web Service properties • ex. adding <service-endpoint> to ejb-jar.xml • Some Web Service concepts have been made to fit within pre-existing deployment descriptors • ex. registering endpoint classes within <servlet> elements • New deployment descriptors have been added • ex. webservices.xml and <jaxrpc-mapping>.xml J2EE Web Services
Server web.xml • web-app • servlet • a re-used/morphed web.xml element that defines a JSE versus a Servlet/JSP • servlet-name • normal servlet element that assigns a unique name within the component/application • servlet-class • class which implements JSE • servlet-mapping • a re-used/morphed web.xml element that defines the mapping from a URL to the specific JSE • servlet-name • identifies JSE/Servlet from above • url-pattern • standard does not allow wildcards or multiple url-patterns for JSEs. • is directly related to the WSDL port endpoint address J2EE Web Services
Server web.xml Example <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>SimplePing</servlet-name> <servlet-class>corej2ee.examples.wsj2ee.simple.impl.SimplePingImpl </servlet-class> </servlet> <servlet-mapping> <servlet-name>SimplePing</servlet-name> <url-pattern>/SimplePing</url-pattern> </servlet-mapping> </web-app> J2EE Web Services
Server ejb-jar.xml • enterprise-beans • session • ejb-name • normal EJB element that uniquely identifies the EJB within the component/application • used by webservices.xml to link service with the EJB • service-endpoint • unique to J2EE Web Services • identifies the interface class that implements the endpoint interface • extends javax.rmi.Remote • value entered here is duplicated in webservices.xml service-endpoint-interface element • ejb-class • normal EJB element that identifies the EJB implementation class • … J2EE Web Services
Server ejb-jar.xml example <?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd“ version="2.1"> <enterprise-beans> <session> <ejb-name>SimplePingEJB</ejb-name> <service-endpoint> corej2ee.examples.wsj2ee.simple.wsimpl.SimplePingEJBPort </service-endpoint> <ejb-class>corej2ee.examples.wsj2ee.simple.ejb.SimplePingEJB </ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> J2EE Web Services
Server Web Service Descriptor (webservices.xml) • Required by all J2EE Components serving Web Service Endpoints • WEB-INF/webservices.xml for JSE • META-INF/webservices.xml for EJB • Binds J2EE Endpoints to their • WSDL port definition, • implementation deployment descriptor, • JAX-RPC mapping, and • endpoint interface • Binding used to generate skeleton code for the Web Service within the component • XML Elements • webservice-description • one element per WSDL file J2EE Web Services
Server webservice-description (webservices.xml) • wsdl-file • path within the component to the WSDL file • jaxrpc-mapping-file • path within the component to the JAXRPC Mapping file • port-component • maps a specific Endpoint to a specific port with the WSDL • port-component-name • unique name within webservices.xml • wsdl-port • provides qualified name of specific WSDL port being mapped • service-endpoint-interface • specify the endpoint interface class here • needed/first time specified for JSE • duplicate information (still required!) specified for EJB service-endpoint • service-impl-bean • ejb-link – points to ejb-name within the ejb-jar.xml declaration • servlet-link – points to the servlet-name within the web.xml declaration • handler • structure defines SOAP message handlers J2EE Web Services
Web Service Handler Description(server webservice-definition and client service-ref sub-element) • handler • optionally used by both server and client to add SOAP message handlers to handle issues like encryption, identification, etc. • handler-name • provides a unique name to the handler for internal use • handler-class • must implemenet javax.xml.rpc.handler.Handler interface • init-param (param-name, param-value) • obtained by handler through HandlerInfo.getHandlerConfig() • soap-header • names specific SOAP header for handler to process • soap-role • role that the handler is playing (ex. Security) • this will be matched with SOAP message actor attribute • port-name • identifies the WSDL port for which this handler applies • services can implement multiple ports J2EE Web Services
JAX-RPC Mapping File • Describes the relationship between Java Endpoint and Service classes and the WSDL document • maps Java endpoint interfaces, methods, and parameters to WSDL portTypes, operations, and messages • associates the Java endpoint with a specific WSDL port • maps Java service interfaces to WSDL service definitions • Allows specific Java and WSDL elements to be developed independently and mapped together within the application • when one is generated from or consistent with the other, this mapping is trivial • when the two are independently created, this mapping can get complex • example: map MyException.class <-> BankTellerFault • 1:1 relationship between the mapping and WSDL files • No standard name for the mapping file • Much of the details can be defaulted, resulting in a “lightweight” mapping file • unfortunately the lightweight form can only be used by RPC-encoded messages, which is not compliant with Basic (Interoperability) Profile 1.0 J2EE Web Services
JAX-RPC Mapping Elements • package-mapping • one or more mapping of WSDL namespaces to Java packages • package-type • Java package name • namespaceURI • WSDL namespace • Heavyweight mapping is very complex • see J2EE Web Services text for a detailed coverage of this topic J2EE Web Services
Client ejb-jar.xml and web.xml Service Reference • JNDI Environment References (j2ee_1_4.xsd) • env-entry • ejb-ref • ejb-local-ref • service-ref • Used to describe an external Web Service that will made available to the client J2EE component • Client container uses this as a starting point to locate the required definitions/mappings to generate the required stubs • Obtained through its java:comp/env JNDI context XXXService service = (XXXService)jndi.lookup(“service/XXXService”); XXXStub stub = service.getXXXPort(); stub.method(); • resource-ref • resource-env-ref • message-destination-ref J2EE Web Services
Primary Client Service Reference (service-ref) Elements • service-ref-name • defines the java:comp/env name the service will be registered • service-interface • the class type in which the JNDI lookup should return • type known at deployment time for statically generated stubs • type known at runtime for dynamically generated stubs • use javax.xml.rpc.Service in this case • wsdl-file • specifies a path within the component for the service’s WSDL file • WEB-INF/wsdl/<name>.wsdl for JSE deployments • META-INF/wsdl/<name>.wsdl for EJB deployments • jaxrpc-mapping-file • specifies a path within the component for the service’s jaxrpc mapping file • WEB-INF/<name>.xml for JSE deployments • META-INF/<name>.xml for EJB deployments • service-qname • identifies a specific service within the WSDL file J2EE Web Services
Other Service Reference Elements • port-component-ref • used by services who make use of Service.getPort(Stub.class) method • required only when using dynamically generated proxy classes • service-endpoint-interface • names the specific endpoint class returned for the stub • port-component-link • if service is deployed within same application, this can directly link the reference to the internally deployed service • handler • used, in this case, to add SOAP handlers on the client side • same structure used as server side webservices.xml J2EE Web Services