01: /*
02: * Copyright (c) 2005 Sun Microsystems, Inc.
03: * All rights reserved.
04: */
05: package demo.handlers.common;
06:
07: import java.io.PrintStream;
08: import java.util.Map;
09: import java.util.Set;
10:
11: import javax.xml.namespace.QName;
12: import javax.xml.soap.SOAPMessage;
13: import javax.xml.ws.handler.MessageContext;
14: import javax.xml.ws.handler.soap.SOAPHandler;
15: import javax.xml.ws.handler.soap.SOAPMessageContext;
16:
17: /*
18: * This simple SOAPHandler will output the contents of incoming
19: * and outgoing messages.
20: */
21: public class LoggingHandler implements SOAPHandler<SOAPMessageContext> {
22:
23: private PrintStream out;
24:
25: public LoggingHandler() {
26: setLogStream(System.out);
27: }
28:
29: protected final void setLogStream(PrintStream ps) {
30: out = ps;
31: }
32:
33: public void init(Map c) {
34: System.out.println("LoggingHandler : init() Called....");
35: }
36:
37: public Set<QName> getHeaders() {
38: return null;
39: }
40:
41: public boolean handleMessage(SOAPMessageContext smc) {
42: System.out.println("LoggingHandler : handleMessage Called....");
43: logToSystemOut(smc);
44: return true;
45: }
46:
47: public boolean handleFault(SOAPMessageContext smc) {
48: System.out.println("LoggingHandler : handleFault Called....");
49: logToSystemOut(smc);
50: return true;
51: }
52:
53: // nothing to clean up
54: public void close(MessageContext messageContext) {
55: System.out.println("LoggingHandler : close() Called....");
56: }
57:
58: // nothing to clean up
59: public void destroy() {
60: System.out.println("LoggingHandler : destroy() Called....");
61: }
62:
63: /*
64: * Check the MESSAGE_OUTBOUND_PROPERTY in the context
65: * to see if this is an outgoing or incoming message.
66: * Write a brief message to the print stream and
67: * output the message. The writeTo() method can throw
68: * SOAPException or IOException
69: */
70: protected void logToSystemOut(SOAPMessageContext smc) {
71: Boolean outboundProperty = (Boolean) smc
72: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
73:
74: if (outboundProperty.booleanValue()) {
75: out.println("\nOutbound message:");
76: } else {
77: out.println("\nInbound message:");
78: }
79:
80: SOAPMessage message = smc.getMessage();
81: try {
82: message.writeTo(out);
83: out.println();
84: } catch (Exception e) {
85: out.println("Exception in handler: " + e);
86: }
87: }
88: }
|