Search in sources :

Example 6 with WrapperHelper

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

the class JAXBWrapperHelperTest method getBooleanTypeWrappedPart.

@Test
public void getBooleanTypeWrappedPart() throws Exception {
    SetIsOK ok = new SetIsOK();
    ok.setParameter3(new boolean[] { true, false });
    ok.setParameter4("hello");
    List<String> partNames = Arrays.asList(new String[] { "Parameter1", "Parameter2", "Parameter3", "Parameter4", "Parameter5" });
    List<String> elTypeNames = Arrays.asList(new String[] { "boolean", "int", "boolean", "string", "string" });
    List<Class<?>> partClasses = Arrays.asList(new Class<?>[] { Boolean.TYPE, Integer.TYPE, boolean[].class, String.class, List.class });
    WrapperHelper wh = new JAXBDataBinding().createWrapperHelper(SetIsOK.class, null, partNames, elTypeNames, partClasses);
    List<Object> lst = wh.getWrapperParts(ok);
    assertEquals(5, lst.size());
    assertTrue(lst.get(0) instanceof Boolean);
    assertTrue(lst.get(1) instanceof Integer);
    assertTrue(lst.get(2) instanceof boolean[]);
    assertTrue(((boolean[]) lst.get(2))[0]);
    assertFalse(((boolean[]) lst.get(2))[1]);
    assertEquals("hello", lst.get(3));
    lst.set(0, Boolean.TRUE);
    Object o = wh.createWrapperObject(lst);
    assertNotNull(0);
    ok = (SetIsOK) o;
    assertTrue(ok.isParameter1());
    assertTrue(ok.getParameter3()[0]);
    assertFalse(ok.getParameter3()[1]);
    assertEquals("hello", ok.getParameter4());
}
Also used : WrapperHelper(org.apache.cxf.databinding.WrapperHelper) Test(org.junit.Test)

Example 7 with WrapperHelper

use of org.apache.cxf.databinding.WrapperHelper 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 ("java.util.concurrent.Future".equals(retType.getName()) || "javax.xml.ws.Response".equals(retType.getName())) {
            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.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 8 with WrapperHelper

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

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

the class WrapperHelperClassLoader method compile.

@Override
public WrapperHelper compile(Class<?> wrapperType, Method[] setMethods, Method[] getMethods, Method[] jaxbMethods, Field[] fields, Object objectFactory) {
    int count = 1;
    String newClassName = wrapperType.getName() + "_WrapperTypeHelper" + count;
    Class<?> cls = findClass(newClassName, wrapperType);
    while (cls != null) {
        try {
            WrapperHelper helper = WrapperHelper.class.cast(cls.getDeclaredConstructor().newInstance());
            if (!helper.getSignature().equals(WrapperHelperClassGenerator.computeSignature(setMethods, getMethods))) {
                count++;
                newClassName = wrapperType.getName() + "_WrapperTypeHelper" + count;
                cls = findClass(newClassName, wrapperType);
            } else {
                return helper;
            }
        } catch (Exception e) {
            return null;
        }
    }
    return null;
}
Also used : WrapperHelper(org.apache.cxf.databinding.WrapperHelper)

Example 10 with WrapperHelper

use of org.apache.cxf.databinding.WrapperHelper 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

WrapperHelper (org.apache.cxf.databinding.WrapperHelper)10 Service (org.apache.cxf.service.Service)5 OperationInfo (org.apache.cxf.service.model.OperationInfo)4 ArrayList (java.util.ArrayList)3 JAXBDataBinding (org.apache.cxf.jaxb.JAXBDataBinding)3 JaxWsImplementorInfo (org.apache.cxf.jaxws.support.JaxWsImplementorInfo)3 JaxWsServiceFactoryBean (org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean)3 InterfaceInfo (org.apache.cxf.service.model.InterfaceInfo)3 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)3 List (java.util.List)2 JAXBContext (javax.xml.bind.JAXBContext)2 Marshaller (javax.xml.bind.Marshaller)2 AbstractWrapperHelper (org.apache.cxf.databinding.AbstractWrapperHelper)2 DataBinding (org.apache.cxf.databinding.DataBinding)2 WrapperCapableDatabinding (org.apache.cxf.databinding.WrapperCapableDatabinding)2 Fault (org.apache.cxf.interceptor.Fault)2 JAXBWrapperHelper (org.apache.cxf.jaxb.JAXBWrapperHelper)2 Exchange (org.apache.cxf.message.Exchange)2 MessageContentsList (org.apache.cxf.message.MessageContentsList)2 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)2