JAX-WS: WS client generating with SoapUI
Sep 02, 10 by Juan Lebrijo about SoapUI, Web Services, Java, blog
We will create the needed classes to generate a Web Service client from its WSDL. We will base in the HelloWorld project, and here will add the security. In this project we response with a Hello for the name which sent as argument through the Web Service. In order to generate the classe we will use SoapUI:
1004.thumbnail.gif 6.14 KB
We must to locate in our hard disk the wsimport.bat tool, downloaded with the SUN standard package implementation (of JAX-WS):
1005.thumbnail.gif 5.47 KB
It generates a folder with sources and other with compiled classes:
1006.thumbnail.gif 8.12 KB
To write the client helped with this classes is as easy as this:
package client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;

public class HelloClient {

    public static void main(String[] args) {
        HelloService service = null;
        try {
            // Creamos el servicio con el WSDL
            URL wsdlLocation = new URL("http://localhost:7001/wsc/HelloService?WSDL");
            String targetNamespace="http://services/";
            String name="HelloService";
            service = new HelloService( wsdlLocation, new QName(targetNamespace, name));
            Hello port = service.getHelloPort();
            // Añadimos capacidades de seguridad a la llamada
            BindingProvider provider = (BindingProvider) port;
            provider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user");   
            provider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "12345678");
            //Mostramos el resultado
            System.out.println(port.sayHello(args[0]));
        } catch (MalformedURLException e ) {
            e.printStackTrace();
        }
    }
}
JAX-WS Handler Chain: Interceptando los mensajes
Aug 29, 10 by Juan Lebrijo about Web Services, Java, blog
Queremos interceptar el WS en la salida y la entrada. Esto puede ser útil para:
  • Hacer log de alguna parte del mensaje
  • Llevar registro de mensajes enviados
  • Controles de seguridad añadidos
  • ....
Hoy vamos a hacer un ejemplo de log de la entrada/salida del WS. Anotamos el WS con el descriptor de la cadena de manejadores:
@WebService
@HandlerChain(file="src/META-INF/handlerChain.xml")
public class Hello {
@WebMethod
    public String sayHello(String name) {
        return "Hello, "+name+".";
    }
}
Que tendrá el contenido siguiente:
  
     
        
         lebrijo.handlers.WSHandler  
        
     
  
La clase WSHandler implementará la interfaz SOAPHandler:
public class WSHandler implements SOAPHandler {  
 
     public Set getHeaders() {  
         return null;  
     }  
  
     public boolean handleFault(SOAPMessageContext context) {  
         logToSystemOut(context);  
         return true;  
     }  
 
     public boolean handleMessage(SOAPMessageContext context) {  
         logToSystemOut(context);  
         return true;  
     }  

     private void logToSystemOut(SOAPMessageContext smc) {  
         Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);  
   
         if (outboundProperty.booleanValue()) {  
             System.out.println("\nOutgoing message:");  
         } else {  
             System.out.println("\nIncoming message:");  
         }  
   
         SOAPMessage message = smc.getMessage();  
         try {  
             message.writeTo(System.out);  
         } catch (Exception e) {  
             System.out.println("Exception in handler: " + e);  
         }  
     }

	public void close(MessageContext context) {
		// TODO Auto-generated method stub
		
	}  
JAX-WS Handler Chain: messages interceptor
Aug 29, 10 by Juan Lebrijo about Web Services, Java, blog
We want to intercept the WS input and output. In order to:
  • Logging any part in the message
  • Registering sent messages
  • Add security controls
  • ....
Here we are going to do a input/output WS example log. We annotate the WS with Handler Chain descriptor:
@WebService
@HandlerChain(file="src/META-INF/handlerChain.xml")
public class Hello {
@WebMethod
    public String sayHello(String name) {
        return "Hello, "+name+".";
    }
}
It will have the following contents:
  
     
        
         lebrijo.handlers.WSHandler  
        
     
  
WSHandler class will implements the SOAPHandler interface:
public class WSHandler implements SOAPHandler {  
 
     public Set getHeaders() {  
         return null;  
     }  
  
     public boolean handleFault(SOAPMessageContext context) {  
         logToSystemOut(context);  
         return true;  
     }  
 
     public boolean handleMessage(SOAPMessageContext context) {  
         logToSystemOut(context);  
         return true;  
     }  

     private void logToSystemOut(SOAPMessageContext smc) {  
         Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);  
   
         if (outboundProperty.booleanValue()) {  
             System.out.println("Outgoing message:");  
         } else {  
             System.out.println("Incoming message:");  
         }  
   
         SOAPMessage message = smc.getMessage();  
         try {  
             message.writeTo(System.out);  
         } catch (Exception e) {  
             System.out.println("Exception in handler: " + e);  
         }  
     }

	public void close(MessageContext context) {
		// TODO Auto-generated method stub
		
	}