Search in sources :

Example 61 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class MessageModeOutInterceptor method doSoap.

private void doSoap(Message message) {
    MessageContentsList list = (MessageContentsList) message.getContent(List.class);
    if (list == null || list.isEmpty()) {
        return;
    }
    Object o = list.get(0);
    if (o instanceof SOAPMessage) {
        SOAPMessage soapMessage = (SOAPMessage) o;
        if (soapMessage.countAttachments() > 0) {
            message.put("write.attachments", Boolean.TRUE);
        }
        try {
            if (message instanceof org.apache.cxf.binding.soap.SoapMessage) {
                org.apache.cxf.binding.soap.SoapMessage cxfSoapMessage = (org.apache.cxf.binding.soap.SoapMessage) message;
                String cxfNamespace = cxfSoapMessage.getVersion().getNamespace();
                SOAPHeader soapHeader = soapMessage.getSOAPHeader();
                String namespace = soapHeader == null ? null : soapHeader.getNamespaceURI();
                if (Soap12.SOAP_NAMESPACE.equals(namespace) && !namespace.equals(cxfNamespace)) {
                    cxfSoapMessage.setVersion(Soap12.getInstance());
                    cxfSoapMessage.put(Message.CONTENT_TYPE, cxfSoapMessage.getVersion().getContentType());
                }
            }
        } catch (SOAPException e) {
        // ignore
        }
        try {
            Object enc = soapMessage.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
            if (enc instanceof String) {
                message.put(Message.ENCODING, enc);
            }
        } catch (SOAPException e) {
        // ignore
        }
        try {
            Object xmlDec = soapMessage.getProperty(SOAPMessage.WRITE_XML_DECLARATION);
            if (xmlDec != null) {
                boolean b = PropertyUtils.isTrue(xmlDec);
                message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, b);
            }
        } catch (SOAPException e) {
        // ignore
        }
    }
    message.getInterceptorChain().add(internal);
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPException(javax.xml.soap.SOAPException) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 62 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class MessageModeOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop != null && !bindingName.equals(bop.getBinding().getName())) {
        return;
    }
    if (saajOut != null) {
        doSoap(message);
    } else if (DataSource.class.isAssignableFrom(type)) {
        // datasource stuff, must check if multi-source
        MessageContentsList list = (MessageContentsList) message.getContent(List.class);
        DataSource ds = (DataSource) list.get(0);
        String ct = ds.getContentType();
        if (ct.toLowerCase().contains("multipart/related")) {
            Message msg = new MessageImpl();
            msg.setExchange(message.getExchange());
            msg.put(Message.CONTENT_TYPE, ct);
            try {
                msg.setContent(InputStream.class, ds.getInputStream());
                AttachmentDeserializer deser = new AttachmentDeserializer(msg);
                deser.initializeAttachments();
            } catch (IOException ex) {
                throw new Fault(ex);
            }
            message.setAttachments(msg.getAttachments());
            final InputStream in = msg.getContent(InputStream.class);
            final String ct2 = (String) msg.get(Message.CONTENT_TYPE);
            list.set(0, new DataSource() {

                public String getContentType() {
                    return ct2;
                }

                public InputStream getInputStream() throws IOException {
                    return in;
                }

                public String getName() {
                    return ct2;
                }

                public OutputStream getOutputStream() throws IOException {
                    return null;
                }
            });
        } else if (!ct.toLowerCase().contains("xml")) {
            // not XML based, need to stream out directly.  This is a bit tricky as
            // we don't want the stax stuff triggering and such
            OutputStream out = message.getContent(OutputStream.class);
            message.put(Message.CONTENT_TYPE, ct);
            try {
                InputStream in = ds.getInputStream();
                IOUtils.copy(in, out);
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                throw new Fault(e);
            }
            list.remove(0);
            out = new CachedOutputStream();
            message.setContent(OutputStream.class, out);
            XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
            message.setContent(XMLStreamWriter.class, writer);
        }
    } else if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && Source.class.isAssignableFrom(type)) {
        // if schema validation is on, we'll end up converting to a DOMSource anyway,
        // let's convert and check for a fault
        MessageContentsList list = (MessageContentsList) message.getContent(List.class);
        Source ds = (Source) list.get(0);
        if (!(ds instanceof DOMSource)) {
            try {
                ds = new DOMSource(StaxUtils.read(ds));
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
            list.set(0, ds);
            validatePossibleFault(message, bop, ((DOMSource) ds).getNode());
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) SOAPMessage(javax.xml.soap.SOAPMessage) AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) DataSource(javax.activation.DataSource) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 63 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class JAXWSMethodInvokerTest method testFaultAvoidHeadersCopy.

@Test
public void testFaultAvoidHeadersCopy() throws Throwable {
    ExceptionService serviceObject = new ExceptionService();
    Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[] {});
    Exchange ex = new ExchangeImpl();
    prepareInMessage(ex, false);
    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    try {
        jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
        fail("Expected fault");
    } catch (Fault fault) {
        Message outMsg = ex.getOutMessage();
        assertNull(outMsg);
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) MessageContentsList(org.apache.cxf.message.MessageContentsList) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 64 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class HolderInInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    MessageContentsList inObjects = MessageContentsList.getContentsList(message);
    Exchange exchange = message.getExchange();
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    if (bop == null) {
        return;
    }
    OperationInfo op = bop.getOperationInfo();
    if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
        return;
    }
    List<MessagePartInfo> parts = op.getOutput().getMessageParts();
    boolean client = Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE));
    if (client) {
        List<Holder<?>> outHolders = CastUtils.cast((List<?>) message.getExchange().getOutMessage().get(CLIENT_HOLDERS));
        for (MessagePartInfo part : parts) {
            if (part.getIndex() != 0 && part.getTypeClass() != null) {
                @SuppressWarnings("unchecked") Holder<Object> holder = (Holder<Object>) outHolders.get(part.getIndex() - 1);
                if (holder != null) {
                    holder.value = inObjects.get(part);
                    inObjects.put(part, holder);
                }
            }
        }
    } else {
        for (MessagePartInfo part : parts) {
            int idx = part.getIndex() - 1;
            if (idx >= 0 && part.getTypeClass() != null) {
                if (inObjects == null) {
                    // if soap:body is empty, the contents may not exist
                    // so we need to create a contents list to store
                    // the holders for the outgoing parts (CXF-4031)
                    inObjects = new MessageContentsList();
                    message.setContent(List.class, inObjects);
                }
                if (idx >= inObjects.size()) {
                    inObjects.set(idx, new Holder<Object>());
                } else {
                    Object o = inObjects.get(idx);
                    inObjects.set(idx, new Holder<Object>(o));
                }
            }
        }
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) Holder(javax.xml.ws.Holder) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Exchange(org.apache.cxf.message.Exchange)

Example 65 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList in project cxf by apache.

the class RMCaptureOutInterceptor method setTerminateSequence.

private void setTerminateSequence(Message msg, Identifier identifier, ProtocolVariation protocol) throws RMException {
    TerminateSequenceType ts = new TerminateSequenceType();
    ts.setIdentifier(identifier);
    MessageContentsList contents = new MessageContentsList(new Object[] { protocol.getCodec().convertToSend(ts) });
    msg.setContent(List.class, contents);
    // create a new exchange for this output-only exchange
    Exchange newex = new ExchangeImpl();
    Exchange oldex = msg.getExchange();
    newex.put(Bus.class, oldex.getBus());
    newex.put(Endpoint.class, oldex.getEndpoint());
    newex.put(Service.class, oldex.getEndpoint().getService());
    newex.put(Binding.class, oldex.getEndpoint().getBinding());
    newex.setConduit(oldex.getConduit(msg));
    newex.setDestination(oldex.getDestination());
    // Setup the BindingOperationInfo
    RMEndpoint rmep = getManager().getReliableEndpoint(msg);
    OperationInfo oi = rmep.getEndpoint(protocol).getEndpointInfo().getService().getInterface().getOperation(protocol.getConstants().getTerminateSequenceAnonymousOperationName());
    BindingInfo bi = rmep.getBindingInfo(protocol);
    BindingOperationInfo boi = bi.getOperation(oi);
    newex.put(BindingInfo.class, bi);
    newex.put(BindingOperationInfo.class, boi);
    msg.setExchange(newex);
    newex.setOutMessage(msg);
}
Also used : Exchange(org.apache.cxf.message.Exchange) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) BindingInfo(org.apache.cxf.service.model.BindingInfo) TerminateSequenceType(org.apache.cxf.ws.rm.v200702.TerminateSequenceType) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Aggregations

MessageContentsList (org.apache.cxf.message.MessageContentsList)69 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)24 Exchange (org.apache.cxf.message.Exchange)23 Message (org.apache.cxf.message.Message)22 Fault (org.apache.cxf.interceptor.Fault)19 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)18 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Endpoint (org.apache.cxf.endpoint.Endpoint)11 MessageImpl (org.apache.cxf.message.MessageImpl)11 Response (javax.ws.rs.core.Response)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 Method (java.lang.reflect.Method)9 OperationInfo (org.apache.cxf.service.model.OperationInfo)9 XMLStreamException (javax.xml.stream.XMLStreamException)8 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)8 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)8 MessageInfo (org.apache.cxf.service.model.MessageInfo)8 Test (org.junit.Test)8 Service (org.apache.cxf.service.Service)7