FileDocCategorySizeDatePackage
WrappedMessage.javaAPI DocGlassfish v2 API20538Wed Jul 04 08:20:20 BST 2007com.sun.enterprise.jbi.serviceengine.comm

WrappedMessage

public class WrappedMessage extends com.sun.xml.ws.api.message.Message implements com.sun.enterprise.jbi.serviceengine.util.JBIConstants, com.sun.enterprise.jbi.serviceengine.util.soap.SOAPConstants
author
bhavani

Fields Summary
private static Logger
logger
private static DocumentBuilderFactory
dbf
Source
wrappedMessage
Holds the contents of the wrapped message.
com.sun.xml.ws.api.message.Message
abstractMessage
Variables used for wrapping the message.
QName
wsdlMessageType
String
wsdlBindingStyle
String
wsdlMessageName
List
wsdlOrderedParts
int[]
wsdlPartBindings
private boolean
log
Constructors Summary
public WrappedMessage()

        if(dbf == null) {
            dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
        }
        setLog();
    
Methods Summary
public com.sun.xml.ws.api.message.Messagecopy()

        return null;
    
public com.sun.xml.ws.api.message.HeaderListgetHeaders()

        return abstractMessage.getHeaders();
    
private org.w3c.dom.NodegetNodeValue(org.w3c.dom.Document d, java.lang.String localName)

        NodeList nl = d.getElementsByTagNameNS("", localName);
        Node n = (nl != null && nl.getLength() > 0)
        ? nl.item(0).getFirstChild()
        : null;
        return n;
    
public java.lang.StringgetPayloadLocalPart()

        return abstractMessage.getPayloadLocalPart();
    
public java.lang.StringgetPayloadNamespaceURI()

        return abstractMessage.getPayloadNamespaceURI();
    
public booleanhasHeaders()

        return abstractMessage.hasHeaders();
    
public booleanhasPayload()

        return abstractMessage.hasPayload();
    
public booleanisFault()

        return abstractMessage.isFault();
    
public javax.xml.soap.SOAPMessagereadAsSOAPMessage()

        return abstractMessage.readAsSOAPMessage();
    
public javax.xml.transform.SourcereadEnvelopeAsSource()

        return abstractMessage.readEnvelopeAsSource();
    
public javax.xml.stream.XMLStreamReaderreadPayload()

        return abstractMessage.readPayload();
    
public TreadPayloadAsJAXB(javax.xml.bind.Unmarshaller unmarshaller)

        return (T) abstractMessage.readPayloadAsJAXB(unmarshaller);
    
public TreadPayloadAsJAXB(com.sun.xml.bind.api.Bridge bridge)

        return abstractMessage.readPayloadAsJAXB(bridge);
    
public javax.xml.transform.SourcereadPayloadAsSource()

        return wrappedMessage;
    
public voidsetAbstractMessage(com.sun.xml.ws.api.message.Message abstractMessage)

 // represents whether the part is bound to 'header', 'body', or 'attachment'.
    
        
        this.abstractMessage = abstractMessage;
    
private voidsetLog()

    
       
        if(logger.isLoggable(Level.FINE)) {
            log = true;
        }
    
private voidsetMessage(byte[] data)

        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        String usedWith = System.getProperty(USED_WITH);
        if(usedWith != null && usedWith.indexOf(USED_WITH_HTTP_SOAP_BC) != -1) {
            DocumentBuilder db = dbf.newDocumentBuilder();
            wrappedMessage = new DOMSource(db.parse(bais));
        } else {
            wrappedMessage = new StreamSource(bais);
        }
    
public voidsetWSDLBindingStyle(java.lang.String wsdlBindingStyle)

        this.wsdlBindingStyle = wsdlBindingStyle;
    
public voidsetWSDLMessageName(java.lang.String wsdlMessageName)

        this.wsdlMessageName = wsdlMessageName;
    
public voidsetWSDLMessageType(javax.xml.namespace.QName wsdlMessageType)

        this.wsdlMessageType = wsdlMessageType;
    
public voidsetWSDLOrderedParts(java.util.List wsdlOrderedParts)

        this.wsdlOrderedParts = wsdlOrderedParts;
    
public voidsetWSDLPartBindings(int[] wsdlPartBindings)

        this.wsdlPartBindings = wsdlPartBindings;
    
public voidwrap()

        
        if(JavaEEServiceEngineContext.getInstance().isServiceMix()) {
            if(log) {
                logger.log(Level.FINE, "Skipping the wrapping...");
            }
            wrappedMessage = abstractMessage.readPayloadAsSource();
        } else {
            if(RPC_STYLE.equalsIgnoreCase(wsdlBindingStyle)) {
                new RPCStyleWrapper().wrap();
            } else {
                new DocumentStyleWrapper().wrap();
            }
        }
    
public voidwrapFault(java.lang.String operationName, com.sun.enterprise.jbi.serviceengine.util.soap.EndpointMetaData emd)

        
        if(JavaEEServiceEngineContext.getInstance().isServiceMix()) {
            if(log) {
                logger.log(Level.FINE, "Skipping the wrapping...");
            }
            wrappedMessage = abstractMessage.readPayloadAsSource();
            return;
        }
        
        DOMResult result = new DOMResult();
        try {
            Transformer t = TF.newTransformer();
            t.transform(abstractMessage.readPayloadAsSource(), result);
        } catch(Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        Document d = (Document)result.getNode();
        
        if(log) {
            logger.log(Level.FINE, "Fault received from JAX-WS : " +
                    UnWrappedMessage.toString(d));
        }
        
        Node faultDetail = getNodeValue(d, FAULT_DETAIL_ELEMENT);
        
        /*
         * Caused by RuntimeException thrown from JAX-WS.
         */
        if(faultDetail == null) {
            logger.log(Level.WARNING, 
                    "RuntimeException thrown from the JAX-WS. No <detail> found.");
            RuntimeException rt_ex = new RuntimeException(
                    "RuntimeException thrown from the JAX-WS. No details found.");
            // don't send service engine stack trace.
            rt_ex.setStackTrace(new StackTraceElement[]{}); 
            throw rt_ex;
        }
        
        javax.wsdl.Message wsdlMsg = null;
        try {
            wsdlMsg = emd.getFaultMessage(operationName,
                    faultDetail.getLocalName(), faultDetail.getNamespaceURI());
        } catch(Exception ex) {
            logger.log(Level.WARNING, ex.getMessage(), ex);
        }

        /*
         * Caused by RuntimeException thrown from the Application.
         */
        if(wsdlMsg == null) {
            logger.log(Level.WARNING, "RuntimeException thrown from the " +
                    "Application. Fault is not defined in the WSDL");
            
            Node n = getNodeValue(d, FAULT_STRING_ELEMENT);
            RuntimeException rt_ex = new RuntimeException(n.getTextContent());
            // don't send service engine stack trace.
            rt_ex.setStackTrace(new StackTraceElement[]{}); 
            throw rt_ex;
        }
        
        /*
         * Caused by the Checked exception thrown from the Application.
         */
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            XMLStreamWriter writer = XOF.createXMLStreamWriter(baos);
            writer.writeStartDocument();
            String wsdlMessageName = faultDetail.getLocalName();
            this.wsdlMessageType = wsdlMsg.getQName();
            writer.writeStartElement(WRAPPER_MESSAGE_QNAME);
            writeMessageAttributes(writer, wsdlMessageName);
            writer.writeStartElement(WRAPPER_PART_QNAME);
            writer.writeCharacters(""); // Force completion of open elems
            DOMUtil.UTIL.writeNode(faultDetail, writer);
            writer.writeEndElement();
            writer.writeEndElement();
            writer.writeEndDocument();
            writer.flush();
            setMessage(baos.toByteArray());
            
            if(log) {
                logger.log(Level.FINE, "\n\nWrapped fault = " + 
                        baos.toString() + "\n\n");
            }
        } catch(Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    
private voidwriteJBIFooter(javax.xml.stream.XMLStreamWriter writer)

        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
    
private voidwriteJBIHeader(javax.xml.stream.XMLStreamWriter writer)

        writer.writeStartDocument();
        writer.writeStartElement(WRAPPER_MESSAGE_QNAME);
        writeMessageAttributes(writer, wsdlMessageName);
    
private voidwriteMessageAttributes(javax.xml.stream.XMLStreamWriter sw, java.lang.String wsdlMessageName)

        String prefix = wsdlMessageType.getPrefix();
        if (prefix == null || prefix.trim().length() == 0) {
            prefix = "msgns";
        }
        sw.writeAttribute(WRAPPER_ATTRIBUTE_VERSION, WRAPPER_ATTRIBUTE_VERSION_VALUE);
        if(wsdlMessageName != null) {
            sw.writeAttribute(WRAPPER_ATTRIBUTE_NAME, wsdlMessageName);
        }
        sw.writeAttribute(WRAPPER_ATTRIBUTE_TYPE, prefix + ":" + wsdlMessageType.getLocalPart());
        sw.writeAttribute("xmlns:" + prefix, wsdlMessageType.getNamespaceURI());
        sw.writeAttribute("xmlns:" + WRAPPER_DEFAULT_NAMESPACE_PREFIX, WRAPPER_DEFAULT_NAMESPACE);
    
public voidwritePayloadTo(javax.xml.stream.XMLStreamWriter sw)

        try {
            sw.flush();
            abstractMessage.writePayloadTo(sw);
        } catch(Exception ex) {
            ex.printStackTrace();
            throw new XMLStreamException(ex);
        }
    
public voidwriteTo(org.xml.sax.ContentHandler contentHandler, org.xml.sax.ErrorHandler errorHandler)

        abstractMessage.writeTo(contentHandler, errorHandler);
    
public voidwriteTo(javax.xml.stream.XMLStreamWriter sw)
Writes the wrapped abstractMessage into the stream writer. The fault is not wrapped to compensate a bug in HTTP SOAP BC. It is assumed that the normal response abstractMessage has only one part.

        writePayloadTo(sw);