Search in sources :

Example 26 with MessageContentsList

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

the class WrapperClassInInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Exchange ex = message.getExchange();
    BindingOperationInfo boi = ex.getBindingOperationInfo();
    if (Boolean.TRUE.equals(message.get(Message.PARTIAL_RESPONSE_MESSAGE)) || boi == null) {
        return;
    }
    Method method = ex.get(Method.class);
    if (method != null && method.getName().endsWith("Async")) {
        Class<?> retType = method.getReturnType();
        if (retType.getName().equals("java.util.concurrent.Future") || retType.getName().equals("javax.xml.ws.Response")) {
            return;
        }
    }
    if (boi.isUnwrappedCapable()) {
        BindingOperationInfo boi2 = boi.getUnwrappedOperation();
        OperationInfo op = boi2.getOperationInfo();
        BindingMessageInfo bmi;
        MessageInfo wrappedMessageInfo = message.get(MessageInfo.class);
        MessageInfo messageInfo;
        if (wrappedMessageInfo == boi.getOperationInfo().getInput()) {
            messageInfo = op.getInput();
            bmi = boi2.getInput();
        } else {
            messageInfo = op.getOutput();
            bmi = boi2.getOutput();
        }
        // Sometimes, an operation can be unwrapped according to WSDLServiceFactory,
        // but not according to JAX-WS. We should unify these at some point, but
        // for now check for the wrapper class.
        MessageContentsList lst = MessageContentsList.getContentsList(message);
        if (lst == null) {
            return;
        }
        message.put(MessageInfo.class, messageInfo);
        message.put(BindingMessageInfo.class, bmi);
        ex.put(BindingOperationInfo.class, boi2);
        if (isGET(message)) {
            LOG.fine("WrapperClassInInterceptor skipped in HTTP GET method");
            return;
        }
        MessagePartInfo wrapperPart = wrappedMessageInfo.getFirstMessagePart();
        Class<?> wrapperClass = wrapperPart.getTypeClass();
        Object wrappedObject = lst.get(wrapperPart.getIndex());
        if (wrapperClass == null || wrappedObject == null || (wrapperClass != null && !wrapperClass.isInstance(wrappedObject))) {
            return;
        }
        WrapperHelper helper = wrapperPart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
        if (helper == null) {
            Service service = ServiceModelUtil.getService(message.getExchange());
            DataBinding dataBinding = service.getDataBinding();
            if (dataBinding instanceof WrapperCapableDatabinding) {
                helper = createWrapperHelper((WrapperCapableDatabinding) dataBinding, messageInfo, wrappedMessageInfo, wrapperClass);
                wrapperPart.setProperty("WRAPPER_CLASS", helper);
            } else {
                return;
            }
        }
        MessageContentsList newParams;
        try {
            newParams = new MessageContentsList(helper.getWrapperParts(wrappedObject));
            List<Integer> removes = null;
            int count = 0;
            for (MessagePartInfo part : messageInfo.getMessageParts()) {
                if (Boolean.TRUE.equals(part.getProperty(ReflectionServiceFactoryBean.HEADER))) {
                    MessagePartInfo mpi = null;
                    for (MessagePartInfo mpi2 : wrappedMessageInfo.getMessageParts()) {
                        if (mpi2.getConcreteName().equals(part.getConcreteName())) {
                            mpi = mpi2;
                        }
                    }
                    if (mpi != null && lst.hasValue(mpi)) {
                        count++;
                        newParams.put(part, lst.get(mpi));
                    } else if (mpi == null || mpi.getTypeClass() == null) {
                        // header, but not mapped to a param on the method
                        if (removes == null) {
                            removes = new ArrayList<>();
                        }
                        removes.add(part.getIndex());
                    }
                } else {
                    ++count;
                }
            }
            if (count == 0) {
                newParams.clear();
            } else if (removes != null) {
                Collections.sort(removes, Collections.reverseOrder());
                for (Integer i : removes) {
                    if (i < newParams.size()) {
                        newParams.remove(i.intValue());
                    }
                }
            }
        } catch (Exception e) {
            throw new Fault(e);
        }
        message.setContent(List.class, newParams);
    }
}
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) WrapperCapableDatabinding(org.apache.cxf.databinding.WrapperCapableDatabinding) ArrayList(java.util.ArrayList) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Exchange(org.apache.cxf.message.Exchange) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) DataBinding(org.apache.cxf.databinding.DataBinding) WrapperHelper(org.apache.cxf.databinding.WrapperHelper)

Example 27 with MessageContentsList

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

the class WrapperClassOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Exchange ex = message.getExchange();
    BindingOperationInfo bop = ex.getBindingOperationInfo();
    MessageInfo messageInfo = message.get(MessageInfo.class);
    if (messageInfo == null || bop == null || !bop.isUnwrapped()) {
        return;
    }
    BindingOperationInfo newbop = bop.getWrappedOperation();
    MessageInfo wrappedMsgInfo;
    if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
        wrappedMsgInfo = newbop.getInput().getMessageInfo();
    } else {
        wrappedMsgInfo = newbop.getOutput().getMessageInfo();
    }
    Class<?> wrapped = null;
    if (wrappedMsgInfo.getMessagePartsNumber() > 0) {
        wrapped = wrappedMsgInfo.getFirstMessagePart().getTypeClass();
    }
    if (wrapped != null) {
        MessagePartInfo firstMessagePart = wrappedMsgInfo.getFirstMessagePart();
        MessageContentsList objs = MessageContentsList.getContentsList(message);
        WrapperHelper helper = firstMessagePart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
        if (helper == null) {
            helper = getWrapperHelper(message, messageInfo, wrappedMsgInfo, wrapped, firstMessagePart);
        }
        if (helper == null) {
            return;
        }
        try {
            MessageContentsList newObjs = new MessageContentsList();
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && helper instanceof AbstractWrapperHelper) {
                ((AbstractWrapperHelper) helper).setValidate(true);
            }
            Object o2 = helper.createWrapperObject(objs);
            newObjs.put(firstMessagePart, o2);
            for (MessagePartInfo p : messageInfo.getMessageParts()) {
                if (Boolean.TRUE.equals(p.getProperty(ReflectionServiceFactoryBean.HEADER))) {
                    MessagePartInfo mpi = wrappedMsgInfo.getMessagePart(p.getName());
                    if (objs.hasValue(p)) {
                        newObjs.put(mpi, objs.get(p));
                    }
                }
            }
            message.setContent(List.class, newObjs);
        } catch (Fault f) {
            throw f;
        } catch (Exception e) {
            throw new Fault(e);
        }
        // we've now wrapped the object, so use the wrapped binding op
        ex.put(BindingOperationInfo.class, newbop);
        if (messageInfo == bop.getOperationInfo().getInput()) {
            message.put(MessageInfo.class, newbop.getOperationInfo().getInput());
            message.put(BindingMessageInfo.class, newbop.getInput());
        } else if (messageInfo == bop.getOperationInfo().getOutput()) {
            message.put(MessageInfo.class, newbop.getOperationInfo().getOutput());
            message.put(BindingMessageInfo.class, newbop.getOutput());
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) AbstractWrapperHelper(org.apache.cxf.databinding.AbstractWrapperHelper) AbstractWrapperHelper(org.apache.cxf.databinding.AbstractWrapperHelper) WrapperHelper(org.apache.cxf.databinding.WrapperHelper) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo)

Example 28 with MessageContentsList

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

the class JAXWSMethodInvokerTest method testSuspendedException.

@Test
public void testSuspendedException() throws Throwable {
    Exception originalException = new RuntimeException();
    ContinuationService serviceObject = new ContinuationService(originalException);
    Method serviceMethod = ContinuationService.class.getMethod("invoke", new Class[] {});
    Exchange ex = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    ex.setInMessage(inMessage);
    inMessage.setExchange(ex);
    inMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    try {
        jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
        fail("Suspended invocation swallowed");
    } catch (SuspendedInvocationException suspendedEx) {
        assertSame(suspendedEx, serviceObject.getSuspendedException());
        assertSame(originalException, suspendedEx.getRuntimeException());
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) MessageContentsList(org.apache.cxf.message.MessageContentsList) Method(java.lang.reflect.Method) SuspendedInvocationException(org.apache.cxf.continuations.SuspendedInvocationException) MessageImpl(org.apache.cxf.message.MessageImpl) SuspendedInvocationException(org.apache.cxf.continuations.SuspendedInvocationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 29 with MessageContentsList

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

the class JAXWSMethodInvokerTest method testProviderInterpretNullAsOneway.

@Test
public void testProviderInterpretNullAsOneway() throws Throwable {
    NullableProviderService serviceObject = new NullableProviderService();
    Method serviceMethod = NullableProviderService.class.getMethod("invoke", new Class[] { Source.class });
    Exchange ex = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    inMessage.setInterceptorChain(new PhaseInterceptorChain(new TreeSet<Phase>()));
    ex.setInMessage(inMessage);
    inMessage.setExchange(ex);
    inMessage.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    // request-response with non-null response
    ex.setOneWay(false);
    MessageContentsList obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertEquals(1, obj.size());
    assertNotNull(obj.get(0));
    assertFalse(ex.isOneWay());
    // oneway with non-null response
    ex.setOneWay(true);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertNull(obj);
    assertTrue(ex.isOneWay());
    // request-response with null response, interpretNullAsOneway not set so
    // default should be true
    ex.setOneWay(false);
    serviceObject.setNullable(true);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertNull(obj);
    assertTrue(ex.isOneWay());
    // request-response with null response, interpretNullAsOneway disabled
    ex.setOneWay(false);
    serviceObject.setNullable(true);
    inMessage.put("jaxws.provider.interpretNullAsOneway", Boolean.FALSE);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertEquals(1, obj.size());
    assertNull(obj.get(0));
    assertFalse(ex.isOneWay());
    // request-response with null response, interpretNullAsOneway explicitly enabled
    ex.setOneWay(false);
    serviceObject.setNullable(true);
    inMessage.put("jaxws.provider.interpretNullAsOneway", Boolean.TRUE);
    obj = (MessageContentsList) jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] { new StreamSource() }));
    assertNull(obj);
    assertTrue(ex.isOneWay());
}
Also used : Exchange(org.apache.cxf.message.Exchange) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) MessageContentsList(org.apache.cxf.message.MessageContentsList) TreeSet(java.util.TreeSet) StreamSource(javax.xml.transform.stream.StreamSource) Method(java.lang.reflect.Method) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 30 with MessageContentsList

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

the class JAXWSMethodInvokerTest method testFaultHeadersCopy.

@Test
public void testFaultHeadersCopy() throws Throwable {
    ExceptionService serviceObject = new ExceptionService();
    Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[] {});
    Exchange ex = new ExchangeImpl();
    prepareInMessage(ex, true);
    Message msg = new MessageImpl();
    SoapMessage outMessage = new SoapMessage(msg);
    ex.setOutMessage(outMessage);
    JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
    try {
        jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
        fail("Expected fault");
    } catch (Fault fault) {
        Message outMsg = ex.getOutMessage();
        Assert.assertNotNull(outMsg);
        @SuppressWarnings("unchecked") List<Header> headers = (List<Header>) outMsg.get(Header.HEADER_LIST);
        Assert.assertEquals(1, headers.size());
        Assert.assertEquals(TEST_HEADER_NAME, headers.get(0).getName());
    }
}
Also used : Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) MessageContentsList(org.apache.cxf.message.MessageContentsList) Fault(org.apache.cxf.interceptor.Fault) Method(java.lang.reflect.Method) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Exchange(org.apache.cxf.message.Exchange) Header(org.apache.cxf.headers.Header) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) 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