KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > handlers > common > LoggingHandler


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc.
3  * All rights reserved.
4  */

5 package demo.handlers.common;
6
7
8 import java.io.PrintStream JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import javax.xml.namespace.QName JavaDoc;
13 import javax.xml.soap.SOAPMessage JavaDoc;
14 import javax.xml.ws.handler.MessageContext;
15 import javax.xml.ws.handler.soap.SOAPHandler;
16 import javax.xml.ws.handler.soap.SOAPMessageContext;
17
18 /*
19  * This simple SOAPHandler will output the contents of incoming
20  * and outgoing messages.
21  */

22 public class LoggingHandler implements SOAPHandler<SOAPMessageContext> {
23
24     private PrintStream JavaDoc out;
25
26     public LoggingHandler() {
27         setLogStream(System.out);
28     }
29
30     protected final void setLogStream(PrintStream JavaDoc ps) {
31         out = ps;
32     }
33
34     public void init(Map JavaDoc c) {
35         System.out.println("LoggingHandler : init() Called....");
36     }
37
38     public Set JavaDoc<QName JavaDoc> getHeaders() {
39         return null;
40     }
41
42     public boolean handleMessage(SOAPMessageContext smc) {
43         System.out.println("LoggingHandler : handleMessage Called....");
44         logToSystemOut(smc);
45         return true;
46     }
47
48     public boolean handleFault(SOAPMessageContext smc) {
49         System.out.println("LoggingHandler : handleFault Called....");
50         logToSystemOut(smc);
51         return true;
52     }
53
54     // nothing to clean up
55
public void close(MessageContext messageContext) {
56         System.out.println("LoggingHandler : close() Called....");
57     }
58
59     // nothing to clean up
60
public void destroy() {
61         System.out.println("LoggingHandler : destroy() Called....");
62     }
63
64     /*
65      * Check the MESSAGE_OUTBOUND_PROPERTY in the context
66      * to see if this is an outgoing or incoming message.
67      * Write a brief message to the print stream and
68      * output the message. The writeTo() method can throw
69      * SOAPException or IOException
70      */

71     protected void logToSystemOut(SOAPMessageContext smc) {
72         Boolean JavaDoc outboundProperty = (Boolean JavaDoc)
73             smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
74
75         if (outboundProperty.booleanValue()) {
76             out.println("\nOutbound message:");
77         } else {
78             out.println("\nInbound message:");
79         }
80
81         SOAPMessage JavaDoc message = smc.getMessage();
82         try {
83             message.writeTo(out);
84             out.println();
85         } catch (Exception JavaDoc e) {
86             out.println("Exception in handler: " + e);
87         }
88     }
89 }
90
Popular Tags