Search in sources :

Example 6 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class LogicalMessageImpl method setPayload.

public void setPayload(Source s) {
    Message message = msgContext.getWrappedMessage();
    Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
    SOAPMessage m = message.getContent(SOAPMessage.class);
    if (m != null && !MessageUtils.isOutbound(message)) {
        try {
            SAAJUtils.getBody(m).removeContents();
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
            StaxUtils.copy(s, writer);
            writer.flush();
            writer.close();
            if (mode == Service.Mode.MESSAGE) {
                s = new DOMSource(m.getSOAPPart());
            } else {
                s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
            }
            W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
            message.setContent(XMLStreamReader.class, r);
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (mode != null) {
        if (message instanceof SoapMessage) {
            if (mode == Service.Mode.MESSAGE) {
                try {
                    // REVISIT: should try to use the original SOAPMessage
                    // instead of creating a new empty one.
                    SOAPMessage msg = initSOAPMessage(null);
                    write(s, SAAJUtils.getBody(msg));
                    s = new DOMSource(msg.getSOAPPart());
                } catch (Exception e) {
                    throw new Fault(e);
                }
            }
        } else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    } else {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
        msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
    }
    msgContext.getWrappedMessage().setContent(Source.class, s);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) Service(javax.xml.ws.Service) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 7 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class ClientFaultConverter method processFaultDetail.

protected void processFaultDetail(Fault fault, Message msg) {
    Element exDetail = (Element) DOMUtils.getChild(fault.getDetail(), Node.ELEMENT_NODE);
    if (exDetail == null) {
        return;
    }
    QName qname = new QName(exDetail.getNamespaceURI(), exDetail.getLocalName());
    FaultInfo faultWanted = null;
    MessagePartInfo part = null;
    BindingOperationInfo boi = msg.getExchange().getBindingOperationInfo();
    if (boi == null) {
        return;
    }
    if (boi.isUnwrapped()) {
        boi = boi.getWrappedOperation();
    }
    for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
        for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
            if (qname.equals(mpi.getConcreteName())) {
                faultWanted = faultInfo;
                part = mpi;
                break;
            }
        }
        if (faultWanted != null) {
            break;
        }
    }
    if (faultWanted == null) {
        // did not find it using the proper qualified names, we'll try again with just the localpart
        for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
            for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
                if (qname.getLocalPart().equals(mpi.getConcreteName().getLocalPart())) {
                    faultWanted = faultInfo;
                    part = mpi;
                    break;
                }
            }
            if (faultWanted != null) {
                break;
            }
        }
    }
    if (faultWanted == null) {
        return;
    }
    Service s = msg.getExchange().getService();
    DataBinding dataBinding = s.getDataBinding();
    Object e = null;
    if (isDOMSupported(dataBinding)) {
        DataReader<Node> reader = this.getNodeDataReader(msg);
        reader.setProperty(DataReader.FAULT, fault);
        e = reader.read(part, exDetail);
    } else {
        DataReader<XMLStreamReader> reader = this.getDataReader(msg);
        XMLStreamReader xsr = new W3CDOMStreamReader(exDetail);
        try {
            xsr.nextTag();
        } catch (XMLStreamException e1) {
            throw new Fault(e1);
        }
        reader.setProperty(DataReader.FAULT, fault);
        e = reader.read(part, xsr);
    }
    if (!(e instanceof Exception)) {
        try {
            Class<?> exClass = faultWanted.getProperty(Class.class.getName(), Class.class);
            if (exClass == null) {
                return;
            }
            if (e == null) {
                Constructor<?> constructor = exClass.getConstructor(String.class);
                e = constructor.newInstance(fault.getMessage());
            } else {
                try {
                    Constructor<?> constructor = getConstructor(exClass, e);
                    e = constructor.newInstance(fault.getMessage(), e);
                } catch (NoSuchMethodException e1) {
                    // Use reflection to convert fault bean to exception
                    e = convertFaultBean(exClass, e, fault);
                }
            }
            msg.setContent(Exception.class, e);
        } catch (Exception e1) {
            LogUtils.log(LOG, Level.INFO, "EXCEPTION_WHILE_CREATING_EXCEPTION", e1, e1.getMessage());
        }
    } else {
        if (fault.getMessage() != null) {
            Field f;
            try {
                f = Throwable.class.getDeclaredField("detailMessage");
                ReflectionUtil.setAccessible(f);
                f.set(e, fault.getMessage());
            } catch (Exception e1) {
            // ignore
            }
        }
        msg.setContent(Exception.class, e);
    }
}
Also used : FaultInfo(org.apache.cxf.service.model.FaultInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Service(org.apache.cxf.service.Service) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) XMLStreamException(javax.xml.stream.XMLStreamException) Field(java.lang.reflect.Field) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) DataBinding(org.apache.cxf.databinding.DataBinding)

Example 8 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class ProviderImpl method convertToInternal.

/**
 * Convert from EndpointReference to CXF internal 2005/08 EndpointReferenceType
 *
 * @param external the javax.xml.ws.EndpointReference
 * @return CXF internal 2005/08 EndpointReferenceType
 */
public static EndpointReferenceType convertToInternal(EndpointReference external) {
    if (external instanceof W3CEndpointReference) {
        Unmarshaller um = null;
        try {
            DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
            DOMResult result = new DOMResult(frag);
            external.writeTo(result);
            W3CDOMStreamReader reader = new W3CDOMStreamReader(frag);
            // CXF internal 2005/08 EndpointReferenceType should be
            // compatible with W3CEndpointReference
            // jaxContext = ContextUtils.getJAXBContext();
            JAXBContext context = JAXBContext.newInstance(new Class[] { org.apache.cxf.ws.addressing.ObjectFactory.class });
            um = context.createUnmarshaller();
            return um.unmarshal(reader, EndpointReferenceType.class).getValue();
        } catch (JAXBException e) {
            throw new IllegalArgumentException("Could not unmarshal EndpointReference", e);
        } finally {
            JAXBUtils.closeUnmarshaller(um);
        }
    }
    return null;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 9 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class AegisElementDataReader method read.

/**
 * Convert a DOM element to a type.
 * @param input
 * @return
 */
public Object read(Element input) throws Exception {
    W3CDOMStreamReader sreader = new W3CDOMStreamReader(input);
    // advance into the first tag
    sreader.nextTag();
    return reader.read(sreader);
}
Also used : W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 10 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class Soap12FaultInInterceptor method unmarshalFault.

public static SoapFault unmarshalFault(SoapMessage message, XMLStreamReader reader) {
    String exMessage = null;
    QName faultCode = null;
    List<QName> subCodes = null;
    String role = null;
    String node = null;
    Element detail = null;
    String lang = null;
    Map<String, String> ns = new HashMap<>();
    ns.put("s", Soap12.SOAP_NAMESPACE);
    XPathUtils xu = new XPathUtils(ns);
    try {
        Node mainNode = message.getContent(Node.class);
        Node fault = null;
        if (reader instanceof W3CDOMStreamReader) {
            W3CDOMStreamReader dr = (W3CDOMStreamReader) reader;
            fault = dr.getCurrentElement();
            dr.consumeFrame();
        } else if (mainNode != null) {
            Node bodyNode = (Node) xu.getValue("//s:Body", mainNode, XPathConstants.NODE);
            StaxUtils.readDocElements(bodyNode.getOwnerDocument(), bodyNode, new FragmentStreamReader(reader), false, false);
            fault = (Element) xu.getValue("//s:Fault", bodyNode, XPathConstants.NODE);
        } else {
            fault = StaxUtils.read(new FragmentStreamReader(reader));
        }
        fault = DOMUtils.getDomElement(fault);
        Element el = (Element) xu.getValue("//s:Fault/s:Code/s:Value", fault, XPathConstants.NODE);
        if (el != null) {
            faultCode = DOMUtils.createQName(el.getTextContent(), el);
        }
        el = (Element) xu.getValue("//s:Fault/s:Code/s:Subcode", fault, XPathConstants.NODE);
        if (el != null) {
            subCodes = new LinkedList<QName>();
            NodeList vlist = el.getElementsByTagNameNS(Soap12.SOAP_NAMESPACE, "Value");
            for (int i = 0; i < vlist.getLength(); i++) {
                Node v = vlist.item(i);
                subCodes.add(DOMUtils.createQName(v.getTextContent(), v));
            }
        }
        exMessage = (String) xu.getValue("//s:Fault/s:Reason/s:Text/text()", fault, XPathConstants.STRING);
        lang = (String) xu.getValue("//s:Fault/s:Reason/s:Text/@xml:lang", fault, XPathConstants.STRING);
        Node detailNode = (Node) xu.getValue("//s:Fault/s:Detail", fault, XPathConstants.NODE);
        if (detailNode != null) {
            detail = (Element) detailNode;
        }
        role = (String) xu.getValue("//s:Fault/s:Role/text()", fault, XPathConstants.STRING);
        node = (String) xu.getValue("//s:Fault/s:Node/text()", fault, XPathConstants.STRING);
    } catch (XMLStreamException e) {
        throw new SoapFault("Could not parse message.", e, message.getVersion().getSender());
    }
    // if the fault's content is invalid and fautlCode is not found, blame the receiver
    if (faultCode == null) {
        faultCode = Soap12.getInstance().getReceiver();
        exMessage = new Message("INVALID_FAULT", LOG).toString();
    }
    SoapFault fault = new SoapFault(exMessage, faultCode);
    fault.setSubCodes(subCodes);
    fault.setDetail(detail);
    fault.setRole(role);
    fault.setNode(node);
    fault.setLang(lang);
    return fault;
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) XPathUtils(org.apache.cxf.helpers.XPathUtils) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader)

Aggregations

W3CDOMStreamReader (org.apache.cxf.staxutils.W3CDOMStreamReader)13 Element (org.w3c.dom.Element)6 XMLStreamException (javax.xml.stream.XMLStreamException)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 Document (org.w3c.dom.Document)5 Fault (org.apache.cxf.interceptor.Fault)4 InputStreamReader (java.io.InputStreamReader)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)3 Message (org.apache.cxf.message.Message)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 JAXBException (javax.xml.bind.JAXBException)2 QName (javax.xml.namespace.QName)2 SOAPMessage (javax.xml.soap.SOAPMessage)2 MessageContentsList (org.apache.cxf.message.MessageContentsList)2 Service (org.apache.cxf.service.Service)2 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)2 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)2 Node (org.w3c.dom.Node)2