Search in sources :

Example 1 with TypeHintContext

use of org.directwebremoting.extend.TypeHintContext in project jaffa-framework by jaffa-projects.

the class FlexBeanConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     *
     * Copied from BasicObjectConverter
     *
     * Added custom code to invoke the setter on the FlexBean for unknown properties.
     */
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    value = value.substring(1, value.length() - 1);
    try {
        FlexBean bean;
        if (instanceType != null) {
            bean = (FlexBean) instanceType.newInstance();
        } else {
            bean = (FlexBean) paramType.newInstance();
        }
        Map properties = getPropertyMapFromObject(bean, false, true);
        // Loop through the properties passed in
        Map tokens = extractInboundTokens(paramType, value);
        // CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout
        {
            String key = "dynaClass";
            String val = (String) tokens.remove(key);
            Property property = (Property) properties.get(key);
            if (val != null && property != null) {
                // property.getPropertyType();
                Class propType = FlexClass.class;
                String[] split = ParseUtil.splitInbound(val);
                String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
                String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
                InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
                TypeHintContext incc = createTypeHintContext(inctx, property);
                Object output = converterManager.convertInbound(propType, nested, inctx, incc);
                property.setValue(bean, output);
                // The input from the client may merely pass the name of the FlexClass
                // Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexBean
                DynaClass flexClass = bean.getDynaClass();
                if (flexClass != null) {
                    flexClass = FlexClass.instance(flexClass.getName());
                    bean = FlexBean.instance((FlexClass) flexClass);
                }
            }
        }
        // is referenced later nested down in the conversion process.
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            Property property = (Property) properties.get(key);
            if (property == null) {
                // CUSTOM CODE: Instead of logging a warning, assume that the inbound token
                // is valid, and create a descriptor for it, such that it invokes the setter
                // on the FlexBean
                Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                property = new FlexDescriptor(key, propType);
            }
            Class propType = property.getPropertyType();
            String[] split = ParseUtil.splitInbound(val);
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            TypeHintContext incc = createTypeHintContext(inctx, property);
            Object output = converterManager.convertInbound(propType, nested, inctx, incc);
            property.setValue(bean, output);
        }
        return bean;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : DynaClass(org.apache.commons.beanutils.DynaClass) TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundVariable(org.directwebremoting.extend.InboundVariable) FlexBean(org.jaffa.flexfields.FlexBean) MarshallException(org.directwebremoting.extend.MarshallException) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) DynaClass(org.apache.commons.beanutils.DynaClass) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Example 2 with TypeHintContext

use of org.directwebremoting.extend.TypeHintContext in project jaffa-framework by jaffa-projects.

the class FlexCriteriaBeanConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     *
     * Copied from BasicObjectConverter
     *
     * Added custom code to invoke the setter on the FlexCriteriaBean for unknown properties.
     */
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    value = value.substring(1, value.length() - 1);
    try {
        FlexCriteriaBean bean;
        if (instanceType != null) {
            bean = (FlexCriteriaBean) instanceType.newInstance();
        } else {
            bean = (FlexCriteriaBean) paramType.newInstance();
        }
        Map properties = getPropertyMapFromObject(bean, false, true);
        // Loop through the properties passed in
        Map tokens = extractInboundTokens(paramType, value);
        // CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout
        {
            String key = "dynaClass";
            String val = (String) tokens.remove(key);
            Property property = (Property) properties.get(key);
            if (val != null && property != null) {
                // property.getPropertyType();
                Class propType = FlexClass.class;
                String[] split = ParseUtil.splitInbound(val);
                String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
                String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
                InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
                TypeHintContext incc = createTypeHintContext(inctx, property);
                Object output = converterManager.convertInbound(propType, nested, inctx, incc);
                property.setValue(bean, output);
                // The input from the client may merely pass the name of the FlexClass
                // Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexCriteriaBean
                DynaClass flexClass = bean.getDynaClass();
                if (flexClass != null) {
                    flexClass = FlexClass.instance(flexClass.getName());
                    bean = FlexCriteriaBean.instance((FlexClass) flexClass);
                }
            }
        }
        // is referenced later nested down in the conversion process.
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            Property property = (Property) properties.get(key);
            if (property == null) {
                // CUSTOM CODE: Instead of logging a warning, assume that the inbound token
                // is valid, and create a descriptor for it, such that it invokes the setter
                // on the FlexCriteriaBean
                Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                propType = findCriteriaFieldClass(propType);
                property = new FlexDescriptor(key, propType);
            }
            Class propType = property.getPropertyType();
            String[] split = ParseUtil.splitInbound(val);
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            TypeHintContext incc = createTypeHintContext(inctx, property);
            Object output = converterManager.convertInbound(propType, nested, inctx, incc);
            property.setValue(bean, output);
        }
        return bean;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : DynaClass(org.apache.commons.beanutils.DynaClass) TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) DynaClass(org.apache.commons.beanutils.DynaClass) Map(java.util.Map) TreeMap(java.util.TreeMap) Property(org.directwebremoting.extend.Property) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 3 with TypeHintContext

use of org.directwebremoting.extend.TypeHintContext in project ma-core-public by infiniteautomation.

the class CollectionConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_ARRAY_START)) {
        throw new MarshallException(paramType, Messages.getString("CollectionConverter.FormatError", ProtocolConstants.INBOUND_ARRAY_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_ARRAY_END)) {
        throw new MarshallException(paramType, Messages.getString("CollectionConverter.FormatError", ProtocolConstants.INBOUND_ARRAY_END));
    }
    value = value.substring(1, value.length() - 1);
    try {
        TypeHintContext icc = inctx.getCurrentTypeHintContext();
        TypeHintContext subthc = icc.createChildContext(0);
        Class subtype = subthc.getExtraTypeInfo();
        // subtype.getMethod("h", null).getTypeParameters();
        Collection col;
        // at the end.
        if (Iterator.class.isAssignableFrom(paramType)) {
            col = new ArrayList();
        } else // If paramType is concrete then just use whatever we've got.
        if (!paramType.isInterface() && !Modifier.isAbstract(paramType.getModifiers())) {
            // If there is a problem creating the type then we have no way
            // of completing this - they asked for a specific type and we
            // can't create that type. I don't know of a way of finding
            // subclasses that might be instaniable so we accept failure.
            col = (Collection) paramType.newInstance();
        } else // If they want a SortedSet then use TreeSet
        if (SortedSet.class.isAssignableFrom(paramType)) {
            col = new TreeSet();
        } else // If they want a Set then use HashSet
        if (Set.class.isAssignableFrom(paramType)) {
            col = new HashSet();
        } else // If they want a List then use an ArrayList
        if (List.class.isAssignableFrom(paramType)) {
            col = new ArrayList();
        } else // If they just want a Collection then just use an ArrayList
        if (Collection.class.isAssignableFrom(paramType)) {
            col = new ArrayList();
        } else {
            throw new MarshallException(paramType);
        }
        // We should put the new object into the working map in case it
        // is referenced later nested down in the conversion process.
        inctx.addConverted(iv, paramType, col);
        StringTokenizer st = new StringTokenizer(value, ProtocolConstants.INBOUND_ARRAY_SEPARATOR);
        int size = st.countTokens();
        for (int i = 0; i < size; i++) {
            String token = st.nextToken();
            String[] split = ParseUtil.splitInbound(token);
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            Object output = config.convertInbound(subtype, nested, inctx, subthc);
            col.add(output);
        }
        // the type we created
        if (Iterator.class.isAssignableFrom(paramType)) {
            return col.iterator();
        } else {
            return col;
        }
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : TypeHintContext(org.directwebremoting.extend.TypeHintContext) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) StringTokenizer(java.util.StringTokenizer) MarshallException(org.directwebremoting.extend.MarshallException) TreeSet(java.util.TreeSet) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 4 with TypeHintContext

use of org.directwebremoting.extend.TypeHintContext in project ma-core-public by infiniteautomation.

the class BasicObjectConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    value = value.substring(1, value.length() - 1);
    try {
        Object bean;
        if (instanceType != null) {
            bean = instanceType.newInstance();
        } else {
            bean = paramType.newInstance();
        }
        // is referenced later nested down in the conversion process.
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        Map properties = getPropertyMapFromObject(bean, false, true);
        // Loop through the properties passed in
        Map tokens = extractInboundTokens(paramType, value);
        for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            Property property = (Property) properties.get(key);
            if (property == null) {
                log.warn("Missing java bean property to match javascript property: " + key + ". For causes see debug level logs:");
                log.debug("- The javascript may be refer to a property that does not exist");
                log.debug("- You may be missing the correct setter: set" + Character.toTitleCase(key.charAt(0)) + key.substring(1) + "()");
                log.debug("- The property may be excluded using include or exclude rules.");
                StringBuffer all = new StringBuffer();
                for (Iterator pit = properties.keySet().iterator(); pit.hasNext(); ) {
                    all.append(pit.next());
                    if (pit.hasNext()) {
                        all.append(',');
                    }
                }
                log.debug("Fields exist for (" + all + ").");
                continue;
            }
            Class propType = property.getPropertyType();
            String[] split = ParseUtil.splitInbound(val);
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            TypeHintContext incc = createTypeHintContext(inctx, property);
            Object output = converterManager.convertInbound(propType, nested, inctx, incc);
            property.setValue(bean, output);
        }
        return bean;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Example 5 with TypeHintContext

use of org.directwebremoting.extend.TypeHintContext in project ma-core-public by infiniteautomation.

the class DwrXmlConfigurator method processParameters.

/**
 * Collections often have missing information. This helps fill the missing
 * data in.
 * @param javascript The name of the creator
 * @param parent The container of the include and exclude elements.
 * @throws ClassNotFoundException If the type attribute can't be converted into a Class
 */
private void processParameters(String javascript, Element parent) throws ClassNotFoundException {
    NodeList nodes = parent.getElementsByTagName(ELEMENT_PARAMETER);
    for (int i = 0; i < nodes.getLength(); i++) {
        Element include = (Element) nodes.item(i);
        String methodName = include.getAttribute(ATTRIBUTE_METHOD);
        // Try to find the method that we are annotating
        Creator creator = creatorManager.getCreator(javascript);
        Class dest = creator.getType();
        Method method = null;
        Method[] methods = dest.getMethods();
        for (int j = 0; j < methods.length; j++) {
            Method test = methods[j];
            if (test.getName().equals(methodName)) {
                if (method == null) {
                    method = test;
                } else {
                    log.warn("Setting extra type info to overloaded methods may fail with <parameter .../>");
                }
            }
        }
        if (method == null) {
            log.error("Unable to find method called: " + methodName + " on type: " + dest.getName() + " from creator: " + javascript);
            continue;
        }
        String number = include.getAttribute(ATTRIBUTE_NUMBER);
        int paramNo = Integer.parseInt(number);
        String types = include.getAttribute(ATTRIBUTE_TYPE);
        StringTokenizer st = new StringTokenizer(types, ",");
        int j = 0;
        while (st.hasMoreTokens()) {
            String type = st.nextToken();
            Class clazz = LocalUtil.classForName(type.trim());
            TypeHintContext thc = new TypeHintContext(converterManager, method, paramNo).createChildContext(j++);
            converterManager.setExtraTypeInfo(thc, clazz);
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) TypeHintContext(org.directwebremoting.extend.TypeHintContext) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Creator(org.directwebremoting.extend.Creator) Method(java.lang.reflect.Method)

Aggregations

TypeHintContext (org.directwebremoting.extend.TypeHintContext)9 InboundVariable (org.directwebremoting.extend.InboundVariable)7 MarshallException (org.directwebremoting.extend.MarshallException)7 Map (java.util.Map)5 Iterator (java.util.Iterator)4 Property (org.directwebremoting.extend.Property)4 Method (java.lang.reflect.Method)3 StringTokenizer (java.util.StringTokenizer)3 TreeMap (java.util.TreeMap)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DynaClass (org.apache.commons.beanutils.DynaClass)2 Creator (org.directwebremoting.extend.Creator)2 InboundContext (org.directwebremoting.extend.InboundContext)2 FlexClass (org.jaffa.flexfields.FlexClass)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1