Search in sources :

Example 1 with InboundVariable

use of org.directwebremoting.extend.InboundVariable 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 InboundVariable

use of org.directwebremoting.extend.InboundVariable 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 InboundVariable

use of org.directwebremoting.extend.InboundVariable 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 InboundVariable

use of org.directwebremoting.extend.InboundVariable 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 InboundVariable

use of org.directwebremoting.extend.InboundVariable in project Gemma by PavlidisLab.

the class CharacteristicConverter method convertInbound.

@SuppressWarnings("unchecked")
@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 {
        Map<String, String> tokens = extractInboundTokens(paramType, value);
        Object bean;
        // usually there is a "null" valueUri.
        if (tokens.containsKey("valueUri")) {
            String[] split = ParseUtil.splitInbound(tokens.get("valueUri"));
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            if (StringUtils.isBlank(nested.getValue()) || "null".equals(nested.getValue())) {
                bean = ubic.gemma.model.common.description.Characteristic.Factory.newInstance();
            } else {
                bean = ubic.gemma.model.common.description.VocabCharacteristic.Factory.newInstance();
            }
        } else {
            bean = ubic.gemma.model.common.description.Characteristic.Factory.newInstance();
        }
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        Map<String, Property> properties = getPropertyMapFromObject(bean, false, true);
        for (Iterator<Entry<String, String>> it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, String> entry = it.next();
            String key = entry.getKey();
            String val = entry.getValue();
            Property property = properties.get(key);
            if (property == null) {
                // 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);
            // Collection
            if ((key.equals("properties")) && (output instanceof ArrayList)) {
                ArrayList<Object> propertyList = (ArrayList<Object>) output;
                output = new HashSet<Object>(propertyList);
            }
            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) ArrayList(java.util.ArrayList) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Entry(java.util.Map.Entry) MarshallException(org.directwebremoting.extend.MarshallException) Property(org.directwebremoting.extend.Property) Map(java.util.Map)

Aggregations

InboundVariable (org.directwebremoting.extend.InboundVariable)8 MarshallException (org.directwebremoting.extend.MarshallException)8 TypeHintContext (org.directwebremoting.extend.TypeHintContext)7 Map (java.util.Map)5 Iterator (java.util.Iterator)4 Property (org.directwebremoting.extend.Property)4 StringTokenizer (java.util.StringTokenizer)3 TreeMap (java.util.TreeMap)3 InboundContext (org.directwebremoting.extend.InboundContext)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DynaClass (org.apache.commons.beanutils.DynaClass)2 FlexClass (org.jaffa.flexfields.FlexClass)2 Method (java.lang.reflect.Method)1 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 TreeSet (java.util.TreeSet)1