Search in sources :

Example 11 with ConverterException

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

the class GroovyBeanMarshaller 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(clazz)) {
            Method readMethod = property.getReadMethod();
            String name = property.getName();
            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 : clazz.getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                writer.key(name);
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + clazz.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) GroovyObject(groovy.lang.GroovyObject) ControllerMethod(grails.web.controllers.ControllerMethod)

Example 12 with ConverterException

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

the class ValidationErrorsMarshaller method marshalObject.

public void marshalObject(Object object, JSON json) throws ConverterException {
    Errors errors = (Errors) object;
    JSONWriter writer = json.getWriter();
    try {
        writer.object();
        writer.key("errors");
        writer.array();
        for (Object o : errors.getAllErrors()) {
            if (o instanceof FieldError) {
                FieldError fe = (FieldError) o;
                writer.object();
                json.property("object", fe.getObjectName());
                json.property("field", fe.getField());
                json.property("rejected-value", fe.getRejectedValue());
                Locale locale = LocaleContextHolder.getLocale();
                if (applicationContext != null) {
                    json.property("message", applicationContext.getMessage(fe, locale));
                } else {
                    json.property("message", fe.getDefaultMessage());
                }
                writer.endObject();
            } else if (o instanceof ObjectError) {
                ObjectError fe = (ObjectError) o;
                writer.object();
                json.property("object", fe.getObjectName());
                Locale locale = LocaleContextHolder.getLocale();
                if (applicationContext != null) {
                    json.property("message", applicationContext.getMessage(fe, locale));
                } else {
                    json.property("message", fe.getDefaultMessage());
                }
                writer.endObject();
            }
        }
        writer.endArray();
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + object.getClass().getName(), e);
    }
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) Locale(java.util.Locale) Errors(org.springframework.validation.Errors) ConverterException(org.grails.web.converters.exceptions.ConverterException) ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 13 with ConverterException

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

the class GroovyBeanMarshaller method marshalObject.

public void marshalObject(Object o, XML xml) throws ConverterException {
    try {
        Class<? extends Object> clazz = o.getClass();
        List<String> excludes = xml.getExcludes(clazz);
        List<String> includes = xml.getIncludes(clazz);
        IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
        boolean isEntity = o.getClass().getAnnotation(Entity.class) != null;
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                continue;
            if (isEntity && (name.equals(GrailsDomainClassProperty.ATTACHED) || name.equals(GrailsDomainClassProperty.ERRORS)))
                continue;
            Method readMethod = property.getReadMethod();
            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);
                xml.startNode(name);
                xml.convertAnother(value);
                xml.end();
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                if (isEntity && (name.equals(GrailsDomainClassProperty.ATTACHED) || name.equals(GrailsDomainClassProperty.ERRORS)))
                    continue;
                xml.startNode(name);
                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 : Entity(grails.persistence.Entity) 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) GroovyObject(groovy.lang.GroovyObject) ControllerMethod(grails.web.controllers.ControllerMethod)

Example 14 with ConverterException

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

the class ValidationErrorsMarshaller method marshalObject.

public void marshalObject(Object object, XML xml) throws ConverterException {
    Errors errors = (Errors) object;
    try {
        for (Object o : errors.getAllErrors()) {
            if (o instanceof FieldError) {
                FieldError fe = (FieldError) o;
                xml.startNode("error");
                xml.attribute("object", fe.getObjectName());
                xml.attribute("field", fe.getField());
                xml.startNode("rejected-value").convertAnother(fe.getRejectedValue());
                xml.end();
                Locale locale = LocaleContextHolder.getLocale();
                if (applicationContext != null) {
                    xml.startNode("message").chars(applicationContext.getMessage(fe, locale)).end();
                } else {
                    String defaultMessage = fe.getDefaultMessage();
                    if (defaultMessage != null)
                        xml.startNode("message").chars(defaultMessage).end();
                }
                xml.end();
            }
        }
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + object.getClass().getName(), e);
    }
}
Also used : Locale(java.util.Locale) Errors(org.springframework.validation.Errors) ConverterException(org.grails.web.converters.exceptions.ConverterException) FieldError(org.springframework.validation.FieldError) BeansException(org.springframework.beans.BeansException) 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