protected Response response() {
try {
Object ret = context.getHandler().getMethod().invoke(controller, args);
//
MimeType mimeType = null;
for (Annotation annotation : context.getHandler().getMethod().getDeclaredAnnotations()) {
if (annotation instanceof MimeType) {
mimeType = (MimeType)annotation;
} else {
mimeType = annotation.annotationType().getAnnotation(MimeType.class);
}
if (mimeType != null && mimeType.value().length > 0) {
// For now we stop but we should look at the accept types of the client
// for doing some basic content negociation
break;
}
}
//
if (ret instanceof Response) {
// We should check that it matches....
// btw we should try to enforce matching during compilation phase
// @Action -> Response.Action
// @View -> Response.Mime
// as we can do it
Response resp = (Response)ret;
if (mimeType != null) {
resp = resp.with(PropertyType.MIME_TYPE, mimeType.value()[0]);
}
return resp;
} else if (ret != null && mimeType != null) {
for (EntityMarshaller writer : Tools.loadService(EntityMarshaller.class, request.controllerPlugin.getApplication().getClassLoader())) {
for (String s : mimeType.value()) {
Streamable streamable = writer.marshall(s, context.getHandler().getMethod(), ret);
if (streamable != null) {
return Response.ok().with(PropertyType.MIME_TYPE, s).body(streamable);
}
}