El siguiente código te permitirá imprimir en el log las trazas de entrada y salida de un servicio web:
Si usas JDK 7 en adelante:
static { System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true"); System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true"); System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true"); System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true"); }
Si es anterior, puedes usar un Handler:
LogMessageSOAPHandler.java
package com.ws.handler; import java.util.Collections; import java.util.Iterator; import java.util.Set; import javax.xml.namespace.QName; import javax.xml.soap.MimeHeader; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ws.util.XMLFormatUtil; public class LogMessageSOAPHandler implements SOAPHandler { private static final Logger LOGGER = LoggerFactory.getLogger(LogMessageSOAPHandler.class); private String getMessageEncoding(SOAPMessage msg) { String encoding = "UTF-8"; try { if (msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING) != null) { encoding = msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING).toString(); } } catch (SOAPException e) { LOGGER.warn("Error when trying to obtain encoding character from SoapMessage, set {} by default", encoding, e); } return encoding; } private void logSoapMessage(SOAPMessageContext context) { String headerTrace = "[SOAP MESSAGE]:"; boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound) { LOGGER.debug("{} Direction=outbound", headerTrace); } else { LOGGER.debug("{} Direction=inbound", headerTrace); } SOAPMessage msg = context.getMessage(); if (msg == null) { LOGGER.warn("{} Message is null", headerTrace); return; } LOGGER.debug("{} Mime Headers: ", headerTrace); // infer generytype @SuppressWarnings("unchecked") Iterator allHeaders = msg.getMimeHeaders().getAllHeaders(); while (allHeaders.hasNext()) { MimeHeader mh = allHeaders.next(); String mime = new StringBuffer().append("name=").append(mh.getName()).append(", value=").append(mh.getValue()).toString(); LOGGER.debug(mime); } try { LOGGER.debug("{} \n {}", headerTrace, XMLFormatUtil.prettyFormat(msg, 2, getMessageEncoding(msg))); } catch (Exception e) { LOGGER.warn("Error trying to trace soap message", e); } } @Override public boolean handleMessage(SOAPMessageContext context) { logSoapMessage(context); return true; } @Override public boolean handleFault(SOAPMessageContext context) { logSoapMessage(context); return true; } @Override public void close(MessageContext context) { // Do nothing because this handler use to print inboud or outbound // message } @Override public Set getHeaders() { return Collections.emptySet(); } }(PARA CLIENTE)
LogHandlerResolver.java
package com.ws.handler; import java.util.ArrayList; import java.util.List; import javax.xml.ws.handler.Handler; import javax.xml.ws.handler.HandlerResolver; import javax.xml.ws.handler.PortInfo; public class LogHandlerResolver implements HandlerResolver { @SuppressWarnings("rawtypes") @Override public List getHandlerChain(PortInfo portInfo) { List handlerChain = new ArrayList(); // Add log web service handlerChain.add(new LogMessageSOAPHandler()); return handlerChain; } }
(PARA SERVIDOR)
handler-chain.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <javaee:handler-chain> <javaee:handler> <!-- log soap message on publisher service --> <javaee:handler-class>com.inetpsa.ped.manager.ws.handler.LogMessageSOAPHandler</javaee:handler-class> </javaee:handler> </javaee:handler-chain> </javaee:handler-chains>
Codigo Servidor:
package com.ws.ressources; import javax.inject.Inject; import javax.jws.HandlerChain; import javax.jws.WebService; import javax.xml.ws.soap.Addressing; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Addressing(enabled = true, required = false) @WebService(targetNamespace = "http://xml.ws.com/ReferentielFabrication", endpointInterface = "com.gen.resources.DemandeAlerteServicePortType", serviceName = "DemandeAlerteService", portName = "DemandeAlerteServiceHttpPort") @HandlerChain(file = "handler-chain.xml") public class DemandeAlerteWebServiceImpl implements DemandeAlerteServicePortType { private static final Logger LOGGER = LoggerFactory.getLogger(DemandeAlerteWebServiceImpl.class); @Inject public AlerteService alerteService; @Override public ReponseAlerteType alerte(DemandeAlerteType demandeale) throws ServiceException_Exception { } }Codigo Cliente:
public void callWebService() throws Exception { // call webservice WebServiceClient webServiceClient = AbonnementCalendrierService2.class.getAnnotation(WebServiceClient.class); URL wsdlURL = AbonnementCalendrierService2.class.getClassLoader() .getResource(WSDL_FILE_PATH); AbonnementCalendrierService2 client = new AbonnementCalendrierService2( wsdlURL, new QName(webServiceClient.targetNamespace(), webServiceClient.name())); // Add log xml ws message client.setHandlerResolver(new PedLogHandlerResolver()); // Add WS-Addressing AddressingFeature addressingfeature = new AddressingFeature(); AbonnementCalendrierService2PortType port = client.getAbonnementCalendrierService2HttpPort(addressingfeature); if (IS_HTTP_TRANSPORT) { ((BindingProvider) port).getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, uriHttpService); LOGGER.debug("Sending soap message to {}", uriHttpService); } else { ((BindingProvider) port).getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, uriJmsService); // Add a port to the Service client.addPort(new QName(webServiceClient.targetNamespace(), webServiceClient.name()), SOAP_JMS_SPECIFICATION_TRANSPORTID, uriJmsService); LOGGER.debug("Sending soap message to {}", uriJmsService); } NotificationAbonnementType2 notificationAbonnement2 = new NotificationAbonnementType2(); notificationAbonnement2.setDateMAJ("12/01/2001"); port.abonnement(notificationAbonnement2); }