Search in sources :

Example 41 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class JAXBEncoderDecoderTest method testUnMarshall.

@Test
public void testUnMarshall() throws Exception {
    // Hello World Wsdl generated namespace
    QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
    MessagePartInfo part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    part.setTypeClass(Class.forName(wrapperAnnotation.className()));
    Document doc = DOMUtils.getEmptyDocument();
    Element elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
    Element rtEl = doc.createElementNS(elName.getNamespaceURI(), "requestType");
    elNode.appendChild(rtEl);
    rtEl.appendChild(doc.createTextNode("Hello Test"));
    Object obj = JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), elNode, part, true);
    assertNotNull(obj);
    // Add a Node and then test
    assertEquals(GreetMe.class, obj.getClass());
    assertEquals("Hello Test", ((GreetMe) obj).getRequestType());
    part.setTypeClass(String.class);
    Node n = null;
    try {
        JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), n, part, true);
        fail("Should have received a Fault");
    } catch (Fault pe) {
    // Expected Exception
    } catch (Exception ex) {
        fail("Should have received a Fault, not: " + ex);
    }
    // Now test schema validation during unmarshaling
    elName = new QName(wrapperAnnotation.targetNamespace(), "stringStruct");
    // Create an XML Tree of
    // <StringStruct><arg1>World</arg1></StringStruct>
    // elNode = soapElFactory.createElement(elName);
    // elNode.addNamespaceDeclaration("", elName.getNamespaceURI());
    part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    part.setTypeClass(Class.forName("org.apache.hello_world_soap_http.types.StringStruct"));
    doc = DOMUtils.getEmptyDocument();
    elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
    rtEl = doc.createElementNS(elName.getNamespaceURI(), "arg1");
    elNode.appendChild(rtEl);
    rtEl.appendChild(doc.createTextNode("World"));
    // Should unmarshal without problems when no schema used.
    obj = JAXBEncoderDecoder.unmarshall(context.createUnmarshaller(), elNode, part, true);
    assertNotNull(obj);
    assertEquals(StringStruct.class, obj.getClass());
    assertEquals("World", ((StringStruct) obj).getArg1());
    try {
        // unmarshal with schema should raise exception.
        Unmarshaller m = context.createUnmarshaller();
        m.setSchema(schema);
        JAXBEncoderDecoder.unmarshall(m, elNode, part, true);
        fail("Should have thrown a Fault");
    } catch (Fault ex) {
    // expected - schema validation should fail.
    }
}
Also used : QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) ObjectWithQualifiedElementElement(org.apache.cxf.jaxb_form.ObjectWithQualifiedElementElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Fault(org.apache.cxf.interceptor.Fault) Document(org.w3c.dom.Document) Unmarshaller(javax.xml.bind.Unmarshaller) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Example 42 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class JAXBAttachmentUnmarshaller method getAttachmentAsByteArray.

@Override
public byte[] getAttachmentAsByteArray(String contentId) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        InputStream is = AttachmentUtil.getAttachmentDataSource(contentId, attachments).getInputStream();
        IOUtils.copy(is, bos);
        is.close();
        bos.close();
    } catch (IOException e) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_READ_ERROR", LOG), e);
    }
    return bos.toByteArray();
}
Also used : InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 43 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class DataReaderImpl method createUnmarshaller.

private Unmarshaller createUnmarshaller() {
    try {
        Unmarshaller um = context.createUnmarshaller();
        if (databinding.getUnmarshallerListener() != null) {
            um.setListener(databinding.getUnmarshallerListener());
        }
        if (setEventHandler) {
            um.setEventHandler(new WSUIDValidationHandler(veventHandler));
        }
        if (databinding.getUnmarshallerProperties() != null) {
            for (Map.Entry<String, Object> propEntry : databinding.getUnmarshallerProperties().entrySet()) {
                try {
                    um.setProperty(propEntry.getKey(), propEntry.getValue());
                } catch (PropertyException pe) {
                    LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe);
                }
            }
        }
        um.setSchema(schema);
        um.setAttachmentUnmarshaller(getAttachmentUnmarshaller());
        for (XmlAdapter<?, ?> adapter : databinding.getConfiguredXmlAdapters()) {
            um.setAdapter(adapter);
        }
        return um;
    } catch (javax.xml.bind.UnmarshalException ex) {
        throw new Fault(new Message("UNMARSHAL_ERROR", LOG, ex.getLinkedException().getMessage()), ex);
    } catch (JAXBException ex) {
        throw new Fault(new Message("UNMARSHAL_ERROR", LOG, ex.getMessage()), ex);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) UnmarshalException(javax.xml.bind.UnmarshalException) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) Fault(org.apache.cxf.interceptor.Fault) Unmarshaller(javax.xml.bind.Unmarshaller) Map(java.util.Map)

Example 44 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class HolderOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    MessageContentsList outObjects = MessageContentsList.getContentsList(message);
    Exchange exchange = message.getExchange();
    OperationInfo op = exchange.getBindingOperationInfo() == null ? null : exchange.getBindingOperationInfo().getOperationInfo();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("op: " + op);
        if (null != op) {
            LOG.fine("op.hasOutput(): " + op.hasOutput());
            if (op.hasOutput()) {
                LOG.fine("op.getOutput().size(): " + op.getOutput().size());
            }
        }
    }
    if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
        LOG.fine("Returning.");
        return;
    }
    if (!Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
        List<MessagePartInfo> parts = op.getOutput().getMessageParts();
        MessageContentsList inObjects = MessageContentsList.getContentsList(exchange.getInMessage());
        if (inObjects != null) {
            if (inObjects != outObjects) {
                for (int x = 0; x < inObjects.size(); x++) {
                    Object o = inObjects.get(x);
                    if (o instanceof Holder) {
                        outObjects.set(x + 1, o);
                    }
                }
            } else {
                LOG.severe("CANNOT_SET_HOLDER_OBJECTS");
                throw new Fault(new org.apache.cxf.common.i18n.Message("CANNOT_SET_HOLDER_OBJECTS", LOG));
            }
        }
        for (MessagePartInfo part : parts) {
            if (part.getIndex() > 0 && part.getTypeClass() != null) {
                Holder<?> holder = (Holder<?>) outObjects.get(part);
                outObjects.put(part, holder.value);
            }
        }
    } else {
        List<Object> holders = new ArrayList<>(outObjects);
        for (int x = 0; x < outObjects.size(); x++) {
            Object o = outObjects.get(x);
            if (o instanceof Holder) {
                outObjects.set(x, ((Holder<?>) o).value);
            } else {
                holders.set(x, null);
            }
        }
        message.put(HolderInInterceptor.CLIENT_HOLDERS, holders);
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) Holder(javax.xml.ws.Holder) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Exchange(org.apache.cxf.message.Exchange)

Example 45 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class MessageModeOutInterceptor method validatePossibleFault.

private void validatePossibleFault(Message message, BindingOperationInfo bop, Node ds) {
    Element el = DOMUtils.getFirstElement(ds);
    if (!"Fault".equals(el.getLocalName())) {
        return;
    }
    message.put(Message.RESPONSE_CODE, 500);
    el = DOMUtils.getFirstElement(el);
    while (el != null && !"detail".equals(el.getLocalName())) {
        el = DOMUtils.getNextElement(el);
    }
    if (el != null) {
        Schema schema = EndpointReferenceUtils.getSchema(message.getExchange().getService().getServiceInfos().get(0), message.getExchange().getBus());
        try {
            validateFaultDetail(el, schema, bop);
        } catch (Exception e) {
            throw new Fault(e);
        }
        // We validated what we can from a fault standpoint
        message.put(Message.SCHEMA_VALIDATION_ENABLED, Boolean.FALSE);
    }
}
Also used : Element(org.w3c.dom.Element) Schema(javax.xml.validation.Schema) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) Fault(org.apache.cxf.interceptor.Fault) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Aggregations

Fault (org.apache.cxf.interceptor.Fault)283 IOException (java.io.IOException)74 QName (javax.xml.namespace.QName)56 Message (org.apache.cxf.message.Message)52 XMLStreamException (javax.xml.stream.XMLStreamException)50 Element (org.w3c.dom.Element)42 Message (org.apache.cxf.common.i18n.Message)34 Exchange (org.apache.cxf.message.Exchange)30 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)30 SOAPException (javax.xml.soap.SOAPException)28 InputStream (java.io.InputStream)27 ArrayList (java.util.ArrayList)27 XMLStreamReader (javax.xml.stream.XMLStreamReader)26 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)26 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)25 Test (org.junit.Test)24 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)23 List (java.util.List)21 SOAPMessage (javax.xml.soap.SOAPMessage)21 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)21