Programming Guidelines for Target Web Service Programming Guidelines for the Callback Client Web Service

11-4 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server

11.4 Programming Guidelines for Target Web Service

The following example shows a simple JWS file that implements the target Web service; see the explanation after the example for coding guidelines that correspond to the Java code in bold. package examples.webservices.callback; import javax.jws.WebService; import javax.xml.ws.BindingType; import javax.xml.ws.wsaddressing.W3CEndpointReference; import examples.webservices.callback.callbackservice.; WebService portName=TargetPort, serviceName=TargetService, targetNamespace=http:example.oracle.com, endpointInterface= examples.webservices.callback.target.TargetPortType, wsdlLocation=wsdlsTarget.wsdl BindingTypevalue=http:schemas.xmlsoap.orgwsdlsoaphttp public class TargetImpl { public String targetOperationString s, W3CEndpointReference callback { CallbackService aservice = new CallbackService; CallbackPortType aport = aservice.getPortcallback, CallbackPortType.class; String result = aport.callbacks; return result + processed by target; } } Follow these guidelines when programming the JWS file that implements the target Web service. Code snippets of the guidelines are shown in bold in the preceding example. ■ Import the packages required to pass the callback service endpoint and access the CallbackService stub implementation. import javax.xml.ws.wsaddressing.W3CEndpointReference; import examples.webservices.callback.callbackservice.; ■ Create an instance of the CallbackService implementation using the stub implementation and get a port by passing the CallbackService service endpoint, which is passed by the calling application CallerService. CallbackService aservice = new CallbackService; CallbackPortType aport = aservice.getPortcallback, CallbackPortType.class; ■ Invoke the callback operation of CallbackService using the port you instantiated: String result = aport.callbacks; ■ Return the result to the CallerService service. return result + processed by target; Using Callbacks 11-5

11.5 Programming Guidelines for the Callback Client Web Service

The following example shows a simple JWS file for a client Web service that invokes the target Web service described in Section 11.4, Programming Guidelines for Target Web Service ; see the explanation after the example for coding guidelines that correspond to the Java code in bold. package examples.webservices.callback; import javax.annotation.Resource; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.BindingType; import javax.xml.ws.Endpoint; import javax.xml.ws.WebServiceContext; import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceRef; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.wsaddressing.W3CEndpointReference; import examples.webservices.callback.target.; WebService portName=CallerPort, serviceName=CallerService, targetNamespace=http:example.oracle.com BindingTypevalue=http:schemas.xmlsoap.orgwsdlsoaphttp public class CallerImpl { Resource private WebServiceContext context; WebServiceRef private TargetService target; WebMethod public String callString s { Object sc = context.getMessageContext.getMessageContext.SERVLET_CONTEXT; Endpoint callbackImpl = Endpoint.createnew CallbackWS; callbackImpl.publishsc; TargetPortType tPort = target.getTargetPort; String result = tPort.targetOperations, callbackImpl.getEndpointReferenceW3CEndpointReference.class; callbackImpl.stop; return result; } } Follow these guidelines when programming the JWS file that invokes the target Web service; code snippets of the guidelines are shown in bold in the preceding example: ■ Import the packages required to access the servlet context, publish the Web service endpoint, and access the TargetService stub implementation. import javax.xml.ws.Endpoint; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.wsaddressing.W3CEndpointReference; import examples.webservices.callback.target.; 11-6 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server ■ Get the servlet context using the WebServiceContext and MessageContext. You will use the servlet context when publishing the Web service endpoint, later. Resource private WebServiceContext context; . . . Object sc context.getMessageContext.getMessageContext.SERVLET_CONTEXT; For more information about accessing runtime information using WebServiceContext and MessageContext, see Accessing Runtime Information About a Web service in Getting Started With JAX-WS Web Services for Oracle WebLogic Server. ■ Create a Web service endpoint to the CallbackService implementation and publish that endpoint to accept incoming requests. Endpoint callbackImpl = Endpoint.createnew CallbackWS; callbackImpl.publishsc; For more information about Web service publishing, see Chapter 10, Publishing a Web Service Endpoint. ■ Access an instance of the TargetService stub implementation and invoke the targetOperation operation of TargetService using the port you instantiated. You pass the CallbackService service endpoint as a javax.xml.ws.wsaddressing.W3CEndpointReference data type: WebServiceRef private TargetService target; . . . TargetPortType tPort = target.getTargetPort; String result = tPort.targetOperations, callbackImpl.getEndpointReferenceW3CEndpointReference.class; ■ Stop publishing the endpoint: callbackImpl.stop;

11.6 Programming Guidelines for the Callback Web Service