Search in sources :

Example 1 with WrapperCapableDatabinding

use of org.apache.cxf.databinding.WrapperCapableDatabinding 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 2 with WrapperCapableDatabinding

use of org.apache.cxf.databinding.WrapperCapableDatabinding in project cxf by apache.

the class WrapperClassOutInterceptor method getWrapperHelper.

private synchronized WrapperHelper getWrapperHelper(Message message, MessageInfo messageInfo, MessageInfo wrappedMessageInfo, Class<?> wrapClass, MessagePartInfo messagePartInfo) {
    WrapperHelper helper = messagePartInfo.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, wrapClass);
            messagePartInfo.setProperty("WRAPPER_CLASS", helper);
        }
    }
    return helper;
}
Also used : DataBinding(org.apache.cxf.databinding.DataBinding) WrapperCapableDatabinding(org.apache.cxf.databinding.WrapperCapableDatabinding) AbstractWrapperHelper(org.apache.cxf.databinding.AbstractWrapperHelper) WrapperHelper(org.apache.cxf.databinding.WrapperHelper) Service(org.apache.cxf.service.Service)

Aggregations

DataBinding (org.apache.cxf.databinding.DataBinding)2 WrapperCapableDatabinding (org.apache.cxf.databinding.WrapperCapableDatabinding)2 WrapperHelper (org.apache.cxf.databinding.WrapperHelper)2 Service (org.apache.cxf.service.Service)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 AbstractWrapperHelper (org.apache.cxf.databinding.AbstractWrapperHelper)1 Fault (org.apache.cxf.interceptor.Fault)1 Exchange (org.apache.cxf.message.Exchange)1 MessageContentsList (org.apache.cxf.message.MessageContentsList)1 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)1 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)1 MessageInfo (org.apache.cxf.service.model.MessageInfo)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 OperationInfo (org.apache.cxf.service.model.OperationInfo)1