KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > transport > axis > AxisProcessor


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.transport.axis;
17
18 import java.util.Vector JavaDoc;
19
20 import javax.xml.soap.Detail JavaDoc;
21 import javax.xml.soap.SOAPBody JavaDoc;
22 import javax.xml.soap.SOAPElement JavaDoc;
23 import javax.xml.soap.SOAPFault JavaDoc;
24
25 import org.apache.axis.AxisFault;
26 import org.apache.axis.Message;
27 import org.apache.axis.MessageContext;
28 import org.apache.axis.message.SOAPBodyElement;
29 import org.apache.axis.message.SOAPEnvelope;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.juddi.IRegistry;
33 import org.apache.juddi.datatype.RegistryObject;
34 import org.apache.juddi.datatype.response.DispositionReport;
35 import org.apache.juddi.datatype.response.ErrInfo;
36 import org.apache.juddi.datatype.response.Result;
37 import org.apache.juddi.error.BusyException;
38 import org.apache.juddi.error.RegistryException;
39 import org.apache.juddi.error.UnsupportedException;
40 import org.apache.juddi.handler.HandlerMaker;
41 import org.apache.juddi.handler.IHandler;
42 import org.apache.juddi.registry.RegistryEngine;
43 import org.apache.juddi.registry.RegistryServlet;
44 import org.apache.juddi.util.Config;
45 import org.apache.juddi.util.xml.XMLUtils;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48
49 /**
50  * @author Steve Viens (sviens@apache.org)
51  * @author Anou Mana (anou_mana@users.sourceforge.net)
52  */

53 public class AxisProcessor
54 {
55   // private reference to the jUDDI logger
56
private static Log log = LogFactory.getLog(AxisProcessor.class);
57
58   // jUDDI XML Handler maker
59
private static HandlerMaker maker = HandlerMaker.getInstance();
60
61   /**
62    * @param soapResponse
63    * @param messageContext
64    */

65   public AxisProcessor(Message soapResponse,MessageContext messageContext)
66   {
67     // the soap request and response objects.
68

69     SOAPEnvelope soapReqEnv = null;
70     SOAPEnvelope soapResEnv = null;
71
72     // grab a reference to the SOAP request from
73
// the Message Context
74

75     Message soapRequest = messageContext.getRequestMessage();
76
77     // write the SOAP request XML out to the log (on debug)
78

79     try { log.debug(soapRequest.getSOAPPartAsString()); }
80     catch(AxisFault af) {
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 soap request and response objects from
92
// raw request and response message objects.
93

94       soapReqEnv = soapRequest.getSOAPEnvelope();
95       soapResEnv = soapResponse.getSOAPEnvelope();
96
97       // pull the uddi request xml element from
98
// the body of the soapRequest
99

100       SOAPBodyElement requestBody = soapReqEnv.getFirstBody();
101       request = requestBody.getAsDOM();
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       // Determine if this message came from through
132
// the Publish, Inquiry or Admin API and handle
133
// it appropriately.
134

135       Object JavaDoc juddiServlet = messageContext.getProperty("transport.http.servlet");
136
137       // confirm that the the appropriate endpoint
138
// was used to invoke the selected jUDDI/UDDI
139
// function.
140

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

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

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

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

194       response = (Element JavaDoc)element.getFirstChild();
195       SOAPBodyElement soapRespBody = new SOAPBodyElement(response);
196       SOAPEnvelope soapRespEnv = soapResponse.getSOAPEnvelope();
197       soapRespEnv.addBodyElement(soapRespBody);
198     }
199     catch(Exception JavaDoc ex)
200     {
201       log.error(ex.getMessage(),ex);
202
203       String JavaDoc faultCode = null;
204       String JavaDoc faultString = null;
205       String JavaDoc faultActor = null;
206       String JavaDoc errno = null;
207       String JavaDoc errCode = null;
208       String JavaDoc errText = null;
209       
210       // All RegistryException and subclasses of RegistryException
211
// should contain values for populating a SOAP Fault as well
212
// as a UDDI DispositionReport with specific information
213
// about the problem.
214
//
215
// We've got to dig out the dispositionReport and populate
216
// the SOAP Fault 'detail' element with this information.
217

218       if (ex instanceof RegistryException)
219       {
220         RegistryException rex = (RegistryException)ex;
221         
222         faultCode = rex.getFaultCode(); // SOAP Fault faultCode
223
faultString = rex.getFaultString(); // SOAP Fault faultString
224
faultActor = rex.getFaultActor(); // SOAP Fault faultActor
225

226         DispositionReport dispRpt = rex.getDispositionReport();
227         if (dispRpt != null)
228         {
229           Result result = null;
230           ErrInfo errInfo = null;
231         
232           Vector JavaDoc results = dispRpt.getResultVector();
233           if ((results != null) && (!results.isEmpty()))
234             result = (Result)results.elementAt(0);
235         
236           if (result != null)
237           {
238             errno = String.valueOf(result.getErrno()); // UDDI DispositionReport errno
239
errInfo = result.getErrInfo();
240           
241             if (errInfo != null)
242             {
243               errCode = errInfo.getErrCode(); // UDDI DispositionReport errCode
244
errText = errInfo.getErrMsg(); // UDDI DispositionReport errMsg
245
}
246           }
247         }
248       }
249       else
250       {
251         // All other exceptions (other than RegistryException
252
// and subclasses) are either a result of a jUDDI
253
// configuration problem or something that we *should*
254
// be catching and converting to a RegistryException
255
// but are not (yet!).
256

257         faultCode = "Server";
258         faultString = ex.getMessage();
259         faultActor = null;
260           
261         errno = String.valueOf(Result.E_FATAL_ERROR);
262         errCode = Result.lookupErrCode(Result.E_FATAL_ERROR);
263         errText = Result.lookupErrText(Result.E_FATAL_ERROR) +
264                   " An internal UDDI server error has " +
265                   "occurred. Please report this error " +
266                   "to the UDDI server administrator.";
267       }
268             
269       // We should have everything we need to assemble
270
// the SOAPFault so lets piece it together and
271
// send it on it's way.
272

273       try {
274         SOAPBody JavaDoc soapResBody = soapResEnv.getBody();
275         SOAPFault JavaDoc soapFault = soapResBody.addFault();
276         soapFault.setFaultCode(faultCode);
277         soapFault.setFaultString(faultString);
278         soapFault.setFaultActor(faultActor);
279         
280         Detail JavaDoc faultDetail = soapFault.addDetail();
281         
282         SOAPElement JavaDoc dispRpt = faultDetail.addChildElement("dispositionReport","",IRegistry.UDDI_V2_NAMESPACE);
283         dispRpt.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
284         dispRpt.setAttribute("operator",Config.getOperator());
285         
286         SOAPElement JavaDoc result = dispRpt.addChildElement("result");
287         result.setAttribute("errno",errno);
288         
289         SOAPElement JavaDoc errInfo = result.addChildElement("errInfo");
290         errInfo.setAttribute("errCode",errCode);
291         errInfo.setValue(errText);
292       }
293       catch (Exception JavaDoc e) {
294         e.printStackTrace();
295       }
296     }
297
298     // write the SOAP response XML out to the log (on debug)
299
try { log.debug(soapResponse.getSOAPPartAsString()); }
300     catch(AxisFault af) {
301       af.printStackTrace();
302     }
303   }
304 }
305
Popular Tags