Search in sources :

Example 6 with MarshallException

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

the class CollectionConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    // First we need to get ourselves the collection data
    Iterator it;
    if (data instanceof Collection) {
        Collection col = (Collection) data;
        it = col.iterator();
    } else if (data instanceof Iterator) {
        it = (Iterator) data;
    } else {
        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
    List ovs = new ArrayList();
    while (it.hasNext()) {
        Object member = it.next();
        OutboundVariable nested;
        try {
            nested = config.convertOutbound(member, 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) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ErrorOutboundVariable(org.directwebremoting.dwrp.ErrorOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException)

Example 7 with MarshallException

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

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

the class DOM4JConverter 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 {
        SAXReader xmlReader = new SAXReader();
        Document doc = xmlReader.read(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 : SAXReader(org.dom4j.io.SAXReader) MarshallException(org.directwebremoting.extend.MarshallException) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) MarshallException(org.directwebremoting.extend.MarshallException)

Example 9 with MarshallException

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

the class DateConverter 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;
    }
    try {
        long millis = 0;
        if (value.length() > 0) {
            millis = Long.parseLong(value);
        }
        Date date = new Date(millis);
        if (paramType == Date.class) {
            return date;
        } else if (paramType == java.sql.Date.class) {
            return new java.sql.Date(date.getTime());
        } else if (paramType == Time.class) {
            return new Time(date.getTime());
        } else if (paramType == Timestamp.class) {
            return new Timestamp(date.getTime());
        } else if (paramType == Calendar.class) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        } else {
            throw new MarshallException(paramType);
        }
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) Calendar(java.util.Calendar) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) MarshallException(org.directwebremoting.extend.MarshallException)

Example 10 with MarshallException

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

the class ExceptionConverter method getPropertyMapFromClass.

/* (non-Javadoc)
     * @see org.directwebremoting.convert.BasicBeanConverter#getPropertyDescriptors(java.lang.Class, boolean, boolean)
     */
public Map getPropertyMapFromClass(Class type, boolean readRequired, boolean writeRequired) throws MarshallException {
    Map descriptors = super.getPropertyMapFromClass(type, readRequired, writeRequired);
    descriptors.put("javaClassName", new PlainProperty("javaClassName", type.getName()));
    // (fix for Bean Introspector peculiarities)
    try {
        fixMissingThrowableProperty(descriptors, "message", "getMessage");
        fixMissingThrowableProperty(descriptors, "cause", "getCause");
    } catch (IntrospectionException ex) {
        throw new MarshallException(type, ex);
    }
    return descriptors;
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) IntrospectionException(java.beans.IntrospectionException) Map(java.util.Map)

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