KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class ClientHandler2 implements Handler JavaDoc {
24
25 private final static String JavaDoc _actorURI = "myActorURI";
26 /**
27  * Constructor for ClientHandler2.
28  */

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

36 public boolean handleRequest(MessageContext JavaDoc context) {
37     System.out.println("ClientHandler2:handleRequest");
38     HandlerTracker.addClientHandler("clienthandler2.handleRequest");
39
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("ClientHandler2-handleRequest", "", "");
50             SOAPHeaderElement JavaDoc hdr = sh.addHeaderElement(name);
51             hdr.addTextNode("Processed");
52
53             String JavaDoc instruction = sb.toString();
54
55             if (instruction.indexOf("client-throw-jaxrpcexception") >= 0) {
56                 soapMsgCtx.setProperty("fault", "Throwing a client side exception from ClientHandler2.handleRequest");
57
58                 QName JavaDoc faultcode = new QName JavaDoc("Testimg Exception",
59                                             "http://example.org/security/");
60                 // According to JAX-RPC spec client handlers cannot throw SOAPFaultException
61
throw new JAXRPCException();
62             } else if (instruction.indexOf("client-return-false") >= 0) {
63                 soapMsgCtx.setProperty("fault", "Returning false from ClientHandler2.handleRequest");
64                 return false;
65             }
66
67
68         } catch (SOAPException ex) {
69             ex.printStackTrace();
70             return false;
71         }
72     }
73     return true;
74 }
75
76 /**
77  * @see javax.xml.rpc.handler.Handler#handleResponse(MessageContext)
78  */

79 public boolean handleResponse(MessageContext JavaDoc context) {
80     System.out.println("ClientHandler2:handleResponse");
81     HandlerTracker.addClientHandler("clienthandler2.handleResponse");
82
83     if (context instanceof SOAPMessageContext JavaDoc) {
84         try {
85             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
86             SOAPMessage JavaDoc soapMsg = soapMsgCtx.getMessage();
87             if (soapMsg == null) {
88                 soapMsg = prepareError(soapMsgCtx);
89             }
90             SOAPPart JavaDoc sp = soapMsg.getSOAPPart();
91             SOAPEnvelope JavaDoc se = sp.getEnvelope();
92             SOAPBody JavaDoc sb = se.getBody();
93             SOAPHeader JavaDoc sh = se.getHeader();
94
95             Name JavaDoc name = se.createName("ClientHandler2-handleResponse", "", "");
96             SOAPHeaderElement JavaDoc hdr = sh.addHeaderElement(name);
97             hdr.addTextNode("Processed");
98
99         } catch (SOAPException ex) {
100             ex.printStackTrace();
101             return false;
102         } catch (java.io.IOException JavaDoc ex) {
103             ex.printStackTrace();
104             return false;
105         } catch (Exception JavaDoc ex) {
106              throw new JAXRPCException(ex);
107          }
108
109     }
110     return true;
111 }
112
113 public SOAPMessage JavaDoc prepareError(SOAPMessageContext JavaDoc soapMsgCtx) throws Exception JavaDoc{
114         MessageFactory messageFactory = MessageFactory.newInstance();
115         SOAPMessage JavaDoc soapMsg = messageFactory.createMessage();
116         soapMsgCtx.setMessage(soapMsg);
117
118         String JavaDoc fault = (String JavaDoc)soapMsgCtx.getProperty("fault");
119         if (fault != null) {
120             SOAPFault soapFault =
121                 soapMsg.getSOAPPart().getEnvelope().getBody().addFault();
122             soapFault.setFaultString(fault);
123         }
124         return soapMsg;
125 }
126 /**
127  * @see javax.xml.rpc.handler.Handler#handleFault(MessageContext)
128  */

129 public boolean handleFault(MessageContext JavaDoc context) {
130     System.out.println("ClientHandler2:handleFault");
131     HandlerTracker.addClientHandler("clienthandler2.handleFault");
132     if (context instanceof SOAPMessageContext JavaDoc) {
133         try {
134             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
135             SOAPMessage JavaDoc soapMsg = soapMsgCtx.getMessage();
136             if (soapMsg == null) {
137                 soapMsg = prepareError(soapMsgCtx);
138             }
139             SOAPPart JavaDoc sp = soapMsg.getSOAPPart();
140             SOAPEnvelope JavaDoc se = sp.getEnvelope();
141             SOAPBody JavaDoc sb = se.getBody();
142             SOAPHeader JavaDoc sh = se.getHeader();
143
144             Name JavaDoc name = se.createName("ClientHandler2-handleFault", "", "");
145             SOAPHeaderElement JavaDoc hdr = sh.addHeaderElement(name);
146             hdr.addTextNode("Processed");
147         } catch (SOAPException ex) {
148             ex.printStackTrace();
149             return false;
150         } catch (Exception JavaDoc ex) {
151             ex.printStackTrace();
152
153              throw new JAXRPCException(ex);
154          }
155     }
156     return true;
157 }
158
159 /**
160  * @see javax.xml.rpc.handler.Handler#init(HandlerInfo)
161  */

162 public void init(HandlerInfo JavaDoc config) {
163     System.out.println("ClientHandler2.init");
164
165 }
166
167 /**
168  * @see javax.xml.rpc.handler.Handler#destroy()
169  */

170 public void destroy() {
171     System.out.println("ClientHandler2.destroy");
172
173 }
174
175 /**
176  * @see javax.xml.rpc.handler.Handler#getHeaders()
177  */

178 public QName JavaDoc[] getHeaders() {
179     System.out.println("ClientHandler2.getheaders");
180
181     return null;
182 }
183
184 }
185
186
Popular Tags