Search in sources :

Example 46 with Exchange

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

the class JAXRSOutInterceptor method processResponse.

// Response shouldn't be closed here
@SuppressWarnings("resource")
private void processResponse(ServerProviderFactory providerFactory, Message message) {
    if (isResponseAlreadyHandled(message)) {
        return;
    }
    MessageContentsList objs = MessageContentsList.getContentsList(message);
    if (objs == null || objs.isEmpty()) {
        return;
    }
    Object responseObj = objs.get(0);
    Response response = null;
    if (responseObj instanceof Response) {
        response = (Response) responseObj;
        if (response.getStatus() == 500 && message.getExchange().get(JAXRSUtils.EXCEPTION_FROM_MAPPER) != null) {
            message.put(Message.RESPONSE_CODE, 500);
            return;
        }
    } else {
        int status = getStatus(message, responseObj != null ? 200 : 204);
        response = JAXRSUtils.toResponseBuilder(status).entity(responseObj).build();
    }
    Exchange exchange = message.getExchange();
    OperationResourceInfo ori = (OperationResourceInfo) exchange.get(OperationResourceInfo.class.getName());
    serializeMessage(providerFactory, message, response, ori, true);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Exchange(org.apache.cxf.message.Exchange) MessageContentsList(org.apache.cxf.message.MessageContentsList) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 47 with Exchange

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

the class XmlSecOutInterceptor method getEncoding.

private String getEncoding(Message message) {
    Exchange ex = message.getExchange();
    String encoding = (String) message.get(Message.ENCODING);
    if (encoding == null && ex.getInMessage() != null) {
        encoding = (String) ex.getInMessage().get(Message.ENCODING);
        message.put(Message.ENCODING, encoding);
    }
    if (encoding == null) {
        encoding = StandardCharsets.UTF_8.name();
        message.put(Message.ENCODING, encoding);
    }
    return encoding;
}
Also used : Exchange(org.apache.cxf.message.Exchange)

Example 48 with Exchange

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

the class DocLiteralInInterceptorTest method testUnmarshalSourceData.

@Test
public void testUnmarshalSourceData() throws Exception {
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(getClass().getResourceAsStream("resources/multiPartDocLitBareReq.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();
    Exchange exchange = new ExchangeImpl();
    Service service = control.createMock(Service.class);
    exchange.put(Service.class, service);
    EasyMock.expect(service.getDataBinding()).andReturn(new SourceDataBinding());
    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);
    OperationInfo operationInfo = new OperationInfo();
    operationInfo.setProperty("operation.is.synthetic", Boolean.TRUE);
    MessageInfo messageInfo = new MessageInfo(operationInfo, Type.INPUT, new QName("http://foo.com", "bar"));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo1"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo2"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo3"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo4"), null));
    for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
        mpi.setMessageContainer(messageInfo);
    }
    operationInfo.setInput("inputName", messageInfo);
    BindingOperationInfo boi = new BindingOperationInfo(null, operationInfo);
    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();
    ServiceInfo serviceInfo = control.createMock(ServiceInfo.class);
    EasyMock.expect(endpointInfo.getService()).andReturn(serviceInfo).anyTimes();
    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();
    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);
    assertEquals(4, params.size());
    assertEquals("StringDefaultInputElem", ((DOMSource) params.get(0)).getNode().getFirstChild().getNodeName());
    assertEquals("IntParamInElem", ((DOMSource) params.get(1)).getNode().getFirstChild().getNodeName());
}
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) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 49 with Exchange

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

the class AbstractEndpointSelectionInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Exchange ex = message.getExchange();
    Set<Endpoint> endpoints = CastUtils.cast((Set<?>) ex.get(MultipleEndpointObserver.ENDPOINTS));
    Endpoint ep = selectEndpoint(message, endpoints);
    if (ep == null) {
        return;
    }
    ex.put(Endpoint.class, ep);
    ex.put(Binding.class, ep.getBinding());
    ex.put(Service.class, ep.getService());
    InterceptorChain chain = message.getInterceptorChain();
    chain.add(ep.getInInterceptors());
    chain.add(ep.getBinding().getInInterceptors());
    chain.add(ep.getService().getInInterceptors());
    chain.setFaultObserver(ep.getOutFaultObserver());
}
Also used : Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 50 with Exchange

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

the class BareInInterceptor method handleMessage.

public void handleMessage(Message message) {
    if (isGET(message) && message.getContent(List.class) != null) {
        LOG.fine("BareInInterceptor skipped in HTTP GET method");
        return;
    }
    DepthXMLStreamReader xmlReader = getXMLStreamReader(message);
    Exchange exchange = message.getExchange();
    DataReader<XMLStreamReader> dr = getDataReader(message);
    MessageContentsList parameters = new MessageContentsList();
    Endpoint ep = exchange.getEndpoint();
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    ServiceInfo si = ep.getEndpointInfo().getService();
    BindingMessageInfo msgInfo = null;
    boolean client = isRequestor(message);
    Collection<OperationInfo> ops = null;
    if (bop == null) {
        ops = new ArrayList<>();
        ops.addAll(si.getInterface().getOperations());
        if (xmlReader.getEventType() == XMLStreamConstants.END_ELEMENT && !client) {
            // TO DO : check duplicate operation with no input
            for (OperationInfo op : ops) {
                MessageInfo bmsg = op.getInput();
                if (bmsg.getMessagePartsNumber() == 0) {
                    BindingOperationInfo boi = ep.getEndpointInfo().getBinding().getOperation(op);
                    exchange.put(BindingOperationInfo.class, boi);
                    exchange.setOneWay(op.isOneWay());
                }
            }
        }
    } else {
        getMessageInfo(message, bop);
        if (client) {
            msgInfo = bop.getOutput();
        } else {
            msgInfo = bop.getInput();
        }
    }
    int paramNum = 0;
    while (StaxUtils.toNextElement(xmlReader)) {
        QName elName = xmlReader.getName();
        Object o = null;
        MessagePartInfo p;
        if (msgInfo != null && msgInfo.getMessageParts() != null) {
            assert msgInfo.getMessageParts().size() > paramNum;
            p = msgInfo.getMessageParts().get(paramNum);
        } else {
            p = findMessagePart(exchange, ops, elName, client, paramNum, message);
        }
        if (p == null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("NO_PART_FOUND", LOG, elName), Fault.FAULT_CODE_CLIENT);
        }
        try {
            o = dr.read(p, xmlReader);
        } catch (Fault fault) {
            if (!isRequestor(message)) {
                fault.setFaultCode(Fault.FAULT_CODE_CLIENT);
            }
            throw fault;
        }
        if (o != null) {
            parameters.put(p, o);
        }
        paramNum++;
    }
    if (!parameters.isEmpty()) {
        message.setContent(List.class, parameters);
    }
}
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) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessageContentsList(org.apache.cxf.message.MessageContentsList) QName(javax.xml.namespace.QName) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Exchange(org.apache.cxf.message.Exchange) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Aggregations

Exchange (org.apache.cxf.message.Exchange)272 Message (org.apache.cxf.message.Message)151 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)131 MessageImpl (org.apache.cxf.message.MessageImpl)118 Test (org.junit.Test)93 Endpoint (org.apache.cxf.endpoint.Endpoint)66 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)50 QName (javax.xml.namespace.QName)42 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)33 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)32 Bus (org.apache.cxf.Bus)30 Fault (org.apache.cxf.interceptor.Fault)27 AddressingProperties (org.apache.cxf.ws.addressing.AddressingProperties)26 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)25 Conduit (org.apache.cxf.transport.Conduit)25 MessageContentsList (org.apache.cxf.message.MessageContentsList)22 SOAPMessage (javax.xml.soap.SOAPMessage)19 XMLStreamReader (javax.xml.stream.XMLStreamReader)19 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)19