KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jaxr > juddi > axis > JBossJuddiAxisProcessor


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jaxr.juddi.axis;
8
9 /*import org.apache.axis.AxisFault;
10 import org.apache.axis.Message;
11 import org.apache.axis.MessageContext;
12 import org.apache.axis.message.SOAPBodyElementAxisImpl;
13 import org.apache.axis.message.SOAPEnvelopeAxisImpl; */

14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.juddi.IRegistry;
18 import org.apache.juddi.datatype.RegistryObject;
19 import org.apache.juddi.datatype.response.DispositionReport;
20 import org.apache.juddi.error.BusyException;
21 import org.apache.juddi.error.RegistryException;
22 import org.apache.juddi.error.UnsupportedException;
23 import org.apache.juddi.handler.DispositionReportHandler;
24 import org.apache.juddi.handler.HandlerMaker;
25 import org.apache.juddi.handler.IHandler;
26 import org.apache.juddi.registry.RegistryEngine;
27 import org.apache.juddi.registry.RegistryServlet;
28 import org.apache.juddi.util.Config;
29 import org.apache.juddi.util.xml.XMLUtils;
30 import org.jboss.axis.AxisFault;
31 import org.jboss.axis.Message;
32 import org.jboss.axis.MessageContext;
33 import org.jboss.axis.message.SOAPBodyElementAxisImpl;
34 import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38
39 /**
40  * JBoss Version of juddi Axis Processor
41  *
42  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
43  * @since Nov 20, 2004
44  */

45 public class JBossJuddiAxisProcessor
46 {
47 // private reference to the jUDDI logger
48
private static Log log = LogFactory.getLog(JBossJuddiAxisProcessor.class);
49
50     // jUDDI XML Handler maker
51
private static HandlerMaker maker = HandlerMaker.getInstance();
52
53     /**
54      * @param soapResponse
55      * @param messageContext
56      */

57     public JBossJuddiAxisProcessor(Message soapResponse, MessageContext messageContext)
58     {
59         /*
60         // get a new monitor from the MonitorFactory
61
62         Monitor monitor = MonitorFactory.getMonitor();
63
64         // if monitoring is turned on inspect the MessageContext
65
66         if (monitor != null)
67           monitor.inspectMessageContext(messageContext); */

68
69         // grab a reference to the SOAP request from
70
// the Message Context
71

72         Message soapRequest = messageContext.getRequestMessage();
73
74         // write the SOAP request XML out to the log (on debug)
75

76         try
77         {
78             log.debug(soapRequest.getSOAPPartAsString());
79         } catch (AxisFault af)
80         {
81             af.printStackTrace();
82         }
83
84         Element JavaDoc request = null;
85         Element JavaDoc response = null;
86         String JavaDoc function = null;
87         String JavaDoc generic = null;
88
89         try
90         {
91             // pull the uddi request xml element from
92
// the body of the soapRequest
93

94             SOAPEnvelopeAxisImpl env = soapRequest.getSOAPEnvelope();
95             SOAPBodyElementAxisImpl requestBody = env.getFirstBody();
96             request = requestBody.getAsDOM();
97
98             // make the monitor inspect the SOAP Body
99
// DOM element
100
/*if (monitor != null)
101               monitor.inspectUDDIRequest(request);*/

102
103             // grab the function name from this element -
104
// we'll need this to lookup the xml handler
105
// to use to unmarshal the xml into a juddi
106
// object.
107

108             function = request.getLocalName();
109
110             // grab the generic value - we'll need it in
111
// the event that an exception is thrown.
112

113             generic = request.getAttribute("generic");
114             if (generic == null)
115                 generic = IRegistry.UDDI_V2_GENERIC;
116
117             // lookup the appropriate xml handler, throw
118
// an UnsupportedException if one could not be
119
// located.
120

121             IHandler requestHandler = maker.lookup(function);
122             if (requestHandler == null)
123                 throw new UnsupportedException("The request " +
124                         "type is unknown: " + function);
125
126             // unmarshal the raw xml into an associated
127
// jUDDI request object.
128

129             RegistryObject uddiRequest = requestHandler.unmarshal(request);
130
131             // make the monitor inspect the UDDI request
132
// object
133
/*if (monitor != null)
134                 monitor.inspectRegistryObject(uddiRequest); */

135
136             // Determine if this message came from through
137
// the Publish, Inquiry or Admin API and handle
138
// it appropriately.
139

140             Object JavaDoc juddiServlet = messageContext.getProperty("transport.http.servlet");
141
142             // confirm that the the appropriate endpoint
143
// was used to invoke the selected jUDDI/UDDI
144
// function.
145

146             // if ((juddiServlet instanceof InquiryServlet) &&
147
if ((juddiServlet instanceof JBossJuddiInquiryServlet) &&
148                     (!(uddiRequest instanceof org.apache.juddi.datatype.request.Inquiry)))
149             {
150                 throw new RegistryException("Inquiry API " +
151                         "does not support function: " + function);
152             } else if (juddiServlet instanceof JBossJuddiPublishServlet &&
153                     (!(uddiRequest instanceof org.apache.juddi.datatype.request.Publish) &&
154                     !(uddiRequest instanceof org.apache.juddi.datatype.request.SecurityPolicy)))
155             {
156                 throw new RegistryException("Publish API " +
157                         "does not support function: " + function);
158             } else if ((juddiServlet instanceof JBossJuddiAdminServlet) && // Admin
159
(!(uddiRequest instanceof org.apache.juddi.datatype.request.Admin)))
160             {
161                 throw new RegistryException("Admin API " +
162                         "does not support function: " + function);
163             }
164
165             // grab a reference to the shared jUDDI registry
166
// instance (make sure it's running) and execute
167
// the requested UDDI function.
168

169             RegistryObject uddiResponse = null;
170
171             RegistryEngine registry = RegistryServlet.getRegistry();
172             if ((registry != null) && (registry.isAvailable()))
173                 uddiResponse = registry.execute(uddiRequest);
174             else
175                 throw new BusyException("The Registry is unavailable");
176
177             // create a new 'temp' XML element. This
178
// element is used as a container in which
179
// to marshal the UDDI response into.
180

181             Document JavaDoc document = XMLUtils.createDocument();
182             Element JavaDoc element = document.createElement("temp");
183
184             // lookup the appropriate response handler
185
// and marshal the juddi object into the
186
// appropriate xml format (we only support
187
// uddi v2.0 at this time) attaching results
188
// to the temporary 'temp' element.
189

190             IHandler responseHandler = maker.lookup(uddiResponse.getClass().getName());
191             responseHandler.marshal(uddiResponse, element);
192
193             // grab a reference to the 'temp' element's
194
// only child here (this has the effect of
195
// discarding the temp element) and appending
196
// this child to the soap response body.
197

198             response = (Element JavaDoc) element.getFirstChild();
199         } catch (RegistryException rex)
200         {
201             log.error(rex.getMessage(), rex);
202
203             String JavaDoc fCode = rex.getFaultCode();
204             String JavaDoc fString = rex.getFaultString();
205             String JavaDoc fActor = rex.getFaultActor();
206
207             DispositionReport dispRpt = rex.getDispositionReport();
208             if (dispRpt != null)
209             {
210                 dispRpt.setGeneric(generic);
211                 dispRpt.setOperator(Config.getOperator());
212             }
213
214             /*if (monitor != null)
215                 monitor.addMonitorFault(fString); */

216
217             response = createFault(fCode, fString, fActor, dispRpt);
218         } catch (AxisFault axf)
219         {
220             log.error(axf.getMessage(), axf);
221             String JavaDoc fCode = String.valueOf(axf.getFaultCode());
222             String JavaDoc fString = axf.getFaultString();
223             String JavaDoc fActor = axf.getFaultActor();
224
225             /*if (monitor != null)
226                 monitor.addMonitorFault(fString); */

227
228             response = createFault(fCode, fString, fActor, null);
229         } catch (Exception JavaDoc ex)
230         {
231             log.error(ex.getMessage(), ex);
232
233             String JavaDoc fCode = null;
234             String JavaDoc fString = ex.getMessage();
235             String JavaDoc fActor = null;
236
237             /*if (monitor != null)
238                 monitor.addMonitorFault(fString); */

239
240             response = createFault(fCode, fString, fActor, null);
241         } finally
242         {
243             // write the monitored information to the currently
244
// configured 'Monitor' implemneted registry (the
245
// default Monitor implementation writes the monitored
246
// information to a database table via JDBC).
247

248             /* if (monitor != null)
249                  monitor.log(); */

250         }
251
252         try
253         {
254
255             SOAPBodyElementAxisImpl soapRespBody = new SOAPBodyElementAxisImpl(response);
256             SOAPEnvelopeAxisImpl soapRespEnv = soapResponse.getSOAPEnvelope();
257             soapRespEnv.addBodyElement(soapRespBody);
258
259
260         } catch (AxisFault af)
261         {
262             af.printStackTrace();
263         }
264
265         // write the SOAP response XML out to the log (on debug)
266
try
267         {
268             log.debug(soapResponse.getSOAPPartAsString());
269         } catch (AxisFault af)
270         {
271             af.printStackTrace();
272         }
273
274     }
275
276     private static Element JavaDoc createFault(String JavaDoc fCode, String JavaDoc fString, String JavaDoc fActor, DispositionReport dispRpt)
277     {
278         // create a new 'Fault' XML element.
279

280         Document JavaDoc document = XMLUtils.createDocument();
281         Element JavaDoc fault = document.createElement("Fault");
282
283         if (fCode != null)
284         {
285             Element JavaDoc fCodeElement = document.createElement("faultcode");
286             fCodeElement.appendChild(document.createTextNode(fCode));
287             fault.appendChild(fCodeElement);
288         }
289
290         if (fString == null)
291             fString = "";
292
293         Element JavaDoc fStringElement = document.createElement("faultstring");
294         fStringElement.appendChild(document.createTextNode(fString));
295         fault.appendChild(fStringElement);
296
297         if (fActor != null)
298         {
299             Element JavaDoc fActorElement = document.createElement("faultactor");
300             fActorElement.appendChild(document.createTextNode(fActor));
301             fault.appendChild(fActorElement);
302         }
303
304         // check for a DispositionReport in the exception and if one exists,
305
// grab it, marshal it into xml and stuff it into a SOAP fault
306
// detail element.
307

308         if (dispRpt != null)
309         {
310             Element JavaDoc fDetailElement = document.createElement("detail");
311             IHandler handler = maker.lookup(DispositionReportHandler.TAG_NAME);
312             handler.marshal(dispRpt, fDetailElement);
313             fault.appendChild(fDetailElement);
314         }
315
316         return fault;
317     }
318 }
319
320
Popular Tags