Search in sources :

Example 11 with MarshallException

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

the class JDOMConverter 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 = LocalUtil.decode(iv.getValue());
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new StringReader(value));
        if (paramType == Document.class) {
            return doc;
        } else if (paramType == Element.class) {
            return doc.getRootElement();
        }
        throw new MarshallException(paramType);
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) MarshallException(org.directwebremoting.extend.MarshallException) Element(org.jdom.Element) StringReader(java.io.StringReader) Document(org.jdom.Document) MarshallException(org.directwebremoting.extend.MarshallException)

Example 12 with MarshallException

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

the class ArrayConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    if (!data.getClass().isArray()) {
        throw new MarshallException(data.getClass());
    }
    // Stash this bit of data to cope with recursion
    ArrayOutboundVariable ov = new ArrayOutboundVariable(outctx);
    outctx.put(data, ov);
    // Convert all the data members
    int size = Array.getLength(data);
    List ovs = new ArrayList();
    for (int i = 0; i < size; i++) {
        OutboundVariable nested;
        try {
            nested = converterManager.convertOutbound(Array.get(data, i), outctx);
        } catch (Exception ex) {
            String errorMessage = "Conversion error for " + data.getClass().getName() + ".";
            log.warn(errorMessage, ex);
            nested = new ErrorOutboundVariable(outctx, errorMessage, true);
        }
        ovs.add(nested);
    }
    // Group the list of converted objects into this OutboundVariable
    ov.init(ovs);
    return ov;
}
Also used : ErrorOutboundVariable(org.directwebremoting.dwrp.ErrorOutboundVariable) OutboundVariable(org.directwebremoting.extend.OutboundVariable) ArrayOutboundVariable(org.directwebremoting.dwrp.ArrayOutboundVariable) ArrayOutboundVariable(org.directwebremoting.dwrp.ArrayOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ErrorOutboundVariable(org.directwebremoting.dwrp.ErrorOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException)

Example 13 with MarshallException

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

the class BasicObjectConverter method extractInboundTokens.

/**
 * Loop over all the inputs and extract a Map of key:value pairs
 * @param paramType The type we are converting to
 * @param value The input string
 * @return A Map of the tokens in the string
 * @throws MarshallException If the marshalling fails
 */
protected Map extractInboundTokens(Class paramType, String value) throws MarshallException {
    Map tokens = new HashMap();
    StringTokenizer st = new StringTokenizer(value, ProtocolConstants.INBOUND_MAP_SEPARATOR);
    int size = st.countTokens();
    for (int i = 0; i < size; i++) {
        String token = st.nextToken();
        if (token.trim().length() == 0) {
            continue;
        }
        int colonpos = token.indexOf(ProtocolConstants.INBOUND_MAP_ENTRY);
        if (colonpos == -1) {
            throw new MarshallException(paramType, Messages.getString("BeanConverter.MissingSeparator", ProtocolConstants.INBOUND_MAP_ENTRY, token));
        }
        String key = token.substring(0, colonpos).trim();
        String val = token.substring(colonpos + 1).trim();
        tokens.put(key, val);
    }
    return tokens;
}
Also used : StringTokenizer(java.util.StringTokenizer) HashMap(java.util.HashMap) MarshallException(org.directwebremoting.extend.MarshallException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 14 with MarshallException

use of org.directwebremoting.extend.MarshallException 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 15 with MarshallException

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

the class BeanConverter method getPropertyMapFromClass.

/* (non-Javadoc)
     * @see org.directwebremoting.extend.NamedConverter#getPropertyMap(java.lang.Class, boolean, boolean)
     */
public Map getPropertyMapFromClass(Class type, boolean readRequired, boolean writeRequired) throws MarshallException {
    try {
        BeanInfo info = Introspector.getBeanInfo(type);
        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
        Map properties = new HashMap();
        for (int i = 0; i < descriptors.length; i++) {
            PropertyDescriptor descriptor = descriptors[i];
            String name = descriptor.getName();
            // We don't marshall getClass()
            if (name.equals("class")) {
                continue;
            }
            // Access rules mean we might not want to do this one
            if (!isAllowedByIncludeExcludeRules(name)) {
                continue;
            }
            if (readRequired && descriptor.getReadMethod() == null) {
                continue;
            }
            if (writeRequired && descriptor.getWriteMethod() == null) {
                continue;
            }
            properties.put(name, new PropertyDescriptorProperty(descriptor));
        }
        return properties;
    } catch (IntrospectionException ex) {
        throw new MarshallException(type, ex);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) MarshallException(org.directwebremoting.extend.MarshallException) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) PropertyDescriptorProperty(org.directwebremoting.impl.PropertyDescriptorProperty) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MarshallException (org.directwebremoting.extend.MarshallException)43 Map (java.util.Map)16 OutboundVariable (org.directwebremoting.extend.OutboundVariable)12 Iterator (java.util.Iterator)10 HashMap (java.util.HashMap)9 TreeMap (java.util.TreeMap)9 Property (org.directwebremoting.extend.Property)9 SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)8 InboundVariable (org.directwebremoting.extend.InboundVariable)8 TypeHintContext (org.directwebremoting.extend.TypeHintContext)7 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)5 IntrospectionException (java.beans.IntrospectionException)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 StringTokenizer (java.util.StringTokenizer)4 Converter (org.directwebremoting.extend.Converter)4 NamedConverter (org.directwebremoting.extend.NamedConverter)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 StringWriter (java.io.StringWriter)3