KickJava   Java API By Example, From Geeks To Geeks.

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


1 package test.wsdl.jaxrpchandlereval;
2
3 import javax.xml.rpc.handler.*;
4 import javax.xml.rpc.soap.*;
5 import javax.xml.namespace.*;
6 import javax.xml.soap.*;
7 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
8 import javax.xml.rpc.JAXRPCException JavaDoc;
9 import javax.xml.rpc.soap.*;
10 import javax.xml.rpc.*;
11 import org.w3c.dom.NodeList JavaDoc;
12
13
14 public class ServiceHandler2 implements Handler {
15
16     public ServiceHandler2() {
17         System.out.println("ServiceHandler2:Constructor");
18     }
19
20     public boolean handleRequest(MessageContext JavaDoc context) {
21         System.out.println("ServiceHandler2:handleRequest");
22
23          try {
24             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
25             SOAPMessage soapMsg = soapMsgCtx.getMessage();
26             SOAPPart sp = soapMsg.getSOAPPart();
27             SOAPEnvelope se = sp.getEnvelope();
28             SOAPBody sb = se.getBody();
29             SOAPHeader sh = se.getHeader();
30
31             Name xmlServicesHandler2HdrName =
32                 se.createName("ServiceHandler2-handleRequest", "", "");
33             SOAPHeaderElement xmlServicesHandler2Hdr =
34                 sh.addHeaderElement(xmlServicesHandler2HdrName);
35             xmlServicesHandler2Hdr.addTextNode(
36                 "Processed by ServiceHandler2Hdr2.handleRequest");
37
38             /*--- getElementsByTagName() does not work.
39             NodeList list = sb.getElementsByTagName("in0");
40             if (list.getLength() == 0) {
41                 throw new JAXRPCException("No such element : 'in0'");
42             }
43             SOAPElement elem = (SOAPElement) list.item(0);
44             String curBody = elem.getValue();
45             ---*/

46             String JavaDoc curBody = sb.toString();
47             if (curBody.indexOf("server-throw-soapfaultexception") >= 0) {
48                 String JavaDoc reason = "A FATAL EXCEPTION has occurred while processing ServiceHandler2.handleRequest";
49                 soapMsgCtx.setProperty("fault", reason);
50
51                 QName faultcode = new QName("Testimg Exception",
52                                             "http://example.org/security/");
53                 throw new SOAPFaultException(faultcode, reason, null, null);
54                 //throw new JAXRPCException(reason);
55
} else if (curBody.indexOf("server-return-false") >= 0) {
56                 soapMsgCtx.setProperty("fault",
57                         "An error has occurred while processing ServiceHandler2.handleRequest - returning false");
58                 return false;
59             }
60          } catch (SOAPException ex) {
61              throw new JAXRPCException JavaDoc(ex);
62          }
63         return true;
64     }
65
66     public boolean handleResponse(MessageContext JavaDoc context) {
67         System.out.println("ServiceHandler2:handleResponse");
68
69         try {
70             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
71             SOAPMessage soapMsg = soapMsgCtx.getMessage();
72             if (soapMsg == null) {
73                 soapMsg = prepareError(soapMsgCtx);
74             }
75
76             SOAPPart sp = soapMsg.getSOAPPart();
77             SOAPEnvelope se = sp.getEnvelope();
78             SOAPBody sb = se.getBody();
79             SOAPHeader sh = se.getHeader();
80
81             Name xmlServicesHandler2HdrName =
82                 se.createName("ServiceHandler2-handleResponse", "", "");
83             SOAPHeaderElement xmlServicesHandler2Hdr =
84                 sh.addHeaderElement(xmlServicesHandler2HdrName);
85             xmlServicesHandler2Hdr.addTextNode(
86                 "Processed by ServiceHandler2Hdr2.handleResponse");
87
88
89          } catch (Exception JavaDoc ex) {
90              throw new JAXRPCException JavaDoc(ex);
91          }
92         return true;
93     }
94
95     public SOAPMessage prepareError(SOAPMessageContext JavaDoc soapMsgCtx) throws Exception JavaDoc{
96         MessageFactory messageFactory = MessageFactory.newInstance();
97         SOAPMessage soapMsg = messageFactory.createMessage();
98         soapMsgCtx.setMessage(soapMsg);
99
100         String JavaDoc fault = (String JavaDoc)soapMsgCtx.getProperty("fault");
101         if (fault != null) {
102             SOAPFault soapFault =
103                 soapMsg.getSOAPPart().getEnvelope().getBody().addFault();
104             soapFault.setFaultString(fault);
105         }
106         return soapMsg;
107     }
108
109     public boolean handleFault(MessageContext JavaDoc context) {
110         System.out.println("\nServiceHandler2:handleFault");
111         try {
112             SOAPMessageContext JavaDoc soapMsgCtx = (SOAPMessageContext JavaDoc)context;
113             SOAPMessage soapMsg = soapMsgCtx.getMessage();
114
115             soapMsg = prepareError(soapMsgCtx);
116
117
118             // soapMsg.writeTo(System.out);
119

120             SOAPPart sp = soapMsg.getSOAPPart();
121             SOAPEnvelope se = sp.getEnvelope();
122             SOAPBody sb = se.getBody();
123             SOAPHeader sh = se.getHeader();
124
125             Name xmlServicesHandler2HdrName =
126                 se.createName("ServiceHandler2-handleFault", "", "");
127             SOAPHeaderElement xmlServicesHandler2Hdr =
128                 sh.addHeaderElement(xmlServicesHandler2HdrName);
129             xmlServicesHandler2Hdr.addTextNode(
130                 "Processed by ServiceHandler2Hdr2.handleFault");
131
132
133          } catch (Exception JavaDoc ex) {
134              ex.printStackTrace();
135          }
136         return true;
137     }
138
139     public void init(HandlerInfo config) {
140         System.out.println("ServiceHandler2:init");
141     }
142
143      public void destroy() {
144         System.out.println("ServiceHandler2:destroy");
145     }
146
147     public QName[] getHeaders() {
148         System.out.println("ServiceHandler2:getHeaders");
149         return null;
150     }
151
152 }
153
154
155
156
157
Popular Tags