Posted By

sergheiistrati on 11/30/09


Tagged

axis


Versions (?)

Axis log handler


 / Published in: Java
 

  1. package org.apache.axis.handlers;
  2.  
  3. import java.security.cert.X509Certificate;
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6.  
  7. import org.apache.axis.AxisFault;
  8. import org.apache.axis.Message;
  9. import org.apache.axis.MessageContext;
  10. import org.apache.axis.components.logger.LogFactory;
  11. import org.apache.axis.transport.http.HTTPConstants;
  12. import org.apache.commons.logging.Log;
  13.  
  14. /**
  15.  * A simple Handler which logs the client certificate, request and response messages on INFO level
  16.  *
  17.  * To use this, deploy it in response flows (global, service, or transport).
  18.  * If is deployed in both response and request flows the elapsed time will be logged in milliseconds.
  19.  *
  20.  * Example for service use: <br/>
  21.  * <handler name="myHandler" type="java:org.apache.axis.handlers.MyHandler" /> <br/>
  22.  * <service name="..." provider="java:RPC"> <br/>
  23.  * <parameter name="..." value="..." /> <br/>
  24.  * <br/>
  25.  * .... <br/>
  26.  * <br/>
  27.  * <requestFlow> <br/>
  28.  * <handler type="myHandler" /> <br/>
  29.  * </requestFlow> <br/>
  30.  * <responseFlow> <br/>
  31.  * <handler type="myHandler" /> <br/>
  32.  * </responseFlow> <br/>
  33.  * </service> <br/>
  34.  *
  35.  *
  36.  */
  37. public class MyHandler extends BasicHandler {
  38.  
  39. private static final String SEPARATOR = System.getProperty("line.separator");
  40. private static final long serialVersionUID = -1511939658630576004L;
  41. protected static Log log = LogFactory.getLog(MyHandler.class.getName());
  42. long start = -1;
  43.  
  44. @Override
  45. public void invoke(MessageContext msgContext) throws AxisFault {
  46. log.debug("Enter: MyHandler::invoke");
  47. if (msgContext.getPastPivot()) {
  48. logMessages(msgContext);
  49. }else{
  50. start = System.currentTimeMillis();
  51. }
  52.  
  53. log.debug("Exit: MyHandler::invoke");
  54. }
  55.  
  56. private void logMessages(MessageContext msgContext) throws AxisFault {
  57. log.debug("Enter: MyHandler::logMessages");
  58. try {
  59. Message inMsg = msgContext.getRequestMessage();
  60. Message outMsg = msgContext.getResponseMessage();
  61.  
  62. StringBuilder str = new StringBuilder();
  63. str.append("--- START ---").append(SEPARATOR);
  64. logCertificate(msgContext, str);
  65. if(start != -1){
  66. str.append("Elapsed Time ms: " + (System.currentTimeMillis() - start)).append(SEPARATOR).append(SEPARATOR);
  67. }
  68. str.append("Transport URL: " + msgContext.getProperty("transport.url")).append(SEPARATOR).append(SEPARATOR);
  69. str.append("Request Message: " + inMsg.getSOAPPartAsString()).append(SEPARATOR).append(SEPARATOR);
  70. str.append("Response Message: " + outMsg.getSOAPPartAsString()).append(SEPARATOR);
  71.  
  72. str.append("--- END ---").append(SEPARATOR);
  73.  
  74. log.info(str.toString());
  75.  
  76. } catch (Exception e) {
  77. log.error(e);
  78. throw AxisFault.makeFault(e);
  79. }
  80. log.debug("Exit: MyHandler::logMessages");
  81. }
  82.  
  83.  
  84.  
  85. private void logCertificate(MessageContext messageContext, StringBuilder str) {
  86. log.debug("Enter: MyHandler::logCertificate");
  87. HttpServletRequest httpServletRequest = (HttpServletRequest) messageContext
  88. .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
  89. X509Certificate[] x509Certificates = (X509Certificate[]) httpServletRequest
  90. .getAttribute("javax.servlet.request.X509Certificate");
  91.  
  92. if (x509Certificates != null && x509Certificates.length > 0) {
  93. log.debug("Total count of X509Certificate: "+x509Certificates.length);
  94. X509Certificate cert = x509Certificates[0];
  95. str.append("Certificate: " + cert.getSubjectX500Principal().getName()).append(SEPARATOR).append(SEPARATOR);
  96. }
  97. log.debug("Exit: MyHandler::logCertificate");
  98. }
  99.  
  100. @Override
  101. public void onFault(MessageContext msgContext) {
  102. log.debug("Enter: MyHandler::onFault");
  103. try {
  104. logMessages(msgContext);
  105. } catch (AxisFault e) {
  106. log.error("AxisFault exception", e);
  107. }
  108. log.debug("Exit: MyHandler::onFault");
  109. }
  110. };

Report this snippet  

You need to login to post a comment.