Search in sources :

Example 76 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class CorbaStreamFaultOutInterceptor method getDataWriter.

protected DataWriter<XMLStreamWriter> getDataWriter(CorbaMessage message) {
    Service serviceModel = ServiceModelUtil.getService(message.getExchange());
    DataWriter<XMLStreamWriter> dataWriter = serviceModel.getDataBinding().createWriter(XMLStreamWriter.class);
    if (dataWriter == null) {
        throw new CorbaBindingException("Couldn't create data writer for outgoing fault message");
    }
    return dataWriter;
}
Also used : CorbaBindingException(org.apache.cxf.binding.corba.CorbaBindingException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Service(org.apache.cxf.service.Service)

Example 77 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class MessageModeInInterceptor method doFromSoapMessage.

private void doFromSoapMessage(Message message, Object sm) {
    SOAPMessage m = (SOAPMessage) sm;
    MessageContentsList list = (MessageContentsList) message.getContent(List.class);
    if (list == null) {
        list = new MessageContentsList();
        message.setContent(List.class, list);
    }
    Object o = m;
    if (StreamSource.class.isAssignableFrom(type)) {
        try {
            try (CachedOutputStream out = new CachedOutputStream()) {
                XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(out);
                StaxUtils.copy(new DOMSource(m.getSOAPPart()), xsw);
                xsw.close();
                o = new StreamSource(out.getInputStream());
            }
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (SAXSource.class.isAssignableFrom(type)) {
        o = new StaxSource(new W3CDOMStreamReader(m.getSOAPPart()));
    } else if (Source.class.isAssignableFrom(type)) {
        o = new DOMSource(m.getSOAPPart());
    }
    list.set(0, o);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) SAXSource(javax.xml.transform.sax.SAXSource) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StaxSource(org.apache.cxf.staxutils.StaxSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List)

Example 78 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class WebFaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Fault f = (Fault) message.getContent(Exception.class);
    if (f == null) {
        return;
    }
    try {
        Throwable thr = f.getCause();
        SOAPFaultException sf = null;
        if (thr instanceof SOAPFaultException) {
            sf = (SOAPFaultException) thr;
        } else if (thr.getCause() instanceof SOAPFaultException) {
            sf = (SOAPFaultException) thr.getCause();
        }
        if (sf != null) {
            SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
            if (soapVersion != null && soapVersion.getVersion() != 1.1) {
                if (f instanceof SoapFault) {
                    for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
                        ((SoapFault) f).addSubCode(it.next());
                    }
                }
                if (sf.getFault().getFaultReasonLocales().hasNext()) {
                    Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
                    String convertedLang = lang.getLanguage();
                    String country = lang.getCountry();
                    if (country.length() > 0) {
                        convertedLang = convertedLang + '-' + country;
                    }
                    f.setLang(convertedLang);
                }
            }
            message.setContent(Exception.class, f);
        }
    } catch (Exception e) {
    // do nothing;
    }
    Throwable cause = f.getCause();
    WebFault fault = null;
    if (cause != null) {
        fault = getWebFaultAnnotation(cause.getClass());
        if (fault == null && cause.getCause() != null) {
            fault = getWebFaultAnnotation(cause.getCause().getClass());
            if (fault != null || cause instanceof RuntimeException) {
                cause = cause.getCause();
            }
        }
    }
    if (cause instanceof Exception && fault != null) {
        Exception ex = (Exception) cause;
        Object faultInfo = null;
        try {
            Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
            faultInfo = method.invoke(cause, new Object[0]);
        } catch (NoSuchMethodException e) {
            faultInfo = createFaultInfoBean(fault, cause);
        } catch (InvocationTargetException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
        } catch (IllegalAccessException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
        } catch (IllegalArgumentException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
        }
        Service service = message.getExchange().getService();
        try {
            DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
                Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
                writer.setSchema(schema);
            }
            OperationInfo op = message.getExchange().getBindingOperationInfo().getOperationInfo();
            QName faultName = getFaultName(fault, cause.getClass(), op);
            MessagePartInfo part = getFaultMessagePart(faultName, op);
            if (f.hasDetails()) {
                writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
            } else {
                writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
                if (!f.getDetail().hasChildNodes()) {
                    f.setDetail(null);
                }
            }
            f.setMessage(ex.getMessage());
        } catch (Exception nex) {
            if (nex instanceof Fault) {
                message.setContent(Exception.class, nex);
                super.handleMessage(message);
            } else {
                // if exception occurs while writing a fault, we'll just let things continue
                // and let the rest of the chain try handling it as is.
                LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
            }
        }
    } else {
        FaultMode mode = message.get(FaultMode.class);
        if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
            // only convert checked exceptions with this
            // otherwise delegate down to the normal protocol specific stuff
            super.handleMessage(message);
        }
    }
}
Also used : Locale(java.util.Locale) OperationInfo(org.apache.cxf.service.model.OperationInfo) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) Schema(javax.xml.validation.Schema) Fault(org.apache.cxf.interceptor.Fault) WebFault(javax.xml.ws.WebFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) WebFault(javax.xml.ws.WebFault) FaultMode(org.apache.cxf.message.FaultMode) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion)

Example 79 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class LogicalHandlerOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    HandlerChainInvoker invoker = getInvoker(message);
    if (invoker.getLogicalHandlers().isEmpty()) {
        return;
    }
    XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
    Node nd = message.getContent(Node.class);
    SOAPMessage m = message.getContent(SOAPMessage.class);
    Document document = null;
    if (m != null) {
        document = m.getSOAPPart();
    } else if (nd != null) {
        document = nd.getOwnerDocument();
    } else {
        document = DOMUtils.newDocument();
        message.setContent(Node.class, document);
    }
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter(document.createDocumentFragment());
    // Replace stax writer with DomStreamWriter
    message.setContent(XMLStreamWriter.class, writer);
    message.put(ORIGINAL_WRITER, origWriter);
    message.getInterceptorChain().add(ending);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 80 with XMLStreamWriter

use of javax.xml.stream.XMLStreamWriter in project cxf by apache.

the class LogicalMessageImpl method write.

private void write(Source source, Node n) {
    try {
        if (source instanceof DOMSource && ((DOMSource) source).getNode() == null) {
            return;
        }
        XMLStreamWriter writer = new W3CDOMStreamWriter((Element) n);
        StaxUtils.copy(source, writer);
    } catch (XMLStreamException e) {
        throw new Fault(e);
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Fault(org.apache.cxf.interceptor.Fault)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)209 XMLStreamException (javax.xml.stream.XMLStreamException)84 StringWriter (java.io.StringWriter)47 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)32 QName (javax.xml.namespace.QName)26 Document (org.w3c.dom.Document)26 Fault (org.apache.cxf.interceptor.Fault)25 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StreamSource (javax.xml.transform.stream.StreamSource)17 StringReader (java.io.StringReader)16 JAXBException (javax.xml.bind.JAXBException)14 DOMSource (javax.xml.transform.dom.DOMSource)14 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)14 Message (org.apache.cxf.message.Message)13 Element (org.w3c.dom.Element)11