Search in sources :

Example 1 with ConverterException

use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.

the class Base64ByteArrayMarshaller method marshalObject.

public void marshalObject(Object object, XML xml) throws ConverterException {
    xml.attribute("encoding", "BASE-64");
    xml.chars("");
    Writable w;
    if (object instanceof byte[]) {
        w = EncodingGroovyMethods.encodeBase64((byte[]) object);
    } else {
        w = EncodingGroovyMethods.encodeBase64((Byte[]) object);
    }
    try {
        w.writeTo(xml.getStream());
    } catch (IOException e) {
        throw new ConverterException(e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException) Writable(groovy.lang.Writable) IOException(java.io.IOException)

Example 2 with ConverterException

use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.

the class EnumMarshaller method marshalObject.

public void marshalObject(Object en, XML xml) throws ConverterException {
    try {
        Class<?> enumClass = en.getClass();
        xml.attribute("enumType", enumClass.getName());
        Method nameMethod = BeanUtils.findDeclaredMethod(enumClass, "name", null);
        try {
            xml.chars(nameMethod.invoke(en).toString());
        } catch (Exception e) {
        // ignored
        }
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Enum with class " + en.getClass().getName(), e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException) Method(java.lang.reflect.Method) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 3 with ConverterException

use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.

the class GenericJavaBeanMarshaller method marshalObject.

public void marshalObject(Object o, XML xml) throws ConverterException {
    try {
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            Method readMethod = property.getReadMethod();
            if (readMethod != null) {
                Object value = readMethod.invoke(o, (Object[]) null);
                xml.startNode(name);
                xml.convertAnother(value);
                xml.end();
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (field.isAccessible() && Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                xml.startNode(field.getName());
                xml.convertAnother(field.get(o));
                xml.end();
            }
        }
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}
Also used : Field(java.lang.reflect.Field) ConverterException(org.grails.web.converters.exceptions.ConverterException) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 4 with ConverterException

use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.

the class GenericJavaBeanMarshaller method marshalObject.

public void marshalObject(Object o, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    Class<? extends Object> clazz = o.getClass();
    List<String> excludes = json.getExcludes(clazz);
    List<String> includes = json.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    try {
        writer.object();
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            Method readMethod = property.getReadMethod();
            if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                continue;
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                if (readMethod.getAnnotation(PersistenceMethod.class) != null)
                    continue;
                if (readMethod.getAnnotation(ControllerMethod.class) != null)
                    continue;
                Object value = readMethod.invoke(o, (Object[]) null);
                writer.key(name);
                json.convertAnother(value);
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (field.isAccessible() && Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                writer.key(field.getName());
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) ConverterException(org.grails.web.converters.exceptions.ConverterException) PropertyDescriptor(java.beans.PropertyDescriptor) ControllerMethod(grails.web.controllers.ControllerMethod) PersistenceMethod(grails.persistence.PersistenceMethod) Method(java.lang.reflect.Method) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) ConverterException(org.grails.web.converters.exceptions.ConverterException) Field(java.lang.reflect.Field) PersistenceMethod(grails.persistence.PersistenceMethod) ControllerMethod(grails.web.controllers.ControllerMethod)

Example 5 with ConverterException

use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.

the class JSON method parse.

/**
     * Parses the given request's InputStream and returns either a JSONObject or a JSONArray
     *
     * @param request the JSON Request
     * @return either a JSONObject or a JSONArray - depending on the given JSON
     * @throws ConverterException when the JSON content is not valid
     */
public static Object parse(HttpServletRequest request) throws ConverterException {
    Object json = request.getAttribute(CACHED_JSON);
    if (json != null) {
        return json;
    }
    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = Converter.DEFAULT_REQUEST_ENCODING;
    }
    try {
        PushbackInputStream pushbackInputStream = null;
        int firstByte = -1;
        try {
            pushbackInputStream = new PushbackInputStream(request.getInputStream());
            firstByte = pushbackInputStream.read();
        } catch (IOException ioe) {
        }
        // code has only been changed from here down
        if (firstByte == -1) {
            json = new JSONObject();
        } else {
            pushbackInputStream.unread(firstByte);
            json = parse(pushbackInputStream, encoding);
        }
        request.setAttribute(CACHED_JSON, json);
        return json;
    } catch (IOException e) {
        throw new ConverterException("Error parsing JSON", e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException)

Aggregations

ConverterException (org.grails.web.converters.exceptions.ConverterException)14 Method (java.lang.reflect.Method)6 PropertyDescriptor (java.beans.PropertyDescriptor)4 Field (java.lang.reflect.Field)4 JSONWriter (org.grails.web.json.JSONWriter)4 PersistenceMethod (grails.persistence.PersistenceMethod)3 ControllerMethod (grails.web.controllers.ControllerMethod)3 IOException (java.io.IOException)3 IncludeExcludeSupport (org.grails.core.util.IncludeExcludeSupport)3 GroovyObject (groovy.lang.GroovyObject)2 Locale (java.util.Locale)2 Errors (org.springframework.validation.Errors)2 FieldError (org.springframework.validation.FieldError)2 Entity (grails.persistence.Entity)1 Writable (groovy.lang.Writable)1 PrettyPrintXMLStreamWriter (org.grails.web.xml.PrettyPrintXMLStreamWriter)1 StreamingMarkupWriter (org.grails.web.xml.StreamingMarkupWriter)1 XMLStreamWriter (org.grails.web.xml.XMLStreamWriter)1 BeansException (org.springframework.beans.BeansException)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1