Search in sources :

Example 1 with JsonResult

use of io.milton.common.JsonResult 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)

Example 2 with JsonResult

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

the class AnnoResource method processForm.

@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException, ConflictException {
    Request request = HttpManager.request();
    Object result = annoFactory.postAnnotationHandler.execute(this, request, parameters);
    if (result instanceof String) {
        String redirect = (String) result;
        return redirect;
    } else if (result instanceof JsonResult) {
        jsonResult = (JsonResult) result;
    } else {
        jsonResult = JsonResult.returnData(getHref(), result);
    }
    return null;
}
Also used : Request(io.milton.http.Request) JsonResult(io.milton.common.JsonResult)

Aggregations

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