KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > jaxrpchandlereval > ClientHandler


1 package test.wsdl.jaxrpchandlereval;
2
3 import javax.xml.namespace.QName JavaDoc;
4 import javax.xml.rpc.handler.Handler JavaDoc;
5 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
6 import javax.xml.rpc.handler.MessageContext JavaDoc;
7 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
8 import javax.xml.soap.Name JavaDoc;
9 import javax.xml.soap.SOAPEnvelope JavaDoc;
10 import javax.xml.soap.SOAPHeader JavaDoc;
11 import javax.xml.soap.SOAPBody JavaDoc;
12 import javax.xml.soap.SOAPHeaderElement JavaDoc;
13 import javax.xml.soap.SOAPMessage JavaDoc;
14 import javax.xml.soap.SOAPPart JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import javax.xml.soap.*;
17 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
18 import javax.xml.rpc.soap.*;
19 import javax.xml.rpc.*;
20
21
22 /**
23  */

24 public class ClientHandler implements Handler JavaDoc {
25
26 private final static String JavaDoc _actorURI = "myActorURI";
27 /**
28  * Constructor for ClientHandler.
29  */

30 public ClientHandler() {
31     System.out.println("ClientHandler:Constructor");
32 }
33
34 /**
35  * @see javax.xml.rpc.handler.Handler#handleRequest(MessageContext)
36  */

37 public boolean handleRequest(MessageContext JavaDoc context) {
38     System.out.println("ClientHandler:handleRequest");
39     HandlerTracker.addClientHandler("clienthandler1.handleRequest");
40     if (context instanceof SOAPMessageContext JavaDoc) {
41         try {
42             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
43             SOAPMessage JavaDoc soapMsg = soapMsgCtx.getMessage();
44             SOAPPart JavaDoc sp = soapMsg.getSOAPPart();
45             SOAPEnvelope JavaDoc se = sp.getEnvelope();
46             SOAPBody JavaDoc sb = se.getBody();
47             SOAPHeader JavaDoc sh = se.getHeader();
48
49             Name JavaDoc name = se.createName("ClientHandler-handleRequest", "", "");
50             SOAPHeaderElement JavaDoc hdr = sh.addHeaderElement(name);
51             hdr.addTextNode("Processed");
52
53             String JavaDoc instruction = sb.toString();
54             if (instruction.indexOf("client-return-false") >= 0) {
55                 soapMsgCtx.setProperty("fault", "Returning false from ClientHandler.handleRequest");
56                 return false;
57             }
58         } catch (SOAPException ex) {
59             ex.printStackTrace();
60             return false;
61         }
62     }
63     return true;
64 }
65
66 public SOAPMessage JavaDoc prepareError(SOAPMessageContext JavaDoc soapMsgCtx) throws Exception JavaDoc{
67         MessageFactory messageFactory = MessageFactory.newInstance();
68         SOAPMessage JavaDoc soapMsg = messageFactory.createMessage();
69         soapMsgCtx.setMessage(soapMsg);
70
71         String JavaDoc fault = (String JavaDoc)soapMsgCtx.getProperty("fault");
72         if (fault != null) {
73             SOAPFault soapFault =
74                 soapMsg.getSOAPPart().getEnvelope().getBody().addFault();
75             soapFault.setFaultString(fault);
76             soapFault.setFaultCode("client.Exception");
77         }
78         return soapMsg;
79 }
80
81     private void trackServerHandler(SOAPHeader JavaDoc sh) {
82         Iterator JavaDoc i = sh.getChildElements();
83         while (i.hasNext()) {
84             Node n = (Node) i.next();
85             if (n instanceof Text) {
86                 continue;
87             }
88             SOAPElement e = (SOAPElement) n;
89             String JavaDoc local = e.getLocalName();
90             if (local.equals("ServiceHandler2-handleFault")) {
91                 HandlerTracker.addServerHandler("serverhandler2.handleFault");
92             } else if (local.equals("ServiceHandler1-handleFault")) {
93                 HandlerTracker.addServerHandler("serverhandler1.handleFault");
94             } else if (local.equals("ServiceHandler2-handleResponse")) {
95                 HandlerTracker.addServerHandler("serverhandler2.handleResponse");
96             } else if (local.equals("ServiceHandler1-handleResponse")) {
97                 HandlerTracker.addServerHandler("serverhandler1.handleResponse");
98             }
99         }
100     }
101
102
103 /**
104  * @see javax.xml.rpc.handler.Handler#handleResponse(MessageContext)
105  */

106 public boolean handleResponse(MessageContext JavaDoc context) {
107     System.out.println("ClientHandler:handleResponse");
108     HandlerTracker.addClientHandler("clienthandler1.handleResponse");
109
110     if (context instanceof SOAPMessageContext JavaDoc) {
111         try {
112             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
113             SOAPMessage JavaDoc soapMsg = soapMsgCtx.getMessage();
114             if (soapMsg == null) {
115                 soapMsg = prepareError(soapMsgCtx);
116             }
117             SOAPPart JavaDoc sp = soapMsg.getSOAPPart();
118             SOAPEnvelope JavaDoc se = sp.getEnvelope();
119             SOAPBody JavaDoc sb = se.getBody();
120             SOAPHeader JavaDoc sh = se.getHeader();
121
122             trackServerHandler(sh);
123
124             Name JavaDoc name = se.createName("ClientHandler-handleResponse", "", "");
125             SOAPHeaderElement JavaDoc hdr = sh.addHeaderElement(name);
126             hdr.addTextNode("Processed");
127         } catch (SOAPException ex) {
128             ex.printStackTrace();
129             return false;
130         } catch (Exception JavaDoc ex) {
131             throw new JAXRPCException (ex);
132         }
133     }
134     return true;
135 }
136
137 /**
138  * @see javax.xml.rpc.handler.Handler#handleFault(MessageContext)
139  */

140 public boolean handleFault(MessageContext JavaDoc context) {
141     System.out.println("ClientHandler:handleFault");
142     HandlerTracker.addClientHandler("clienthandler1.handleFault");
143     if (context instanceof SOAPMessageContext JavaDoc) {
144         try {
145             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
146             SOAPMessage JavaDoc soapMsg = soapMsgCtx.getMessage();
147             SOAPPart JavaDoc sp = soapMsg.getSOAPPart();
148             SOAPEnvelope JavaDoc se = sp.getEnvelope();
149             SOAPBody JavaDoc sb = se.getBody();
150             SOAPHeader JavaDoc sh = se.getHeader();
151
152             Name JavaDoc name = se.createName("ClientHandler-handleFault", "", "");
153             SOAPHeaderElement JavaDoc hdr = sh.addHeaderElement(name);
154             hdr.addTextNode("Processed");
155         } catch (SOAPException ex) {
156             ex.printStackTrace();
157             return false;
158         }
159     }
160     return false;
161 }
162
163 /**
164  * @see javax.xml.rpc.handler.Handler#init(HandlerInfo)
165  */

166 public void init(HandlerInfo JavaDoc config) {
167     System.out.println("ClientHandler.init");
168
169 }
170
171 /**
172  * @see javax.xml.rpc.handler.Handler#destroy()
173  */

174 public void destroy() {
175     System.out.println("ClientHandler.destroy");
176 }
177
178 /**
179  * @see javax.xml.rpc.handler.Handler#getHeaders()
180  */

181 public QName JavaDoc[] getHeaders() {
182     System.out.println("ClientHandler.getheaders");
183
184     return null;
185 }
186
187 }
188
189
Popular Tags