Search in sources :

Example 41 with MessageContentsList

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

the class SimpleCxfRsBinding method buildResponse.

/**
     * Builds the response for the client.
     * <p />
     * Always returns a JAX-RS {@link Response} object, which gives the user a better control on the response behaviour.
     * If the message body is already an instance of {@link Response}, we reuse it and just inject the relevant HTTP headers.
     * @param camelExchange
     * @param base
     * @return
     */
protected Object buildResponse(org.apache.camel.Exchange camelExchange, Object base) {
    Message m = camelExchange.hasOut() ? camelExchange.getOut() : camelExchange.getIn();
    ResponseBuilder response;
    // if the body is different to Response, it's an entity; therefore, check 
    if (base instanceof Response) {
        response = Response.fromResponse((Response) base);
    } else {
        int status = m.getHeader(org.apache.camel.Exchange.HTTP_RESPONSE_CODE, Status.OK.getStatusCode(), Integer.class);
        response = Response.status(status);
        // avoid using the request MessageContentsList as the entity; it simply doesn't make sense
        if (base != null && !(base instanceof MessageContentsList)) {
            response.entity(base);
        }
    }
    // Compute which headers to transfer by applying the HeaderFilterStrategy, and transfer them to the JAX-RS Response
    Map<String, String> headersToPropagate = filterCamelHeadersForResponseHeaders(m.getHeaders(), camelExchange);
    for (Entry<String, String> entry : headersToPropagate.entrySet()) {
        response.header(entry.getKey(), entry.getValue());
    }
    return response.build();
}
Also used : Response(javax.ws.rs.core.Response) Message(org.apache.camel.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 42 with MessageContentsList

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

the class DefaultCxfBinding method populateCxfResponseFromExchange.

/**
     * This method is called by {@link CxfConsumer} to populate a CXF response exchange 
     * from a Camel exchange.
     */
public void populateCxfResponseFromExchange(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange) {
    if (cxfExchange.isOneWay()) {
        return;
    }
    // create response context
    Map<String, Object> responseContext = new HashMap<String, Object>();
    org.apache.camel.Message response;
    if (camelExchange.getPattern().isOutCapable()) {
        if (camelExchange.hasOut()) {
            response = camelExchange.getOut();
            LOG.trace("Get the response from the out message");
        } else {
            // Take the in message as a fall back
            response = camelExchange.getIn();
            LOG.trace("Get the response from the in message as a fallback");
        }
    } else {
        response = camelExchange.getIn();
        LOG.trace("Get the response from the in message");
    }
    // propagate response context
    Map<String, Object> camelHeaders = response.getHeaders();
    extractInvocationContextFromCamel(camelExchange, camelHeaders, responseContext, Client.RESPONSE_CONTEXT);
    propagateHeadersFromCamelToCxf(camelExchange, camelHeaders, cxfExchange, responseContext);
    // create out message
    Endpoint ep = cxfExchange.get(Endpoint.class);
    Message outMessage = ep.getBinding().createMessage();
    cxfExchange.setOutMessage(outMessage);
    DataFormat dataFormat = camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class);
    // make sure the "requestor role" property does not get propagated as we do switch role
    responseContext.remove(Message.REQUESTOR_ROLE);
    outMessage.putAll(responseContext);
    // Do we still need to put the response context back like this
    outMessage.put(Client.RESPONSE_CONTEXT, responseContext);
    LOG.trace("Set out response context = {}", responseContext);
    // set body
    Object outBody = DefaultCxfBinding.getBodyFromCamel(response, dataFormat);
    if (outBody != null) {
        if (dataFormat == DataFormat.PAYLOAD) {
            CxfPayload<?> payload = (CxfPayload<?>) outBody;
            outMessage.setContent(List.class, getResponsePayloadList(cxfExchange, payload.getBodySources()));
            outMessage.put(Header.HEADER_LIST, payload.getHeaders());
        } else {
            if (responseContext.get(Header.HEADER_LIST) != null) {
                outMessage.put(Header.HEADER_LIST, responseContext.get(Header.HEADER_LIST));
            }
            MessageContentsList resList = null;
            // Create a new MessageContentsList to avoid OOM from the HolderOutInterceptor
            if (outBody instanceof List) {
                resList = new MessageContentsList((List<?>) outBody);
            } else if (outBody.getClass().isArray()) {
                resList = new MessageContentsList((Object[]) outBody);
            } else {
                resList = new MessageContentsList(outBody);
            }
            if (resList != null) {
                outMessage.setContent(List.class, resList);
                LOG.trace("Set Out CXF message content = {}", resList);
            }
        }
    } else if (!cxfExchange.isOneWay() && cxfExchange.getInMessage() != null && MessageUtils.isTrue(cxfExchange.getInMessage().getContextualProperty("jaxws.provider.interpretNullAsOneway"))) {
        // treat this non-oneway call as oneway when the provider returns a null
        changeToOneway(cxfExchange);
        return;
    }
    // propagate attachments
    Set<Attachment> attachments = null;
    boolean isXop = Boolean.valueOf(camelExchange.getProperty(Message.MTOM_ENABLED, String.class));
    for (Map.Entry<String, org.apache.camel.Attachment> entry : camelExchange.getOut().getAttachmentObjects().entrySet()) {
        if (attachments == null) {
            attachments = new HashSet<Attachment>();
        }
        AttachmentImpl attachment = new AttachmentImpl(entry.getKey());
        org.apache.camel.Attachment camelAttachment = entry.getValue();
        attachment.setDataHandler(camelAttachment.getDataHandler());
        for (String name : camelAttachment.getHeaderNames()) {
            attachment.setHeader(name, camelAttachment.getHeader(name));
        }
        attachment.setXOP(isXop);
        attachments.add(attachment);
    }
    if (attachments != null) {
        outMessage.setAttachments(attachments);
    }
    BindingOperationInfo boi = cxfExchange.get(BindingOperationInfo.class);
    if (boi != null) {
        cxfExchange.put(BindingMessageInfo.class, boi.getOutput());
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.cxf.message.Attachment) Endpoint(org.apache.cxf.endpoint.Endpoint) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ArrayList(java.util.ArrayList) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) TreeMap(java.util.TreeMap)

Example 43 with MessageContentsList

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

the class DefaultCxfBinding method getResponsePayloadList.

// Non public methods
// -------------------------------------------------------------------------
protected MessageContentsList getResponsePayloadList(org.apache.cxf.message.Exchange exchange, List<Source> elements) {
    BindingOperationInfo boi = exchange.getBindingOperationInfo();
    if (boi.isUnwrapped()) {
        boi = boi.getWrappedOperation();
        exchange.put(BindingOperationInfo.class, boi);
    }
    MessageContentsList answer = new MessageContentsList();
    int i = 0;
    if (boi.getOutput() != null) {
        for (MessagePartInfo partInfo : boi.getOutput().getMessageParts()) {
            if (elements != null && elements.size() > i) {
                answer.put(partInfo, elements.get(i++));
            }
        }
    }
    return answer;
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 44 with MessageContentsList

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

the class BodyWriter method handleMessage.

public void handleMessage(Message outMessage) throws Fault {
    MessageContentsList objs = MessageContentsList.getContentsList(outMessage);
    if (objs == null || objs.size() == 0) {
        return;
    }
    OutputStream os = outMessage.getContent(OutputStream.class);
    if (os == null) {
        XMLStreamWriter writer = outMessage.getContent(XMLStreamWriter.class);
        if (writer == null) {
            return;
        }
    }
    Object body = objs.get(0);
    Annotation[] customAnns = (Annotation[]) outMessage.get(Annotation.class.getName());
    Type t = outMessage.get(Type.class);
    doWriteBody(outMessage, body, t, customAnns, os);
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) MessageContentsList(org.apache.cxf.message.MessageContentsList) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) Annotation(java.lang.annotation.Annotation)

Example 45 with MessageContentsList

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

the class DocLiteralInInterceptorTest method testUnmarshalSourceDataWrapped.

@Test
public void testUnmarshalSourceDataWrapped() throws Exception {
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(getClass().getResourceAsStream("resources/docLitWrappedReq.xml"));
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
    XMLStreamReader filteredReader = new PartialXMLStreamReader(reader, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));
    // advance the xml reader to the message parts
    StaxUtils.read(filteredReader);
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
    Message m = new MessageImpl();
    // request to keep the document as wrapped
    m.put(DocLiteralInInterceptor.KEEP_PARAMETERS_WRAPPER, true);
    Exchange exchange = new ExchangeImpl();
    Service service = control.createMock(Service.class);
    exchange.put(Service.class, service);
    EasyMock.expect(service.getDataBinding()).andReturn(new SourceDataBinding()).anyTimes();
    EasyMock.expect(service.size()).andReturn(0).anyTimes();
    EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();
    Endpoint endpoint = control.createMock(Endpoint.class);
    exchange.put(Endpoint.class, endpoint);
    // wrapped
    OperationInfo operationInfo = new OperationInfo();
    MessageInfo messageInfo = new MessageInfo(operationInfo, Type.INPUT, new QName(NS, "foo"));
    messageInfo.addMessagePart(new MessagePartInfo(new QName(NS, "personId"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName(NS, "ssn"), null));
    messageInfo.getMessagePart(0).setConcreteName(new QName(NS, "personId"));
    messageInfo.getMessagePart(1).setConcreteName(new QName(NS, "ssn"));
    operationInfo.setInput("inputName", messageInfo);
    // wrapper
    OperationInfo operationInfoWrapper = new OperationInfo();
    MessageInfo messageInfoWrapper = new MessageInfo(operationInfo, Type.INPUT, new QName(NS, "foo"));
    messageInfoWrapper.addMessagePart(new MessagePartInfo(new QName(NS, "GetPerson"), null));
    messageInfoWrapper.getMessagePart(0).setConcreteName(new QName(NS, "GetPerson"));
    operationInfoWrapper.setInput("inputName", messageInfoWrapper);
    operationInfoWrapper.setUnwrappedOperation(operationInfo);
    ServiceInfo serviceInfo = control.createMock(ServiceInfo.class);
    EasyMock.expect(serviceInfo.getName()).andReturn(new QName("http://foo.com", "service")).anyTimes();
    InterfaceInfo interfaceInfo = control.createMock(InterfaceInfo.class);
    EasyMock.expect(serviceInfo.getInterface()).andReturn(interfaceInfo).anyTimes();
    EasyMock.expect(interfaceInfo.getName()).andReturn(new QName("http://foo.com", "interface")).anyTimes();
    BindingInfo bindingInfo = new BindingInfo(serviceInfo, "");
    BindingOperationInfo boi = new BindingOperationInfo(bindingInfo, operationInfoWrapper);
    exchange.put(BindingOperationInfo.class, boi);
    EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
    BindingInfo binding = control.createMock(BindingInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
    EasyMock.expect(endpointInfo.getBinding()).andReturn(binding).anyTimes();
    EasyMock.expect(binding.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endpointInfo.getService()).andReturn(serviceInfo).anyTimes();
    EasyMock.expect(endpointInfo.getName()).andReturn(new QName("http://foo.com", "endpoint")).anyTimes();
    EasyMock.expect(endpointInfo.getProperty("URI", URI.class)).andReturn(new URI("dummy")).anyTimes();
    List<OperationInfo> operations = new ArrayList<>();
    EasyMock.expect(interfaceInfo.getOperations()).andReturn(operations).anyTimes();
    m.setExchange(exchange);
    m.put(Message.SCHEMA_VALIDATION_ENABLED, false);
    m.setContent(XMLStreamReader.class, reader);
    control.replay();
    new DocLiteralInInterceptor().handleMessage(m);
    MessageContentsList params = (MessageContentsList) m.getContent(List.class);
    // we expect a wrapped document
    assertEquals(1, params.size());
    Map<String, String> ns = new HashMap<>();
    ns.put("ns", NS);
    XPathUtils xu = new XPathUtils(ns);
    assertEquals("hello", xu.getValueString("//ns:GetPerson/ns:personId", ((DOMSource) params.get(0)).getNode().getFirstChild()));
    assertEquals("1234", xu.getValueString("//ns:GetPerson/ns:ssn", ((DOMSource) params.get(0)).getNode().getFirstChild()));
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SourceDataBinding(org.apache.cxf.databinding.source.SourceDataBinding) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) URI(java.net.URI) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BindingInfo(org.apache.cxf.service.model.BindingInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) MessageInfo(org.apache.cxf.service.model.MessageInfo) Exchange(org.apache.cxf.message.Exchange) XPathUtils(org.apache.cxf.helpers.XPathUtils) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

MessageContentsList (org.apache.cxf.message.MessageContentsList)63 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)24 Exchange (org.apache.cxf.message.Exchange)22 Fault (org.apache.cxf.interceptor.Fault)19 Message (org.apache.cxf.message.Message)19 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)18 ArrayList (java.util.ArrayList)13 List (java.util.List)13 MessageImpl (org.apache.cxf.message.MessageImpl)11 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 Method (java.lang.reflect.Method)9 OperationInfo (org.apache.cxf.service.model.OperationInfo)9 Response (javax.ws.rs.core.Response)8 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