Debian Codegraph @

test/wsdl/jaxrpchandlereval/ClientHandler2.java

raw
1 package test.wsdl.jaxrpchandlereval;
2
3 import javax.xml.namespace.QName;
4 import javax.xml.rpc.handler.Handler;
5 import javax.xml.rpc.handler.HandlerInfo;
6 import javax.xml.rpc.handler.MessageContext;
7 import javax.xml.rpc.handler.soap.SOAPMessageContext;
8 import javax.xml.soap.Name;
9 import javax.xml.soap.SOAPEnvelope;
10 import javax.xml.soap.SOAPHeader;
11 import javax.xml.soap.SOAPBody;
12 import javax.xml.soap.SOAPHeaderElement;
13 import javax.xml.soap.SOAPMessage;
14 import javax.xml.soap.SOAPPart;
15 import java.util.Iterator;
16 import javax.xml.soap.*;
17 import javax.xml.rpc.handler.soap.SOAPMessageContext;
18 import javax.xml.rpc.soap.*;
19 import javax.xml.rpc.*;
20
21 /**
22 */
23 public class ClientHandler2 implements Handler {
24
25 private final static String _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 context) {
37 System.out.println("ClientHandler2:handleRequest");
38 HandlerTracker.addClientHandler("clienthandler2.handleRequest");
39
40 if (context instanceof SOAPMessageContext) {
41 try {
42 SOAPMessageContext soapMsgCtx = (SOAPMessageContext)context;
43 SOAPMessage soapMsg = soapMsgCtx.getMessage();
44 SOAPPart sp = soapMsg.getSOAPPart();
45 SOAPEnvelope se = sp.getEnvelope();
46 SOAPBody sb = se.getBody();
47 SOAPHeader sh = se.getHeader();
48
49 Name name = se.createName("ClientHandler2-handleRequest", "", "");
50 SOAPHeaderElement hdr = sh.addHeaderElement(name);
51 hdr.addTextNode("Processed");
52
53 String 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 faultcode = new QName("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 context) {
80 System.out.println("ClientHandler2:handleResponse");
81 HandlerTracker.addClientHandler("clienthandler2.handleResponse");
82
83 if (context instanceof SOAPMessageContext) {
84 try {
85 SOAPMessageContext soapMsgCtx = (SOAPMessageContext)context;
86 SOAPMessage soapMsg = soapMsgCtx.getMessage();
87 if (soapMsg == null) {
88 soapMsg = prepareError(soapMsgCtx);
89 }
90 SOAPPart sp = soapMsg.getSOAPPart();
91 SOAPEnvelope se = sp.getEnvelope();
92 SOAPBody sb = se.getBody();
93 SOAPHeader sh = se.getHeader();
94
95 Name name = se.createName("ClientHandler2-handleResponse", "", "");
96 SOAPHeaderElement hdr = sh.addHeaderElement(name);
97 hdr.addTextNode("Processed");
98
99 } catch (SOAPException ex) {
100 ex.printStackTrace();
101 return false;
102 } catch (java.io.IOException ex) {
103 ex.printStackTrace();
104 return false;
105 } catch (Exception ex) {
106 throw new JAXRPCException(ex);
107 }
108
109 }
110 return true;
111 }
112
113 public SOAPMessage prepareError(SOAPMessageContext soapMsgCtx) throws Exception{
114 MessageFactory messageFactory = MessageFactory.newInstance();
115 SOAPMessage soapMsg = messageFactory.createMessage();
116 soapMsgCtx.setMessage(soapMsg);
117
118 String fault = (String)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 context) {
130 System.out.println("ClientHandler2:handleFault");
131 HandlerTracker.addClientHandler("clienthandler2.handleFault");
132 if (context instanceof SOAPMessageContext) {
133 try {
134 SOAPMessageContext soapMsgCtx = (SOAPMessageContext)context;
135 SOAPMessage soapMsg = soapMsgCtx.getMessage();
136 if (soapMsg == null) {
137 soapMsg = prepareError(soapMsgCtx);
138 }
139 SOAPPart sp = soapMsg.getSOAPPart();
140 SOAPEnvelope se = sp.getEnvelope();
141 SOAPBody sb = se.getBody();
142 SOAPHeader sh = se.getHeader();
143
144 Name name = se.createName("ClientHandler2-handleFault", "", "");
145 SOAPHeaderElement hdr = sh.addHeaderElement(name);
146 hdr.addTextNode("Processed");
147 } catch (SOAPException ex) {
148 ex.printStackTrace();
149 return false;
150 } catch (Exception 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 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[] getHeaders() {
179 System.out.println("ClientHandler2.getheaders");
180
181 return null;
182 }
183
184 }
185