KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > examples > logging > LoggingMessageHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.examples.logging;
21
22 /**
23  * This is the implementation for the LoggingMessageHandlerSOAP Message Handler.
24  * Created Jun 3, 2005 5:37:25 PM
25  * @author jungi
26  */

27
28 //<editor-fold defaultstate="collapsed" desc="import statements. Click the + sign on the left to edit the code.">
29
import javax.xml.rpc.handler.MessageContext JavaDoc;
30 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
31 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
32 import javax.xml.namespace.QName JavaDoc;
33 import javax.xml.soap.SOAPElement JavaDoc;
34 import javax.xml.soap.SOAPMessage JavaDoc;
35 import javax.xml.soap.SOAPPart JavaDoc;
36 import javax.xml.soap.SOAPEnvelope JavaDoc;
37 import javax.xml.soap.SOAPHeader JavaDoc;
38 import javax.xml.soap.SOAPBody JavaDoc;
39 import java.util.Date JavaDoc;
40 //</editor-fold>
41

42 public class LoggingMessageHandler extends javax.xml.rpc.handler.GenericHandler JavaDoc {
43     // TODO Change and enhance the handle methods to suit individual needs.
44

45     private QName JavaDoc[] headers;
46     
47     public void init(HandlerInfo JavaDoc config) {
48         headers = config.getHeaders();
49     }
50     
51     public javax.xml.namespace.QName JavaDoc[] getHeaders() {
52         return headers;
53     }
54     
55     // Currently prints out the contents of the SOAP body plus some date information.
56
public boolean handleRequest(MessageContext JavaDoc context) {
57         try{
58             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
59             SOAPMessage JavaDoc msg = smc.getMessage();
60             SOAPPart JavaDoc sp = msg.getSOAPPart();
61             SOAPEnvelope JavaDoc se = sp.getEnvelope();
62             SOAPHeader JavaDoc shd = se.getHeader();
63             
64             SOAPBody JavaDoc sb = se.getBody();
65             java.util.Iterator JavaDoc childElems = sb.getChildElements();
66             SOAPElement JavaDoc child;
67             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
68             while (childElems.hasNext()) {
69                 child = (SOAPElement JavaDoc) childElems.next();
70                 message.append(new Date JavaDoc().toString() + "--");
71                 formLogMessage(child, message);
72             }
73             
74             System.out.println("Log message: " + message.toString());
75         } catch(Exception JavaDoc e){
76             e.printStackTrace();
77         }
78         return true;
79     }
80     
81     public boolean handleResponse(MessageContext JavaDoc context) {
82         return true;
83     }
84     
85     public boolean handleFault(MessageContext JavaDoc context) {
86         return true;
87     }
88     
89     public void destroy() {
90     }
91     
92     private void formLogMessage(SOAPElement JavaDoc child, StringBuffer JavaDoc message) {
93         message.append(child.getElementName().getLocalName());
94         message.append(child.getValue() != null ? ":" + child.getValue() + " " : " ");
95         
96         try{
97             java.util.Iterator JavaDoc childElems = child.getChildElements();
98             while (childElems.hasNext()) {
99                 Object JavaDoc c = childElems.next();
100                 if(c instanceof SOAPElement JavaDoc)
101                     formLogMessage((SOAPElement JavaDoc)c, message);
102             }
103         }catch(Exception JavaDoc e){
104             e.printStackTrace();
105         }
106     }
107 }
108
Popular Tags