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);
}
}
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;
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations