Search in sources :

Example 26 with MarshallException

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

the class JaffaDateConverter 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 {
    // Error out if an unsupported class is passed
    if (paramType != DateOnly.class && paramType != DateTime.class) {
        log.warn("Unsupported input. Class=" + paramType);
        throw new MarshallException(paramType);
    }
    // Extract Char Encoding from the Web Context
    String charEncoding = null;
    WebContext context = WebContextFactory.get();
    if (context != null) {
        HttpServletRequest req = context.getHttpServletRequest();
        if (req != null)
            charEncoding = req.getCharacterEncoding();
    }
    if (charEncoding == null)
        charEncoding = CHAR_ENCODER;
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL))
        return null;
    Object output;
    try {
        // Handle a millisecond input
        long millis = 0;
        if (value.length() > 0)
            millis = Long.parseLong(value);
        // DWR returns null dates as '0', so we must return a null in this case
        if (millis == 0)
            return null;
        Boolean useServerTime = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(RULE_NAME_USE_SERVER_TIME));
        if (useServerTime != null && useServerTime) {
            if (log.isInfoEnabled())
                log.info("A String representation of a date should be posted by the client, since the application rule '" + RULE_NAME_USE_SERVER_TIME + "' is set");
        }
        output = paramType == DateOnly.class ? new DateOnly(millis) : new DateTime(millis);
    } catch (NumberFormatException e) {
        try {
            // Handle a String input
            if (log.isDebugEnabled())
                log.debug("Error in parsing '" + value + "' as milliseconds since 1970. Will attempt to parse it as a String representation of a date");
            output = paramType == DateOnly.class ? DateTime.toDateOnly(Parser.parseDateTime(URLDecoder.decode(value, charEncoding), FORMAT_DATE_TIME)) : Parser.parseDateTime(URLDecoder.decode(value, charEncoding), FORMAT_DATE_TIME);
        } catch (FormatDateTimeException e1) {
            if (log.isDebugEnabled())
                log.debug("Error in parsing Date '" + value + "' using the format '" + FORMAT_DATE_TIME + '\'', e1);
            throw new MarshallException(DateOnly.class, e1);
        } catch (UnsupportedEncodingException e1) {
            if (log.isDebugEnabled())
                log.debug("Error in encoding Date '" + value + "' using the format '" + charEncoding + '\'', e1);
            throw new MarshallException(DateOnly.class, e1);
        }
    }
    if (log.isDebugEnabled())
        log.debug("Inbound '" + value + "' converted to '" + output + '\'');
    return output;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebContext(org.directwebremoting.WebContext) MarshallException(org.directwebremoting.extend.MarshallException) DateOnly(org.jaffa.datatypes.DateOnly) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DateTime(org.jaffa.datatypes.DateTime) FormatDateTimeException(org.jaffa.datatypes.exceptions.FormatDateTimeException)

Example 27 with MarshallException

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

the class JaffaDateConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    // Error out if an unsupported class is passed
    if (!(data instanceof DateOnly) && !(data instanceof DateTime)) {
        log.warn("Unsupported input. Class=" + data.getClass() + ", data=" + data);
        throw new MarshallException(data.getClass());
    }
    // Create a javascipt Date object.
    // When using server time, Pass the individual date elements. This will ensure that a similar date will be constructed in the client's timezone.
    // When not using server time, pass the millisecond since 1970 value. This will result in a date adjusted to the client's timezone.
    StringBuilder jsDateConstructor = new StringBuilder("new Date(");
    Boolean useServerTime = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(RULE_NAME_USE_SERVER_TIME));
    if (data instanceof DateOnly) {
        DateOnly d = (DateOnly) data;
        if (useServerTime != null && useServerTime)
            jsDateConstructor.append(d.year()).append(',').append(d.month()).append(',').append(d.day());
        else
            jsDateConstructor.append(d.timeInMillis());
    } else {
        DateTime d = (DateTime) data;
        if (useServerTime != null && useServerTime)
            jsDateConstructor.append(d.year()).append(',').append(d.month()).append(',').append(d.day()).append(',').append(d.hourOfDay()).append(',').append(d.minute()).append(',').append(d.second()).append(',').append(d.milli());
        else
            jsDateConstructor.append(d.timeInMillis());
    }
    String output = jsDateConstructor.append(')').toString();
    if (log.isDebugEnabled())
        log.debug("Outbound '" + data + "' converted to '" + output + '\'');
    return new SimpleOutboundVariable(output, outctx, true);
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) DateOnly(org.jaffa.datatypes.DateOnly) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) DateTime(org.jaffa.datatypes.DateTime)

Example 28 with MarshallException

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

the class DoublePointConverter method convertOutbound.

@Override
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    if (!(data instanceof DoublePoint))
        return super.convertOutbound(data, outctx);
    // Where we collect out converted children
    Map<String, OutboundVariable> ovs = new TreeMap<String, OutboundVariable>();
    // We need to do this before collecting the children to save recursion
    ObjectOutboundVariable ov = new ObjectOutboundVariable(outctx);
    outctx.put(data, ov);
    try {
        Map<String, Property> properties = getPropertyMapFromObject(data, true, false);
        for (Iterator<Entry<String, Property>> it = properties.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, Property> entry = it.next();
            String name = entry.getKey();
            Property property = entry.getValue();
            Object value = property.getValue(data);
            OutboundVariable nested;
            if (value instanceof Double) {
                // Reduce precision to save bandwidth
                Double v = Double.parseDouble(String.format("%.3f", value));
                nested = getConverterManager().convertOutbound(v, outctx);
            } else {
                nested = getConverterManager().convertOutbound(value, outctx);
            }
            ovs.put(name, nested);
        }
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
    ov.init(ovs, getJavascript());
    return ov;
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) TreeMap(java.util.TreeMap) MarshallException(org.directwebremoting.extend.MarshallException) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) Entry(java.util.Map.Entry) MarshallException(org.directwebremoting.extend.MarshallException) DoublePoint(ubic.basecode.dataStructure.DoublePoint) Property(org.directwebremoting.extend.Property)

Example 29 with MarshallException

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

Example 30 with MarshallException

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

the class ArrayConverter 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 {
    if (!paramType.isArray()) {
        throw new MarshallException(paramType);
    }
    String value = iv.getValue();
    if (value.startsWith(ProtocolConstants.INBOUND_ARRAY_START)) {
        value = value.substring(1);
    }
    if (value.endsWith(ProtocolConstants.INBOUND_ARRAY_END)) {
        value = value.substring(0, value.length() - 1);
    }
    StringTokenizer st = new StringTokenizer(value, ProtocolConstants.INBOUND_ARRAY_SEPARATOR);
    int size = st.countTokens();
    Class componentType = paramType.getComponentType();
    // componentType = LocalUtil.getNonPrimitiveType(componentType);
    Object array = Array.newInstance(componentType, size);
    // 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, array);
    InboundContext incx = iv.getLookup();
    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(incx, null, splitType, splitValue);
        Object output = converterManager.convertInbound(componentType, nested, inctx, inctx.getCurrentTypeHintContext());
        Array.set(array, i, output);
    }
    return array;
}
Also used : StringTokenizer(java.util.StringTokenizer) InboundContext(org.directwebremoting.extend.InboundContext) MarshallException(org.directwebremoting.extend.MarshallException) InboundVariable(org.directwebremoting.extend.InboundVariable)

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