1 22 package org.jboss.jaxr.juddi; 23 24 import org.apache.juddi.IRegistry; 25 import org.apache.juddi.datatype.RegistryObject; 26 import org.apache.juddi.datatype.response.DispositionReport; 27 import org.apache.juddi.datatype.response.ErrInfo; 28 import org.apache.juddi.datatype.response.Result; 29 import org.apache.juddi.error.BusyException; 30 import org.apache.juddi.error.FatalErrorException; 31 import org.apache.juddi.error.RegistryException; 32 import org.apache.juddi.error.UnsupportedException; 33 import org.apache.juddi.handler.HandlerMaker; 34 import org.apache.juddi.handler.IHandler; 35 import org.apache.juddi.registry.RegistryEngine; 36 import org.apache.juddi.registry.RegistryServlet; 37 import org.apache.juddi.util.Config; 38 import org.apache.juddi.util.xml.XMLUtils; 39 import org.jboss.logging.Logger; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.NamedNodeMap ; 43 import org.w3c.dom.Node ; 44 import org.w3c.dom.NodeList ; 45 46 import javax.servlet.ServletException ; 47 import javax.servlet.http.HttpServlet ; 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 50 import javax.xml.parsers.DocumentBuilder ; 51 import javax.xml.parsers.DocumentBuilderFactory ; 52 import javax.xml.parsers.ParserConfigurationException ; 53 import javax.xml.soap.Detail ; 54 import javax.xml.soap.MessageFactory ; 55 import javax.xml.soap.Name ; 56 import javax.xml.soap.SOAPBody ; 57 import javax.xml.soap.SOAPBodyElement ; 58 import javax.xml.soap.SOAPElement ; 59 import javax.xml.soap.SOAPException ; 60 import javax.xml.soap.SOAPFactory ; 61 import javax.xml.soap.SOAPFault ; 62 import javax.xml.soap.SOAPMessage ; 63 import javax.xml.soap.SOAPPart ; 64 65 import java.io.ByteArrayOutputStream ; 66 import java.io.IOException ; 67 import java.util.Vector ; 68 69 76 public class JUDDIServlet extends HttpServlet 77 { 78 79 private static final long serialVersionUID = 8768916717023791095L; 80 81 private static DocumentBuilder docBuilder = null; 83 84 private static HandlerMaker maker = HandlerMaker.getInstance(); 86 87 private static Logger log = Logger.getLogger(JUDDIServlet.class); 88 89 92 public void doGet(HttpServletRequest req, HttpServletResponse res) 93 throws ServletException , IOException 94 { 95 res.setHeader("Allow", "POST"); 96 res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "The request " + 97 "method 'GET' is not allowed by UDDI API."); 98 } 99 100 public void doPost(HttpServletRequest req, HttpServletResponse res) 101 throws ServletException , IOException 102 { 103 res.setContentType("text/xml; charset=utf-8"); 104 105 SOAPMessage soapReq = null; 106 SOAPMessage soapRes = null; 107 108 try 109 { 110 MessageFactory msgFactory = MessageFactory.newInstance(); 111 soapReq = msgFactory.createMessage(null, req.getInputStream()); 112 soapRes = msgFactory.createMessage(); 113 if(log.isTraceEnabled()) 114 { 115 ByteArrayOutputStream bs = new ByteArrayOutputStream (); 116 soapReq.writeTo(bs); 117 log.trace("Request received::"+bs.toString()); 118 } 119 120 SOAPBody soapReqBody = soapReq.getSOAPBody(); 121 Element uddiReq = (Element ) soapReqBody.getFirstChild(); 122 if (uddiReq == null) 123 throw new FatalErrorException("A UDDI request was not " + 124 "found in the SOAP message."); 125 126 String function = uddiReq.getLocalName(); 127 if ((function == null) || (function.trim().length() == 0)) 128 throw new FatalErrorException("The name of the UDDI request " + 129 "could not be identified."); 130 IHandler requestHandler = maker.lookup(function); 131 if (requestHandler == null) 132 throw new UnsupportedException("The UDDI request " + 133 "type specified is unknown: " + function); 134 135 String generic = uddiReq.getAttribute("generic"); 136 if (generic == null) 137 throw new FatalErrorException("A UDDI generic attribute " + 138 "value was not found for UDDI request: " + function + " (The " + 139 "'generic' attribute must be present)"); 140 else if (!generic.equals(IRegistry.UDDI_V2_GENERIC)) 141 throw new UnsupportedException("Currently only UDDI v2 " + 142 "requests are supported. The generic attribute value " + 143 "received was: " + generic); 144 145 148 RegistryObject uddiReqObj = requestHandler.unmarshal(uddiReq); 149 if(uddiReqObj == null) 150 throw new FatalErrorException("Uddi Request is null"); 151 152 156 RegistryObject uddiResObj = null; 157 RegistryEngine registry = RegistryServlet.getRegistry(); 158 if ((registry != null) && (registry.isAvailable())) 159 uddiResObj = registry.execute(uddiReqObj); 160 else 161 throw new BusyException("The Registry is currently unavailable."); 162 163 167 IHandler responseHandler = maker.lookup(uddiResObj.getClass().getName()); 168 if (responseHandler == null) 169 throw new FatalErrorException("The response object " + 170 "type is unknown: " + uddiResObj.getClass().getName()); 171 172 175 DocumentBuilder docBuilder = getDocumentBuilder(); 176 Document document = docBuilder.newDocument(); 177 Element element = document.createElement("temp"); 178 179 184 responseHandler.marshal(uddiResObj, element); 185 log.debug("Response that will be sent:"); 186 log.debug(XMLUtils.toString((Element ) element.getFirstChild())); 187 188 193 195 Element el = (Element ) element.getFirstChild(); 196 soapRes = this.createSOAPMessage(el); 197 } catch (Exception ex) { 199 String faultCode = null; 201 String faultString = null; 202 String faultActor = null; 203 204 String errno = null; 206 String errCode = null; 207 String errMsg = null; 208 209 if (ex instanceof RegistryException) 210 { 211 212 log.error("RegistryException::",ex); 213 214 RegistryException rex = (RegistryException) ex; 215 216 faultCode = rex.getFaultCode(); faultString = rex.getFaultString(); faultActor = rex.getFaultActor(); 220 DispositionReport dispRpt = rex.getDispositionReport(); 221 if (dispRpt != null) 222 { 223 Result result = null; 224 ErrInfo errInfo = null; 225 226 Vector results = dispRpt.getResultVector(); 227 if ((results != null) && (!results.isEmpty())) 228 result = (Result) results.elementAt(0); 229 230 if (result != null) 231 { 232 errno = String.valueOf(result.getErrno()); errInfo = result.getErrInfo(); 234 235 if (errInfo != null) 236 { 237 errCode = errInfo.getErrCode(); errMsg = errInfo.getErrMsg(); } 240 } 241 } 242 } else 243 { 244 245 log.error(ex.getMessage(), ex); 246 247 faultCode = "Server"; 248 faultString = ex.getMessage(); 249 faultActor = null; 250 251 252 errno = String.valueOf(Result.E_FATAL_ERROR); 253 errCode = Result.lookupErrCode(Result.E_FATAL_ERROR); 254 errMsg = Result.lookupErrText(Result.E_FATAL_ERROR); 255 } 256 257 try 258 { 259 SOAPBody soapResBody = soapRes.getSOAPBody(); 260 SOAPFault soapFault = soapResBody.addFault(); 261 if(faultCode == null) 262 faultCode = "Unavailable"; 263 soapFault.setFaultCode(faultCode); 264 soapFault.setFaultString(faultString); 265 soapFault.setFaultActor(faultActor); 266 267 Detail faultDetail = soapFault.addDetail(); 268 269 SOAPElement dispRpt = faultDetail.addChildElement("dispositionReport", "", IRegistry.UDDI_V2_NAMESPACE); 270 dispRpt.setAttribute("generic", IRegistry.UDDI_V2_GENERIC); 271 dispRpt.setAttribute("operator", Config.getOperator()); 272 273 SOAPElement result = dispRpt.addChildElement("result"); 274 result.setAttribute("errno", errno); 275 276 SOAPElement errInfo = result.addChildElement("errInfo"); 277 errInfo.setAttribute("errCode", errCode); 278 errInfo.setValue(errMsg); 279 } catch (Exception e) 280 { log.error("A serious error has occured while assembling the SOAP Fault.", e); 282 } 283 } finally 284 { 285 try 286 { 287 if(log.isTraceEnabled()) 288 { 289 ByteArrayOutputStream bs = new ByteArrayOutputStream (); 290 soapRes.writeTo(bs); 291 log.trace("Response being sent::"+bs.toString()); 292 } 293 soapRes.writeTo(res.getOutputStream()); 294 } catch (SOAPException sex) 295 { 296 log.error("SOAPException::",sex); 297 } 298 } 299 } 300 301 304 private DocumentBuilder getDocumentBuilder() 305 { 306 if (docBuilder == null) 307 docBuilder = createDocumentBuilder(); 308 return docBuilder; 309 } 310 311 314 private synchronized DocumentBuilder createDocumentBuilder() 315 { 316 if (docBuilder != null) 317 return docBuilder; 318 319 try 320 { 321 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 322 factory.setNamespaceAware(true); 323 325 docBuilder = factory.newDocumentBuilder(); 326 } catch (ParserConfigurationException pcex) 327 { 328 log.error("ParserConfigurationException::",pcex); 329 } 330 331 return docBuilder; 332 } 333 334 private SOAPMessage createSOAPMessage(Element elem) throws Exception 335 { 336 String prefix = "uddi"; 337 MessageFactory msgFactory = MessageFactory.newInstance(); 338 SOAPFactory factory = SOAPFactory.newInstance(); 339 340 SOAPMessage message = msgFactory.createMessage(); 341 message.getSOAPHeader().detachNode(); 342 SOAPPart soapPart = message.getSOAPPart(); 343 SOAPBody soapBody = soapPart.getEnvelope().getBody(); 344 String uddins = IRegistry.UDDI_V2_NAMESPACE; 346 Name bodyName = factory.createName(elem.getNodeName(), prefix, uddins); 347 SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName); 348 bodyElement.addNamespaceDeclaration(prefix,uddins); 349 appendAttributes(bodyElement, elem.getAttributes(), factory); 350 appendElements(bodyElement,elem.getChildNodes(), factory); 351 return message; 352 } 353 354 private void appendAttributes(SOAPElement bodyElement, NamedNodeMap nnm, 355 SOAPFactory factory) throws SOAPException 356 { 357 int len = nnm != null ? nnm.getLength() : 0; 358 for (int i = 0; i < len; i++) 359 { 360 Node n = nnm.item(i); 361 String nodename = n.getNodeName(); 362 String nodevalue = n.getNodeValue(); 363 if("xmlns".equals(nodename)) 364 continue; 365 if("xml:lang".equals(nodename)) 367 { 368 Name xmlLang = factory.createName("lang","xml", 369 "http://www.w3.org/TR/REC-xml/"); 370 bodyElement.addAttribute(xmlLang, nodevalue); 371 } 372 else 373 bodyElement.addAttribute(factory.createName(nodename), nodevalue); 374 } 375 } 376 377 private void appendElements(SOAPElement bodyElement, NodeList nlist, 378 SOAPFactory factory) 379 throws SOAPException 380 { 381 String prefix = "uddi"; 382 String uddins = IRegistry.UDDI_V2_NAMESPACE; 383 int len = nlist != null ? nlist.getLength() : 0; 384 385 for (int i = 0; i < len; i++) 386 { 387 Node node = nlist.item(i); 388 short nodeType = node != null ? node.getNodeType() : -100; 389 if (Node.ELEMENT_NODE == nodeType) 390 { 391 Element el = (Element )node; 392 Name name = factory.createName(el.getNodeName(), prefix,uddins); 393 SOAPElement attachedEl = bodyElement.addChildElement(name); 394 appendAttributes(attachedEl, el.getAttributes(), factory); 395 appendElements(attachedEl, el.getChildNodes(), factory); 396 } else if (nodeType == Node.TEXT_NODE) 397 { 398 bodyElement.addTextNode(node.getNodeValue()); 399 } 400 } 401 } 402 } 403 | Popular Tags |