/**
 * $Id: SOAPMessageLogHandler.java,v 1.2 2006/03/23 11:46:21 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 */

package vkc.ws.axis.handler;

import java.io.PrintStream;
import java.util.Properties;

import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.SOAPPart;
import org.apache.axis.handlers.BasicHandler;

/**
 * Simple Axis-Handler zur Ausgabe von SOAP Messages
 */
public class SOAPMessageLogHandler extends BasicHandler {
	private static boolean monitorEnabled = false;

	private static PrintStream out = System.out;
	private static Properties properties = new Properties();
	
	public synchronized final static void configure(Properties properties) {
		SOAPMessageLogHandler.properties = properties == null ? new Properties() : properties;

		monitorEnabled = "true".equals(SOAPMessageLogHandler.properties.getProperty("axis.soapmonitor.enabled", ""));
	}
	
	/**
	 * Constructor
	 */
	public SOAPMessageLogHandler() {
		super();
	}

	/**
	 * Process SOAP messages
	 */
	public void invoke(MessageContext messageContext) throws AxisFault {
		if (!monitorEnabled) {
			return;
		}
		
		if (!messageContext.getPastPivot()) {
			Message message = messageContext.getRequestMessage();
			
			if (message != null) {
				out.println("\nSOAP request:");
				out.println("-----------------------------------------------");
				out.println(((SOAPPart) message.getSOAPPart()).getAsString());
			}
		} else {
			Message message = messageContext.getResponseMessage();

			if (message != null) {
				out.println("\nSOAP response:");
				out.println("-----------------------------------------------");
				out.println(((SOAPPart) message.getSOAPPart()).getAsString());
			}
		}
	}
}