1 19 20 package org.netbeans.test.examples.logging; 21 22 27 28 import javax.xml.rpc.handler.MessageContext ; 30 import javax.xml.rpc.handler.HandlerInfo ; 31 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 32 import javax.xml.namespace.QName ; 33 import javax.xml.soap.SOAPElement ; 34 import javax.xml.soap.SOAPMessage ; 35 import javax.xml.soap.SOAPPart ; 36 import javax.xml.soap.SOAPEnvelope ; 37 import javax.xml.soap.SOAPHeader ; 38 import javax.xml.soap.SOAPBody ; 39 import java.util.Date ; 40 42 public class LoggingMessageHandler extends javax.xml.rpc.handler.GenericHandler { 43 45 private QName [] headers; 46 47 public void init(HandlerInfo config) { 48 headers = config.getHeaders(); 49 } 50 51 public javax.xml.namespace.QName [] getHeaders() { 52 return headers; 53 } 54 55 public boolean handleRequest(MessageContext context) { 57 try{ 58 SOAPMessageContext smc = (SOAPMessageContext ) context; 59 SOAPMessage msg = smc.getMessage(); 60 SOAPPart sp = msg.getSOAPPart(); 61 SOAPEnvelope se = sp.getEnvelope(); 62 SOAPHeader shd = se.getHeader(); 63 64 SOAPBody sb = se.getBody(); 65 java.util.Iterator childElems = sb.getChildElements(); 66 SOAPElement child; 67 StringBuffer message = new StringBuffer (); 68 while (childElems.hasNext()) { 69 child = (SOAPElement ) childElems.next(); 70 message.append(new Date ().toString() + "--"); 71 formLogMessage(child, message); 72 } 73 74 System.out.println("Log message: " + message.toString()); 75 } catch(Exception e){ 76 e.printStackTrace(); 77 } 78 return true; 79 } 80 81 public boolean handleResponse(MessageContext context) { 82 return true; 83 } 84 85 public boolean handleFault(MessageContext context) { 86 return true; 87 } 88 89 public void destroy() { 90 } 91 92 private void formLogMessage(SOAPElement child, StringBuffer message) { 93 message.append(child.getElementName().getLocalName()); 94 message.append(child.getValue() != null ? ":" + child.getValue() + " " : " "); 95 96 try{ 97 java.util.Iterator childElems = child.getChildElements(); 98 while (childElems.hasNext()) { 99 Object c = childElems.next(); 100 if(c instanceof SOAPElement ) 101 formLogMessage((SOAPElement )c, message); 102 } 103 }catch(Exception e){ 104 e.printStackTrace(); 105 } 106 } 107 } 108 | Popular Tags |