KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > systest > handlers > TestSOAPHandler


1 package org.objectweb.celtix.systest.handlers;
2
3
4 import java.util.Map JavaDoc;
5 import java.util.Set JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 import javax.xml.namespace.QName JavaDoc;
9 import javax.xml.soap.SOAPBody JavaDoc;
10 import javax.xml.soap.SOAPMessage JavaDoc;
11 import javax.xml.ws.handler.MessageContext;
12 import javax.xml.ws.handler.soap.SOAPHandler;
13 import javax.xml.ws.handler.soap.SOAPMessageContext;
14
15 import org.w3c.dom.DOMException JavaDoc;
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Node JavaDoc;
18
19 /**
20  * Describe class TestSOAPHandler here.
21  *
22  *
23  * Created: Fri Oct 21 13:24:05 2005
24  *
25  * @author <a HREF="mailto:codea@iona.com">codea</a>
26  * @version 1.0
27  */

28 public class TestSOAPHandler<T extends SOAPMessageContext> extends TestHandlerBase
29     implements SOAPHandler<T> {
30
31     public TestSOAPHandler() {
32         this(true);
33     }
34
35     public TestSOAPHandler(boolean serverSide) {
36         super(serverSide);
37     }
38
39     // Implementation of javax.xml.ws.handler.soap.SOAPHandler
40

41     public final Set JavaDoc<QName JavaDoc> getHeaders() {
42         return null;
43     }
44   
45     public String JavaDoc getHandlerId() {
46         return "soapHandler" + getId();
47     }
48     
49     public final boolean handleMessage(T ctx) {
50
51         boolean continueProcessing = true;
52
53         try {
54             methodCalled("handleMessage");
55             printHandlerInfo("handleMessage", isOutbound(ctx));
56             Object JavaDoc b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
57             boolean outbound = (Boolean JavaDoc)b;
58             SOAPMessage JavaDoc msg = ctx.getMessage();
59
60             if (isServerSideHandler()) {
61                 if (outbound) {
62                     continueProcessing = true;
63                 } else {
64                     continueProcessing = getReturnValue(outbound, ctx);
65                     if (!continueProcessing) {
66                         outbound = true;
67                     }
68                 }
69
70                 if (outbound) {
71                     try {
72                         // append handler id to SOAP response message
73
SOAPBody JavaDoc body = msg.getSOAPBody();
74                         Node JavaDoc resp = body.getFirstChild();
75
76                         if (resp.getNodeName().contains("pingResponse")) {
77                             Node JavaDoc child = resp.getFirstChild();
78                             Document JavaDoc doc = resp.getOwnerDocument();
79                             Node JavaDoc info = doc.createElementNS(child.getNamespaceURI(), child.getLocalName());
80                             info.setPrefix("ns4");
81                             info.appendChild(doc.createTextNode(getHandlerId()));
82                             resp.appendChild(info);
83                             msg.saveChanges();
84                         }
85                     } catch (DOMException JavaDoc e) {
86                         e.printStackTrace();
87                     }
88                 } else {
89                     getHandlerInfoList(ctx).add(getHandlerId());
90                 }
91             }
92         } catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }
95         return continueProcessing;
96     }
97
98     public final boolean handleFault(T ctx) {
99         methodCalled("handleFault");
100         printHandlerInfo("handleFault", isOutbound(ctx));
101         return true;
102     }
103
104     public final void init(final Map JavaDoc map) {
105         methodCalled("init");
106
107     }
108
109     public final void destroy() {
110         methodCalled("destroy");
111     }
112
113     public final void close(MessageContext messageContext) {
114         methodCalled("close");
115     }
116
117     private boolean getReturnValue(boolean outbound, T ctx) {
118
119         if (outbound) {
120             return true;
121         }
122
123         boolean ret = true;
124         try {
125             SOAPMessage JavaDoc msg = ctx.getMessage();
126             SOAPBody JavaDoc body = msg.getSOAPBody();
127
128             if (body.getFirstChild().getFirstChild() == null) {
129                 return true;
130             }
131
132             Node JavaDoc commandNode = body.getFirstChild().getFirstChild().getFirstChild();
133             String JavaDoc arg = commandNode.getNodeValue();
134             String JavaDoc namespace = body.getFirstChild().getFirstChild().getNamespaceURI();
135             
136             StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(arg, " ");
137             String JavaDoc hid = strtok.nextToken();
138             String JavaDoc direction = strtok.nextToken();
139             String JavaDoc command = strtok.nextToken();
140             
141             if (getHandlerId().equals(hid)
142                 && "inbound".equals(direction)) {
143                 if ("stop".equals(command)) {
144
145                     // remove the incoming request body.
146
Document JavaDoc doc = body.getOwnerDocument();
147                     // build the SOAP response for this message
148
//
149
Node JavaDoc wrapper = doc.createElementNS(namespace, "pingResponse");
150                     wrapper.setPrefix("ns4");
151                     body.removeChild(body.getFirstChild());
152                     body.appendChild(wrapper);
153
154                     for (String JavaDoc info : getHandlerInfoList(ctx)) {
155                         // copy the the previously invoked handler list into the response.
156
// Ignore this handlers information as it will be added again later.
157
//
158
if (!info.contains(getHandlerId())) {
159                             Node JavaDoc newEl = doc.createElementNS(namespace, "HandlersInfo");
160                             newEl.setPrefix("ns4");
161                             newEl.appendChild(doc.createTextNode(info));
162                             wrapper.appendChild(newEl);
163                         }
164                     }
165                     ret = false;
166                 } else if ("throw".equals(command)) {
167                     //throwException(strtok.nextToken());
168
}
169             }
170
171         } catch (Exception JavaDoc e) {
172             e.printStackTrace();
173         }
174             
175         return ret;
176     }
177
178
179     public String JavaDoc toString() {
180         return getHandlerId();
181     }
182 }
183
Popular Tags