KickJava   Java API By Example, From Geeks To Geeks.

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


1 package test.wsdl.jaxrpchandler;
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
17 /**
18  */

19 public class ClientHandler implements Handler JavaDoc {
20
21     private final static String JavaDoc _actorURI = "myActorURI";
22     /**
23      * Constructor for ClientHandler.
24      */

25     public ClientHandler() {
26         super();
27     }
28
29     /**
30      * @see javax.xml.rpc.handler.Handler#handleRequest(MessageContext)
31      */

32     public boolean handleRequest(MessageContext JavaDoc context) {
33         System.out.println("Hey - in Handle request");
34         try {
35             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
36             SOAPMessage JavaDoc msg = smc.getMessage();
37             SOAPPart JavaDoc sp = msg.getSOAPPart();
38             SOAPEnvelope JavaDoc se = sp.getEnvelope();
39             SOAPHeader JavaDoc sh = se.getHeader();
40             Name JavaDoc name =
41             se.createName(
42                          "HeaderTest",
43                          "AXIS",
44                          "http://xml.apache.org/axis");
45             SOAPHeaderElement JavaDoc she = sh.addHeaderElement(name);
46             she.setActor(_actorURI);
47             she.addAttribute(se.createName("counter", "", ""), "1");
48             she.addAttribute(se.createName("faultCounter", "", ""), "1");
49         } catch (Exception JavaDoc e) {
50             e.printStackTrace();
51         }
52
53         return true;
54     }
55
56     /**
57      * @see javax.xml.rpc.handler.Handler#handleResponse(MessageContext)
58      */

59     public boolean handleResponse(MessageContext JavaDoc context) {
60         System.out.println("Hey - in Handle response");
61         try {
62             String JavaDoc counter = null;
63             String JavaDoc faultCounter = null;
64
65             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
66             SOAPMessage JavaDoc msg = smc.getMessage();
67             SOAPPart JavaDoc sp = msg.getSOAPPart();
68             SOAPEnvelope JavaDoc se = sp.getEnvelope();
69             SOAPHeader JavaDoc sh = se.getHeader();
70             SOAPBody JavaDoc sb = se.getBody();
71             Name JavaDoc name =
72             se.createName(
73                          "HeaderTest",
74                          "AXIS",
75                          "http://xml.apache.org/axis");
76             Iterator JavaDoc iter = sh.extractHeaderElements(_actorURI);
77             while (iter.hasNext()) {
78                 SOAPHeaderElement JavaDoc she = (SOAPHeaderElement JavaDoc) iter.next();
79                 counter =
80                 she.getAttributeValue(se.createName("counter", "", ""));
81                 System.out.println(
82                                   "The counter in the element sent back is " + counter);
83
84                 faultCounter =
85                 she.getAttributeValue(se.createName("faultCounter", "", ""));
86                 System.out.println(
87                                   "The faultCounter in the element sent back is " + faultCounter);
88
89             }
90
91             if ((counter != null) && (counter.equals("3")) && (!sb.hasFault())) {
92                 JAXRPCHandlerTestCase.completeRoundtrip();
93             }
94
95             if ((faultCounter != null) && (faultCounter.equals("3")) && (sb.hasFault())) {
96                 JAXRPCHandlerTestCase.setFaultRoundtrip(Integer.parseInt(faultCounter));
97             }
98
99
100         } catch (Exception JavaDoc e) {
101             e.printStackTrace();
102         }
103
104         return false;
105     }
106
107     /**
108      * @see javax.xml.rpc.handler.Handler#handleFault(MessageContext)
109      */

110     public boolean handleFault(MessageContext JavaDoc context) {
111         return false;
112     }
113
114     /**
115      * @see javax.xml.rpc.handler.Handler#init(HandlerInfo)
116      */

117     public void init(HandlerInfo JavaDoc config) {
118     }
119
120     /**
121      * @see javax.xml.rpc.handler.Handler#destroy()
122      */

123     public void destroy() {
124     }
125
126     /**
127      * @see javax.xml.rpc.handler.Handler#getHeaders()
128      */

129     public QName JavaDoc[] getHeaders() {
130         return null;
131     }
132
133 }
134
135
Popular Tags