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