Search in sources :

Example 56 with Exchange

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

the class HolderOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    MessageContentsList outObjects = MessageContentsList.getContentsList(message);
    Exchange exchange = message.getExchange();
    OperationInfo op = exchange.getBindingOperationInfo() == null ? null : exchange.getBindingOperationInfo().getOperationInfo();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("op: " + op);
        if (null != op) {
            LOG.fine("op.hasOutput(): " + op.hasOutput());
            if (op.hasOutput()) {
                LOG.fine("op.getOutput().size(): " + op.getOutput().size());
            }
        }
    }
    if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
        LOG.fine("Returning.");
        return;
    }
    if (!Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
        List<MessagePartInfo> parts = op.getOutput().getMessageParts();
        MessageContentsList inObjects = MessageContentsList.getContentsList(exchange.getInMessage());
        if (inObjects != null) {
            if (!(inObjects == outObjects)) {
                for (int x = 0; x < inObjects.size(); x++) {
                    Object o = inObjects.get(x);
                    if (o instanceof Holder) {
                        outObjects.set(x + 1, o);
                    }
                }
            } else {
                LOG.severe("CANNOT_SET_HOLDER_OBJECTS");
                throw new Fault(new org.apache.cxf.common.i18n.Message("CANNOT_SET_HOLDER_OBJECTS", LOG));
            }
        }
        for (MessagePartInfo part : parts) {
            if (part.getIndex() > 0 && part.getTypeClass() != null) {
                Holder<?> holder = (Holder<?>) outObjects.get(part);
                outObjects.put(part, holder.value);
            }
        }
    } else {
        List<Object> holders = new ArrayList<>(outObjects);
        for (int x = 0; x < outObjects.size(); x++) {
            Object o = outObjects.get(x);
            if (o instanceof Holder) {
                outObjects.set(x, ((Holder<?>) o).value);
            } else {
                holders.set(x, null);
            }
        }
        message.put(HolderInInterceptor.CLIENT_HOLDERS, holders);
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) Holder(javax.xml.ws.Holder) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Exchange(org.apache.cxf.message.Exchange)

Example 57 with Exchange

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

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

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

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

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