Search in sources :

Example 16 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList 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 17 with MessageContentsList

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

the class SamlFormOutInterceptor method getRequestForm.

@SuppressWarnings("unchecked")
protected Form getRequestForm(Message message) {
    Object ct = message.get(Message.CONTENT_TYPE);
    if (ct == null || !MediaType.APPLICATION_FORM_URLENCODED.equalsIgnoreCase(ct.toString())) {
        return null;
    }
    MessageContentsList objs = MessageContentsList.getContentsList(message);
    if (objs != null && objs.size() == 1) {
        Object obj = objs.get(0);
        if (obj instanceof Form) {
            return (Form) obj;
        } else if (obj instanceof MultivaluedMap) {
            return new Form((MultivaluedMap<String, String>) obj);
        }
    }
    return null;
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) Form(javax.ws.rs.core.Form) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 18 with MessageContentsList

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

the class AbstractXmlSecOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (message.getExchange().get(Throwable.class) != null) {
        return;
    }
    try {
        Document doc = getDomDocument(message);
        if (doc == null) {
            return;
        }
        Document finalDoc = processDocument(message, doc);
        message.setContent(List.class, new MessageContentsList(new DOMSource(finalDoc)));
    } catch (Exception ex) {
        StringWriter sw = new StringWriter();
        ex.printStackTrace(new PrintWriter(sw));
        LOG.warning(sw.toString());
        throw new Fault(new RuntimeException(ex.getMessage() + ", stacktrace: " + sw.toString()));
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) StringWriter(java.io.StringWriter) Fault(org.apache.cxf.interceptor.Fault) Document(org.w3c.dom.Document) PrintWriter(java.io.PrintWriter)

Example 19 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList 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 20 with MessageContentsList

use of org.apache.cxf.message.MessageContentsList 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

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