KickJava   Java API By Example, From Geeks To Geeks.

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


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.SOAPHeaderElement JavaDoc;
12 import javax.xml.soap.SOAPMessage JavaDoc;
13 import javax.xml.soap.SOAPPart JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  */

18 public class ServerHandler implements Handler JavaDoc {
19
20     private int _counter = 0;
21     private int _faultCounter = 0;
22
23     public final static String JavaDoc _actorURI = "myActorURI";
24
25     /**
26      * Constructor for ClientHandler.
27      */

28     public ServerHandler() {
29         super();
30     }
31
32     /**
33      * @see javax.xml.rpc.handler.Handler#handleRequest(MessageContext)
34      */

35     public boolean handleRequest(MessageContext JavaDoc context) {
36         System.out.println("Hey - in Handle request");
37         try {
38             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
39             SOAPMessage JavaDoc msg = smc.getMessage();
40             SOAPPart JavaDoc sp = msg.getSOAPPart();
41             SOAPEnvelope JavaDoc se = sp.getEnvelope();
42             SOAPHeader JavaDoc sh = se.getHeader();
43             Name JavaDoc name = se.createName("HeaderTest", "AXIS",
44                                       "http://xml.apache.org/axis");
45             Iterator JavaDoc iter = sh.extractHeaderElements(_actorURI);
46             while (iter.hasNext()) {
47                 SOAPHeaderElement JavaDoc she = (SOAPHeaderElement JavaDoc) iter.next();
48                 String JavaDoc counter = she.getAttributeValue(se.createName("counter","",""));
49                 _counter = Integer.parseInt(counter) + 1;
50
51                 String JavaDoc faultCounter = she.getAttributeValue(se.createName("faultCounter","",""));
52                 _faultCounter = Integer.parseInt(faultCounter) + 1; // Increment it to 2
53
}
54         } catch (Exception JavaDoc e) {
55             e.printStackTrace();
56         }
57
58         return true;
59     }
60
61     /**
62      * @see javax.xml.rpc.handler.Handler#handleResponse(MessageContext)
63      */

64     public boolean handleResponse(MessageContext JavaDoc context) {
65
66         System.out.println("Hey - in Handle response");
67         try {
68             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
69             SOAPMessage JavaDoc msg = smc.getMessage();
70             SOAPPart JavaDoc sp = msg.getSOAPPart();
71             SOAPEnvelope JavaDoc se = sp.getEnvelope();
72             SOAPHeader JavaDoc sh = se.getHeader();
73             Name JavaDoc name = se.createName("HeaderTest", "AXIS",
74                                       "http://xml.apache.org/axis");
75             SOAPHeaderElement JavaDoc she = sh.addHeaderElement(name);
76             she.addAttribute(se.createName("counter","",""), new Integer JavaDoc(_counter +1).toString());
77             she.setActor(_actorURI);
78         } catch (Exception JavaDoc e) {
79             e.printStackTrace();
80         }
81         return true;
82     }
83
84     /**
85      * @see javax.xml.rpc.handler.Handler#handleFault(MessageContext)
86      */

87     public boolean handleFault(MessageContext JavaDoc context) {
88         try {
89             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) context;
90             SOAPMessage JavaDoc msg = smc.getMessage();
91             SOAPPart JavaDoc sp = msg.getSOAPPart();
92             SOAPEnvelope JavaDoc se = sp.getEnvelope();
93             SOAPHeader JavaDoc sh = se.getHeader();
94             Name JavaDoc name = se.createName("HeaderTest", "AXIS",
95                                       "http://xml.apache.org/axis");
96             SOAPHeaderElement JavaDoc she = sh.addHeaderElement(name);
97
98             she.addAttribute(se.createName("faultCounter","",""), new Integer JavaDoc(_faultCounter +1).toString());
99             she.setActor(_actorURI);
100         } catch (Exception JavaDoc e) {
101             e.printStackTrace();
102         }
103
104         return true;
105     }
106
107     /**
108      * @see javax.xml.rpc.handler.Handler#init(HandlerInfo)
109      */

110     public void init(HandlerInfo JavaDoc config) {
111     }
112
113     /**
114      * @see javax.xml.rpc.handler.Handler#destroy()
115      */

116     public void destroy() {
117     }
118
119     /**
120      * @see javax.xml.rpc.handler.Handler#getHeaders()
121      */

122     public QName JavaDoc[] getHeaders() {
123         return null;
124     }
125
126 }
127
Popular Tags