Search in sources :

Example 6 with ConverterException

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

the class XML method parse.

/**
     * Parses the give XML (read from the POST Body of the Request)
     *
     * @param request an HttpServletRequest
     * @return a groovy.util.XmlSlurper
     * @throws ConverterException
     */
public static Object parse(HttpServletRequest request) throws ConverterException {
    Object xml = request.getAttribute(CACHED_XML);
    if (xml != null)
        return xml;
    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = Converter.DEFAULT_REQUEST_ENCODING;
    }
    try {
        if (!request.getMethod().equalsIgnoreCase("GET")) {
            xml = parse(request.getInputStream(), encoding);
            request.setAttribute(CACHED_XML, xml);
        }
        return xml;
    } catch (IOException e) {
        throw new ConverterException("Error parsing XML", e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException) IOException(java.io.IOException)

Example 7 with ConverterException

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

the class XML method render.

public void render(Writer out) throws ConverterException {
    stream = new StreamingMarkupWriter(out, encoding);
    writer = config.isPrettyPrint() ? new PrettyPrintXMLStreamWriter(stream) : new XMLStreamWriter(stream);
    try {
        isRendering = true;
        writer.startDocument(encoding, "1.0");
        writer.startNode(getElementName(target));
        convertAnother(target);
        writer.end();
        finalizeRender(out);
    } catch (Exception e) {
        throw new ConverterException(e);
    } finally {
        isRendering = false;
    }
}
Also used : StreamingMarkupWriter(org.grails.web.xml.StreamingMarkupWriter) PrettyPrintXMLStreamWriter(org.grails.web.xml.PrettyPrintXMLStreamWriter) ConverterException(org.grails.web.converters.exceptions.ConverterException) XMLStreamWriter(org.grails.web.xml.XMLStreamWriter) PrettyPrintXMLStreamWriter(org.grails.web.xml.PrettyPrintXMLStreamWriter) IOException(java.io.IOException) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 8 with ConverterException

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

the class ConverterUtil method createConverter.

public static <T> T createConverter(Class<T> converterClass, Object target, ApplicationContext applicationContext) throws ConverterException {
    try {
        T converter = converterClass.newInstance();
        if (converter instanceof ApplicationContextAware && applicationContext != null) {
            ((ApplicationContextAware) converter).setApplicationContext(applicationContext);
        }
        ((AbstractConverter) converter).setTarget(target);
        return converter;
    } catch (Exception e) {
        throw new ConverterException("Initialization of Converter Object " + converterClass.getName() + " failed for target " + target.getClass().getName(), e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException) ApplicationContextAware(org.springframework.context.ApplicationContextAware) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 9 with ConverterException

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

the class ClosureObjectMarshaller method marshalObject.

public void marshalObject(Object object, T converter) throws ConverterException {
    try {
        int argCount = closure.getParameterTypes().length;
        Object result = null;
        if (argCount <= 1) {
            result = closure.call(object);
        } else if (argCount == 2) {
            result = closure.call(new Object[] { object, converter });
        } else {
            throw new ConverterException("Invalid Parameter count for registered Object Marshaller for class " + clazz.getName());
        }
        if (result != null && result != object && result != converter) {
            converter.convertAnother(result);
        }
    } catch (Exception e) {
        throw e instanceof ConverterException ? (ConverterException) e : new ConverterException(e);
    }
}
Also used : ConverterException(org.grails.web.converters.exceptions.ConverterException) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 10 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, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    try {
        writer.object();
        Class<?> enumClass = en.getClass();
        json.property("enumType", enumClass.getName());
        Method nameMethod = BeanUtils.findDeclaredMethod(enumClass, "name", null);
        try {
            json.property("name", nameMethod.invoke(en));
        } catch (Exception e) {
            json.property("name", "");
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Enum with class " + en.getClass().getName(), e);
    }
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) ConverterException(org.grails.web.converters.exceptions.ConverterException) Method(java.lang.reflect.Method) 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