package org.jboss.soa.bpel.testcase;

import java.util.Iterator;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import org.w3c.dom.NodeList;

public class HandleLogging implements SOAPHandler<SOAPMessageContext> {

	public Set<QName> getHeaders() {
		//System.out.println(">>>>>>. headers");
		return null;
	}

	public void close(MessageContext arg0) {
		//System.out.println(">>>>> close");
	}

	public boolean handleFault(SOAPMessageContext arg0) {
		//System.out.println(">>>>> fault");
		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean handleMessage(SOAPMessageContext messageContext) {
		try {
			Boolean outboundProperty = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

			// Mensagem chegando
			if (!outboundProperty.booleanValue()) {
				SOAPMessageContext sctx = (SOAPMessageContext) messageContext;
		        SOAPMessage message     = sctx.getMessage();
		        SOAPPart sp             = message.getSOAPPart();
		        SOAPEnvelope senv       = sp.getEnvelope();
		        SOAPHeader sh           = senv.getHeader();
		        
		        System.out.println("1: " + senv.getNodeName());
		        Iterator it2 = senv.getChildElements();
		        while (it2.hasNext()) {
		        	Object obj = it2.next();
		        	if (obj instanceof SOAPHeader) {
		        		SOAPHeader soapHeader = (SOAPHeader) obj;
		        		System.out.println("2: \t" + soapHeader.getNodeName());
		        		Iterator it3 = soapHeader.getChildElements();
		        		if (it3 != null) {
		        			while (it3.hasNext()) {
		        				SOAPHeaderElement soapH = (SOAPHeaderElement) it3.next();
		        				System.out.println("3: \t\t" + soapH.getNodeName() + "> " + soapH.getValue());
		        			}
		        		}
		        	}
		        	if (obj instanceof SOAPBody) {
		        		SOAPBody soapBody = (SOAPBody) obj;
		        		Iterator it4 = soapBody.getChildElements();
		        		if (it4 != null) {
		        			while (it4.hasNext()) {
		        				System.out.println("4: \t" + soapBody.getNodeName());
		        				Object obj4 = it4.next();
		        				if (obj4 instanceof SOAPBodyElement) {
			        				SOAPBodyElement soapBodyElement = (SOAPBodyElement) obj4;
			        				System.out.println("5: \t" + soapBodyElement.getNodeName());
			        				Iterator it5 = soapBodyElement.getChildElements();
	        						NodeList nl =  soapBodyElement.getChildNodes();
	        						if (nl != null) {
	        							for (int i = 0; i < nl.getLength(); i++) {
	        								System.out.println("6: \t\t" + nl.item(i).getNodeName());
	        							}
	        						}
	        						/*Object obj5 = it5.next();
	        						SOAPElement element = (SOAPElement) obj5;
	        						String name = element.getNodeName();
	        						System.out.println("\t\t" + name);*/
		        				}
		        			}
		        		}
		        	}
		        }
		        
		        
		        if (sh != null) {
			        Iterator it = sh.getChildElements();
			        while (it.hasNext()) {
			        	Node node = (Node) it.next();
			        	System.out.println("Name: " + node.getNodeName() + " - Value: " + node.getValue());
			        }
		        }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}

}
