KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.test.examples.logging;
2
3
4 /**
5  * This is the implementation for the DummyHandlerSOAP Message Handler.
6  * Created Jun 4, 2005 2:02:51 AM
7  * @author jungi
8  */

9 //<editor-fold defaultstate="collapsed" desc="import statements. Click the + sign on the left to edit the code.">
10
import javax.xml.rpc.handler.MessageContext JavaDoc;
11 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
12 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
13 import javax.xml.namespace.QName JavaDoc;
14 import javax.xml.soap.SOAPElement JavaDoc;
15 import javax.xml.soap.SOAPMessage JavaDoc;
16 import javax.xml.soap.SOAPPart JavaDoc;
17 import javax.xml.soap.SOAPEnvelope JavaDoc;
18 import javax.xml.soap.SOAPHeader JavaDoc;
19 import javax.xml.soap.SOAPBody JavaDoc;
20 import java.util.Date JavaDoc;
21 //</editor-fold>
22
public class DummyHandler extends javax.xml.rpc.handler.GenericHandler JavaDoc {
23     // TODO Change and enhance the handle methods to suit individual needs.
24

25     private QName JavaDoc[] headers;
26     
27     public void init(HandlerInfo JavaDoc config) {
28         headers = config.getHeaders();
29     }
30     
31     public javax.xml.namespace.QName JavaDoc[] getHeaders() {
32         return headers;
33     }
34     
35     // Currently prints out the contents of the SOAP body plus some date information.
36
public boolean handleRequest(MessageContext JavaDoc context) {
37         try{
38             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
39             SOAPMessage JavaDoc msg = smc.getMessage();
40             SOAPPart JavaDoc sp = msg.getSOAPPart();
41             SOAPEnvelope JavaDoc se = sp.getEnvelope();
42             SOAPHeader JavaDoc shd = se.getHeader();
43             
44             SOAPBody JavaDoc sb = se.getBody();
45             java.util.Iterator JavaDoc childElems = sb.getChildElements();
46             SOAPElement JavaDoc child;
47             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
48             while (childElems.hasNext()) {
49                 child = (SOAPElement JavaDoc) childElems.next();
50                 message.append(new Date JavaDoc().toString() + "--");
51                 formLogMessage(child, message);
52             }
53             
54             System.out.println("Log message: " + message.toString());
55         } catch(Exception JavaDoc e){
56             e.printStackTrace();
57         }
58         return true;
59     }
60     
61     public boolean handleResponse(MessageContext JavaDoc context) {
62         return true;
63     }
64     
65     public boolean handleFault(MessageContext JavaDoc context) {
66         return true;
67     }
68     
69     public void destroy() {
70     }
71     
72     private void formLogMessage(SOAPElement JavaDoc child, StringBuffer JavaDoc message) {
73         message.append(child.getElementName().getLocalName());
74         message.append(child.getValue() != null ? ":" + child.getValue() + " " : " ");
75         
76         try{
77             java.util.Iterator JavaDoc childElems = child.getChildElements();
78             while (childElems.hasNext()) {
79                 Object JavaDoc c = childElems.next();
80                 if(c instanceof SOAPElement JavaDoc)
81                     formLogMessage((SOAPElement JavaDoc)c, message);
82             }
83         }catch(Exception JavaDoc e){
84             e.printStackTrace();
85         }
86     }
87 }
88
Popular Tags