Return to Snippet

Revision: 20895
at November 30, 2009 09:34 by sergheiistrati


Initial Code
package org.apache.axis.handlers;

import java.security.cert.X509Certificate;

import javax.servlet.http.HttpServletRequest;

import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.components.logger.LogFactory;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;

/**
 * A simple Handler which logs the client certificate, request and response messages on INFO level
 * 
 * To use this, deploy it in response flows (global, service, or transport). 
 * If is deployed in both response and request flows the elapsed time will be logged in milliseconds.
 * 
 * Example for service use:															<br/>
 *  <handler name="myHandler" type="java:org.apache.axis.handlers.MyHandler" />		<br/>
 *	<service name="..." provider="java:RPC">										<br/>
 *		<parameter name="..." value="..." />										<br/>
 *																					<br/>
 *		....																		<br/>
 *																					<br/>
 *		<requestFlow>																<br/>
 *			<handler type="myHandler" />											<br/>
 *		</requestFlow>																<br/>
 *		<responseFlow>																<br/>
 *			<handler type="myHandler" />											<br/>
 *		</responseFlow>																<br/>
 *	</service>																		<br/>
 * 
 * 
 */
public class MyHandler extends BasicHandler {

	private static final String SEPARATOR = System.getProperty("line.separator");
	private static final long serialVersionUID = -1511939658630576004L;
	protected static Log log = LogFactory.getLog(MyHandler.class.getName());
	long start = -1;
	
	@Override
	public void invoke(MessageContext msgContext) throws AxisFault {
		log.debug("Enter: MyHandler::invoke");
		if (msgContext.getPastPivot()) {		
			logMessages(msgContext);
		}else{
			start = System.currentTimeMillis();
		}

		log.debug("Exit: MyHandler::invoke");
	}

	private void logMessages(MessageContext msgContext) throws AxisFault {
		log.debug("Enter: MyHandler::logMessages");
		try {
			Message inMsg = msgContext.getRequestMessage();
			Message outMsg = msgContext.getResponseMessage();

			StringBuilder str = new StringBuilder();
			str.append("--- START ---").append(SEPARATOR);
			logCertificate(msgContext, str);
			if(start != -1){
				str.append("Elapsed Time ms: " + (System.currentTimeMillis() - start)).append(SEPARATOR).append(SEPARATOR);
			}
			str.append("Transport URL: " + msgContext.getProperty("transport.url")).append(SEPARATOR).append(SEPARATOR);
			str.append("Request Message: " + inMsg.getSOAPPartAsString()).append(SEPARATOR).append(SEPARATOR);
			str.append("Response Message: " + outMsg.getSOAPPartAsString()).append(SEPARATOR);

			str.append("--- END ---").append(SEPARATOR);
			
			log.info(str.toString());

		} catch (Exception e) {
			log.error(e);
			throw AxisFault.makeFault(e);
		}
		log.debug("Exit: MyHandler::logMessages");
	}

	

	private void logCertificate(MessageContext messageContext, StringBuilder str) {
		log.debug("Enter: MyHandler::logCertificate");
		HttpServletRequest httpServletRequest = (HttpServletRequest) messageContext
				.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
		X509Certificate[] x509Certificates = (X509Certificate[]) httpServletRequest
				.getAttribute("javax.servlet.request.X509Certificate");
		
		if (x509Certificates != null && x509Certificates.length > 0) {
			log.debug("Total count of X509Certificate: "+x509Certificates.length);
			X509Certificate cert = x509Certificates[0];
			str.append("Certificate: " + cert.getSubjectX500Principal().getName()).append(SEPARATOR).append(SEPARATOR);
		}
		log.debug("Exit: MyHandler::logCertificate");
	}

	@Override
	public void onFault(MessageContext msgContext) {
		log.debug("Enter: MyHandler::onFault");
		try {
			logMessages(msgContext);
		} catch (AxisFault e) {
			log.error("AxisFault exception", e);
		}
		log.debug("Exit: MyHandler::onFault");
	}
};

Initial URL

                                

Initial Description

                                

Initial Title
Axis log handler

Initial Tags

                                

Initial Language
Java