1 16 package org.apache.juddi.registry; 17 18 import java.io.IOException ; 19 import java.util.Vector ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServlet ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.soap.Detail ; 29 import javax.xml.soap.MessageFactory ; 30 import javax.xml.soap.SOAPBody ; 31 import javax.xml.soap.SOAPElement ; 32 import javax.xml.soap.SOAPException ; 33 import javax.xml.soap.SOAPFault ; 34 import javax.xml.soap.SOAPMessage ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.juddi.IRegistry; 39 import org.apache.juddi.datatype.RegistryObject; 40 import org.apache.juddi.datatype.response.DispositionReport; 41 import org.apache.juddi.datatype.response.ErrInfo; 42 import org.apache.juddi.datatype.response.Result; 43 import org.apache.juddi.error.BusyException; 44 import org.apache.juddi.error.FatalErrorException; 45 import org.apache.juddi.error.RegistryException; 46 import org.apache.juddi.error.UnsupportedException; 47 import org.apache.juddi.handler.HandlerMaker; 48 import org.apache.juddi.handler.IHandler; 49 import org.apache.juddi.util.Config; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Element ; 52 53 56 public abstract class AbstractService extends HttpServlet 57 { 58 private static Log log = LogFactory.getLog(AbstractService.class); 60 61 private static DocumentBuilder docBuilder = null; 63 64 67 public void doGet(HttpServletRequest req, HttpServletResponse res) 68 throws ServletException , IOException 69 { 70 res.setHeader("Allow","POST"); 71 res.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED,"Use of the " + 72 "HTTP request method 'GET' is not allowed by UDDI specification."); 73 } 74 75 78 public void doPost(HttpServletRequest req, HttpServletResponse res) 79 throws ServletException , IOException 80 { 81 res.setContentType("text/xml; charset=utf-8"); 82 83 SOAPMessage soapReq = null; 84 SOAPMessage soapRes = null; 85 86 try 87 { 88 92 MessageFactory msgFactory = MessageFactory.newInstance(); 93 soapReq = msgFactory.createMessage(null,req.getInputStream()); 94 soapRes = msgFactory.createMessage(); 95 96 102 SOAPBody soapReqBody = soapReq.getSOAPBody(); 103 Element uddiReq = (Element )soapReqBody.getFirstChild(); 104 if (uddiReq == null) 105 throw new FatalErrorException("A UDDI request was not " + 106 "found in the SOAP message."); 107 108 115 String operation = uddiReq.getLocalName(); 116 if ((operation == null) || (operation.trim().length() == 0)) 117 throw new FatalErrorException("The UDDI service operation " + 118 "could not be identified."); 119 120 126 String version = uddiReq.getAttribute("generic"); 127 if (version == null) 128 throw new FatalErrorException("A UDDI generic attribute " + 129 "value was not found for UDDI request: "+operation+" (The " + 130 "'generic' attribute must be present)"); 131 132 136 validateRequest(operation,version,uddiReq); 137 138 141 HandlerMaker maker = HandlerMaker.getInstance(); 142 IHandler requestHandler = maker.lookup(operation); 143 if (requestHandler == null) 144 throw new UnsupportedException("The UDDI service operation " + 145 "specified is unknown or unsupported: " +operation); 146 147 150 RegistryObject uddiReqObj = requestHandler.unmarshal(uddiReq); 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 186 191 document.appendChild(element.getFirstChild()); 192 soapRes.getSOAPBody().addDocument(document); 193 } 194 catch(Exception ex) { 196 String faultCode = null; 198 String faultString = null; 199 String faultActor = null; 200 201 String errno = null; 203 String errCode = null; 204 String errText = null; 205 206 214 if (ex instanceof RegistryException) 215 { 216 221 log.error(ex.getMessage()); 222 223 RegistryException rex = (RegistryException)ex; 224 225 faultCode = rex.getFaultCode(); faultString = rex.getFaultString(); faultActor = rex.getFaultActor(); 229 DispositionReport dispRpt = rex.getDispositionReport(); 230 if (dispRpt != null) 231 { 232 Result result = null; 233 ErrInfo errInfo = null; 234 235 Vector results = dispRpt.getResultVector(); 236 if ((results != null) && (!results.isEmpty())) 237 result = (Result)results.elementAt(0); 238 239 if (result != null) 240 { 241 errno = String.valueOf(result.getErrno()); errInfo = result.getErrInfo(); 243 244 if (errInfo != null) 245 { 246 errCode = errInfo.getErrCode(); errText = errInfo.getErrMsg(); } 249 } 250 } 251 } 252 else if (ex instanceof SOAPException ) 253 { 254 log.error(ex.getMessage()); 255 256 262 faultCode = "Client"; 263 faultString = ex.getMessage(); 264 faultActor = null; 265 266 272 errno = String.valueOf(Result.E_FATAL_ERROR); 273 errCode = Result.lookupErrCode(Result.E_FATAL_ERROR); 274 errText = Result.lookupErrText(Result.E_FATAL_ERROR) + 275 " " + ex.getMessage(); 276 } 277 else { 279 285 log.error(ex.getMessage(),ex); 286 287 293 faultCode = "Server"; 294 faultString = ex.getMessage(); 295 faultActor = null; 296 297 302 errno = String.valueOf(Result.E_FATAL_ERROR); 303 errCode = Result.lookupErrCode(Result.E_FATAL_ERROR); 304 errText = Result.lookupErrText(Result.E_FATAL_ERROR) + 305 " An internal UDDI server error has " + 306 "occurred. Please report this error " + 307 "to the UDDI server administrator."; 308 } 309 310 314 try { 315 SOAPBody soapResBody = soapRes.getSOAPBody(); 316 SOAPFault soapFault = soapResBody.addFault(); 317 soapFault.setFaultCode(faultCode); 318 soapFault.setFaultString(faultString); 319 soapFault.setFaultActor(faultActor); 320 321 324 Detail faultDetail = soapFault.addDetail(); 325 326 SOAPElement dispRpt = faultDetail.addChildElement("dispositionReport","",IRegistry.UDDI_V2_NAMESPACE); 327 dispRpt.setAttribute("generic",IRegistry.UDDI_V2_GENERIC); 328 dispRpt.setAttribute("operator",Config.getOperator()); 329 330 SOAPElement result = dispRpt.addChildElement("result"); 331 result.setAttribute("errno",errno); 332 333 SOAPElement errInfo = result.addChildElement("errInfo"); 334 errInfo.setAttribute("errCode",errCode); 335 errInfo.setValue(errText); 336 } 337 catch (Exception e) { log.error("A serious error has occured while assembling the SOAP Fault.",e); 339 } 340 } 341 finally 342 { 343 try { 344 soapRes.setProperty(SOAPMessage.WRITE_XML_DECLARATION,"true"); 345 soapRes.writeTo(res.getOutputStream()); 346 } 347 catch(SOAPException sex) { 348 log.error(sex); 349 } 350 } 351 } 352 353 356 public abstract void validateRequest(String operation,String generic,Element uddiReq) 357 throws RegistryException; 358 359 362 private DocumentBuilder getDocumentBuilder() 363 { 364 if (docBuilder == null) 365 docBuilder = createDocumentBuilder(); 366 return docBuilder; 367 } 368 369 372 private synchronized DocumentBuilder createDocumentBuilder() 373 { 374 if (docBuilder != null) 375 return docBuilder; 376 377 try { 378 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 379 382 docBuilder = factory.newDocumentBuilder(); 383 } 384 catch(ParserConfigurationException pcex) { 385 pcex.printStackTrace(); 386 } 387 388 return docBuilder; 389 } 390 } | Popular Tags |