Search in sources :

Example 1 with ModelAndView

use of io.milton.common.ModelAndView in project lobcder by skoulouzis.

the class GetAnnotationHandler method execute.

public void execute(AnnoResource resource, OutputStream out, Range range, Map<String, String> params, String contentType) {
    Object source = resource.getSource();
    ControllerMethod cm = getBestMethod(source.getClass(), contentType, params, null);
    if (cm == null) {
        throw new RuntimeException("Method not found: " + getClass() + " - " + source.getClass());
    }
    log.trace("execute GET method: " + cm.method.getName());
    try {
        Object[] args = outer.buildInvokeArgs(resource, cm.method, range, params, contentType, out);
        Object result = cm.method.invoke(cm.controller, args);
        if (result != null) {
            log.trace("method returned a value, so write it to output");
            if (result instanceof String) {
                ModelAndView modelAndView = new ModelAndView("resource", source, result.toString());
                processTemplate(modelAndView, resource, out);
            } else if (result instanceof JsonResult) {
                JsonResult jsonr = (JsonResult) result;
                JsonWriter jsonWriter = new JsonWriter();
                jsonWriter.write(jsonr, out);
            } else if (result instanceof InputStream) {
                InputStream contentIn = (InputStream) result;
                if (range != null) {
                    StreamUtils.readTo(contentIn, out, true, false, range.getStart(), range.getFinish());
                } else {
                    try {
                        IOUtils.copy(contentIn, out);
                    } finally {
                        IOUtils.closeQuietly(contentIn);
                    }
                }
            } else if (result instanceof byte[]) {
                byte[] bytes = (byte[]) result;
                out.write(bytes);
            } else if (result instanceof ModelAndView) {
                processTemplate((ModelAndView) result, resource, out);
            } else {
                throw new RuntimeException("Unsupported return type from method: " + cm.method.getName() + " in " + cm.controller.getClass() + " should return String or byte[]");
            }
        }
        out.flush();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : InputStream(java.io.InputStream) ModelAndView(io.milton.common.ModelAndView) JsonResult(io.milton.common.JsonResult) IOException(java.io.IOException)

Aggregations

JsonResult (io.milton.common.JsonResult)1 ModelAndView (io.milton.common.ModelAndView)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1