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();
}
}
}
@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
}
@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
}