1 3 11 package com.sun.enterprise.jbi.serviceengine.util.soap; 12 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 15 16 import java.util.Iterator ; 17 import java.util.logging.Level ; 18 import java.util.logging.Logger ; 19 import java.util.StringTokenizer ; 20 21 import java.net.HttpURLConnection ; 22 23 import javax.jbi.JBIException; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.jbi.messaging.MessagingException; 26 27 import javax.xml.soap.Detail ; 28 import javax.xml.soap.SOAPBody ; 29 import javax.xml.soap.SOAPEnvelope ; 30 import javax.xml.soap.SOAPException ; 31 import javax.xml.soap.SOAPHeader ; 32 import javax.xml.soap.SOAPMessage ; 33 import javax.xml.soap.SOAPPart ; 34 import javax.xml.soap.SOAPFault ; 35 import javax.xml.soap.AttachmentPart ; 36 import javax.xml.soap.SOAPElement ; 37 import javax.xml.soap.Name ; 38 39 import javax.activation.DataHandler ; 40 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.DOMException ; 43 import org.w3c.dom.NodeList ; 44 import org.w3c.dom.Attr ; 45 import org.w3c.dom.Element ; 46 import javax.xml.transform.dom.DOMSource ; 47 import javax.xml.transform.stream.StreamSource ; 48 import javax.xml.transform.TransformerFactory ; 49 import javax.xml.transform.Transformer ; 50 import javax.xml.transform.stream.StreamResult ; 51 import javax.xml.transform.Source ; 52 53 import javax.xml.parsers.DocumentBuilder ; 54 import javax.xml.parsers.DocumentBuilderFactory ; 55 import javax.xml.parsers.FactoryConfigurationError ; 56 import javax.xml.parsers.ParserConfigurationException ; 57 63 public class MessageNormalizerImpl implements MessageNormalizer 64 { 65 68 private Logger mLogger; 69 70 73 private StringTranslator mStringTranslator; 74 75 79 public MessageNormalizerImpl() 80 { 81 mLogger = Logger.getLogger(this.getClass().getPackage().getName()); 82 mStringTranslator = new StringTranslator(this.getClass().getPackage().getName(), this.getClass().getClassLoader()); 83 } 84 85 94 public void normalizeMessage( 95 SOAPWrapper soapWrapper, NormalizedMessage normalizedMessage, 96 Operation operation) 97 throws JBIException 98 { 99 if ( soapWrapper.getStatus() == HttpURLConnection.HTTP_INTERNAL_ERROR ) 100 { 101 normalizeFaultMessage( soapWrapper, normalizedMessage); 102 } 103 else 104 { 105 normalizeResponseMessage( soapWrapper, normalizedMessage, operation); 106 } 107 } 108 109 117 public void normalizeFaultMessage( 118 SOAPWrapper soapWrapper, NormalizedMessage normalizedMessage) 119 throws JBIException 120 { 121 122 try 123 { 124 SOAPMessage soapMessage = soapWrapper.getMessage(); 125 126 if (soapMessage != null) 127 { 128 SOAPPart soapPart = soapMessage.getSOAPPart(); 129 SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 130 SOAPBody soapBody = soapEnvelope.getBody(); 131 if ( soapBody.hasFault() ) 132 { 133 137 mLogger.fine(mStringTranslator.getString("SBC_FAULT_ELEMENT_FOUND")); 138 139 SOAPFault soapFault = soapBody.getFault(); 140 Detail soapDetail = soapFault.getDetail(); 141 if ( soapDetail != null ) 142 { 143 normalizedMessage.setContent( 144 new DOMSource ( 145 getChildElement(soapDetail))); 146 SOAPHeader soapHeader = soapEnvelope.getHeader(); 148 149 if (soapHeader != null) 150 { 151 normalizedMessage.setProperty( 152 SOAPConstants.HEADER_PROPERTY_NAME, 153 soapHeader); 154 } 155 normalizedMessage.setProperty( 156 SOAPConstants.FAULT_STRING_PROPERTY_NAME, 157 soapFault.getFaultString()); 158 normalizedMessage.setProperty( 159 SOAPConstants.FAULT_CODE_PROPERTY_NAME, 160 extractFaultCode( soapFault.getFaultCode()) ); 161 } 162 else 163 { 164 throw new JBIException( soapFault.getFaultString() ); 167 } 168 169 170 } 171 else 172 { 173 mLogger.severe(mStringTranslator.getString("SBC_ALGORITHM_ERROR")); 175 } 176 } 177 } 178 catch (SOAPException soapException) 179 { 180 mLogger.severe ( mStringTranslator.getString("SBC_NORMALIZE_FAULT_MESSAGE_FAILURE") ); 181 182 mLogger.severe( mStringTranslator.getString("SBC_ERROR_DETAILS") ); 183 JBIException jbiException = 184 new JBIException( 185 mStringTranslator.getString( 186 "SBC_NORMALIZE_FAULT_MESSAGE_FAILURE") ); 187 jbiException.initCause(soapException); 188 throw jbiException; 189 } 190 } 191 192 193 202 public void normalizeResponseMessage( 203 SOAPWrapper soapWrapper, NormalizedMessage normalizedMessage, 204 Operation operation) 205 throws JBIException 206 { 207 mLogger.fine( mStringTranslator.getString("SBC_NORMALIZE_SOAP_MESSAGE") ); 208 try 209 { 210 SOAPMessage soapMessage = soapWrapper.getMessage(); 211 212 if (soapMessage != null) 213 { 214 SOAPPart soapPart = soapMessage.getSOAPPart(); 215 SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 216 SOAPBody soapBody = soapEnvelope.getBody(); 217 218 org.w3c.dom.Node bodyContents = getBodyContentsAsNode(soapBody); 222 DOMSource ds = new DOMSource (bodyContents); 223 normalizedMessage.setContent(ds); 225 227 228 normalizeAttachments(soapMessage, normalizedMessage); 230 231 SOAPHeader soapHeader = soapEnvelope.getHeader(); 233 234 if (soapHeader != null) 235 { 236 } 239 } 240 241 Iterator messageProperties = soapWrapper.getProperties(); 242 243 for (; messageProperties.hasNext();) 244 { 245 String propertyName = (String ) messageProperties.next(); 246 normalizedMessage.setProperty( 247 propertyName, soapWrapper.getValue(propertyName)); 248 } 249 } 250 catch (RuntimeException runtimeException) 251 { 252 mLogger.severe ( mStringTranslator.getString("SBC_NORMALIZE_SOAP_MESSAGE_FAILURE_RT_EXP") ); 254 255 JBIException jbiException = new JBIException( 256 mStringTranslator.getString( 257 "SBC_NORMALIZE_SOAP_MESSAGE_FAILURE") ); 258 jbiException.initCause(runtimeException); 259 throw jbiException; 260 } 261 catch (SOAPException soapException) 262 { 263 mLogger.severe( mStringTranslator.getString("SBC_NORMALIZE_SOAP_MESSAGE_FAILURE") ); 264 mLogger.severe( mStringTranslator.getString("SBC_ERROR_DETAILS", soapException.toString()) ); 265 266 JBIException jbiException = new JBIException( 267 mStringTranslator.getString( 268 "SBC_NORMALIZE_SOAP_MESSAGE_FAILURE") ); 269 jbiException.initCause(soapException); 270 throw jbiException; 271 } 272 catch(Exception ex) 273 { 274 mLogger.severe("Some Exception while dumping Source."); 275 ex.printStackTrace(); 276 } 277 278 mLogger.fine( mStringTranslator.getString("SBC_SUCCESS_NORMALISE_SUCCESS") ); 279 } 280 281 private org.w3c.dom.Node getBodyContentsAsNode(SOAPBody body) 282 { 283 org.w3c.dom.Node dNode = null; 284 try 285 { 286 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 287 DocumentBuilder builder = factory.newDocumentBuilder(); 288 Document mDoc = builder.newDocument(); 289 290 Iterator iter2 = body.getChildElements(); 291 while (iter2.hasNext()) { 294 javax.xml.soap.Node n = (javax.xml.soap.Node )iter2.next(); 295 if(n instanceof SOAPElement ) 296 { 297 dNode = createDOMNodeFromSOAPNode(n, mDoc); 298 break; 300 } 301 } 302 } 303 catch(ParserConfigurationException pce) 304 { 305 pce.printStackTrace(); 306 return null; 307 } 308 catch(Exception e) 309 { 310 e.printStackTrace(); 311 return null; 312 } 313 314 return dNode; 315 316 } 317 318 322 private org.w3c.dom.Node createDOMNodeFromSOAPNode(javax.xml.soap.Node soapNode, Document document) 323 throws ParserConfigurationException 324 { 325 org.w3c.dom.Node result = null; 326 327 if (soapNode instanceof SOAPElement ) 330 { 331 SOAPElement soapElement = (SOAPElement ) soapNode; 332 Name name = soapElement.getElementName(); 333 334 if( (name.getURI().length() != 0) && (name.getQualifiedName().length() != 0) ) 336 result = document.createElementNS(name.getURI(), name.getQualifiedName()); 337 else if(name.getLocalName() != null) 338 result = document.createElement(name.getLocalName()); 339 else 340 { 341 } 343 344 for (Iterator iter = soapElement.getAllAttributes();iter.hasNext(); ) 347 { 348 Name attrName = (Name ) iter.next(); 349 String attrValue = soapElement.getAttributeValue(attrName); 350 351 Attr attribute = null; 353 if (attrName.getURI() == null) 354 attribute = document.createAttribute(attrName.getQualifiedName()); 355 else 356 attribute = document.createAttributeNS(attrName.getURI(),attrName.getQualifiedName()); 357 358 attribute.setValue(attrValue); 359 360 ((Element ) result).setAttributeNodeNS(attribute); 361 } 362 363 for (Iterator iter = soapElement.getChildElements(); iter.hasNext(); ) 367 { 368 javax.xml.soap.Node childSOAPNode = (javax.xml.soap.Node ) iter.next(); 369 appendSOAPNodeToDOMNode(document, result, childSOAPNode); 370 } 371 } 372 else if (soapNode instanceof javax.xml.soap.Text ) 373 { 374 javax.xml.soap.Text textNode = (javax.xml.soap.Text ) soapNode; 375 String textValue = textNode.getValue(); 376 377 if (textNode.isComment()) 379 result = document.createComment(textValue); 380 else 381 result = document.createTextNode(textValue); 382 } 383 else 384 { 385 } 387 388 return (result); 389 } 390 391 396 private void appendSOAPNodeToDOMNode(Document document, org.w3c.dom.Node domNode,javax.xml.soap.Node soapNode) 397 throws ParserConfigurationException 398 { 399 org.w3c.dom.Node newDOMNode = createDOMNodeFromSOAPNode(soapNode, document); 400 401 404 domNode.appendChild(newDOMNode); 405 } 406 407 408 private static void dump(Source source) throws Exception 409 { 410 TransformerFactory tf = TransformerFactory.newInstance(); 411 Transformer t = tf.newTransformer(); 412 StreamResult stdOut = new StreamResult (System.out); 413 414 System.out.println("[BEGIN_MESSAGE_DUMP]"); 415 t.transform(source, stdOut); 416 System.out.println("[END_MESSAGE_DUMP]"); 417 } 418 419 420 431 protected Node extractPayload(SOAPBody soapBody, Operation operation, boolean isFault) 432 throws JBIException 433 { 434 mLogger.fine( mStringTranslator.getString("SBC_EXTRACT_REQUEST_PAYLOAD") ); 435 436 return getChildElement(soapBody); 437 } 438 439 446 public boolean isFault(int responseCode) 447 { 448 return false; 449 } 450 451 458 private Node getChildElement(Node parentNode) 459 { 460 NodeList childNodes = parentNode.getChildNodes(); 461 Node currentNode = null; 462 Node elementNode = null; 463 464 for (int i = 0; i < childNodes.getLength(); i++) 465 { 466 currentNode = childNodes.item(i); 467 468 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 469 { 470 elementNode = currentNode; 471 472 break; 473 } 474 } 475 476 return elementNode; 477 } 478 479 486 private String extractFaultCode(String completeFaultCode) 487 { 488 String faultCode; 489 StringTokenizer tokenizer = new StringTokenizer (completeFaultCode, 490 ":"); 491 if ( tokenizer.countTokens() == 1) 492 { 493 faultCode = completeFaultCode; 494 } 495 else 496 { 497 tokenizer.nextToken(); 499 faultCode = tokenizer.nextToken(); 500 } 501 return faultCode; 502 } 503 504 513 private void normalizeAttachments(SOAPMessage soapMessage, 514 NormalizedMessage normalizedMessage) 515 throws SOAPException , MessagingException 516 { 517 if ( soapMessage != null) 518 { 519 if ( soapMessage.countAttachments() > 0 ) 520 { 521 Iterator attachmentIter = soapMessage.getAttachments(); 522 for (; attachmentIter.hasNext();) 523 { 524 AttachmentPart attachment = (AttachmentPart ) attachmentIter.next(); 525 DataHandler dataHandler = attachment.getDataHandler(); 526 String contentId = attachment.getContentId(); 527 normalizedMessage.addAttachment( contentId, dataHandler); 528 } 529 } 530 } 531 } 532 } 533 | Popular Tags |