use of play.mvc.Http.Context in project japid42 by branaway.
the class GlobalSettingsWithJapid method onRequest.
@Override
public Action<?> onRequest(Request request, final Method actionMethod) {
final String actionName = actionMethod.getDeclaringClass().getName() + "." + actionMethod.getName();
final Map<String, String> threadData = JapidController.threadData.get();
if (!cacheResponse) {
return new Action.Simple() {
public Promise<SimpleResult> call(Context ctx) throws Throwable {
// pass the FQN to the japid controller to determine the
// template to use
// will be cleared right when the value is retrieved in the
// japid controller
// assuming the delegate call will take place in the same
// thread
threadData.put(ACTION_METHOD, actionName);
Promise<SimpleResult> call = delegate.call(ctx);
threadData.remove(ACTION_METHOD);
return call;
}
};
}
return new Action<Cached>() {
public Promise<SimpleResult> call(Context ctx) {
try {
beforeActionInvocation(ctx, actionMethod);
SimpleResult result = null;
Request req = ctx.request();
String method = req.method();
int duration = 0;
String key = null;
Cached cachAnno = actionMethod.getAnnotation(Cached.class);
// Check the cache (only for GET or HEAD)
if ((method.equals("GET") || method.equals("HEAD")) && cachAnno != null) {
key = cachAnno.key();
if ("".equals(key) || key == null) {
key = "urlcache:" + req.uri() + ":" + req.queryString();
}
duration = cachAnno.duration();
result = (SimpleResult) Cache.get(key);
}
if (result == null) {
// pass the action name hint to japid controller
threadData.put(ACTION_METHOD, actionName);
Promise<SimpleResult> ps = delegate.call(ctx);
threadData.remove(ACTION_METHOD);
if (!StringUtils.isEmpty(key) && duration > 0) {
result = ps.get(1, TimeUnit.MILLISECONDS);
Cache.set(key, result, duration);
}
onActionInvocationResult(ctx);
return ps;
} else {
onActionInvocationResult(ctx);
return Promise.pure(result);
}
} catch (RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
};
// return new Action.Simple() {
// public Result call(Context ctx) throws Throwable {
// beforeActionInvocation(ctx, actionMethod);
// dumpIt(ctx.request(), actionMethod);
// Result call = delegate.call(ctx);
// onActionInvocationResult(ctx);
// return call;
// }
// };
}
Aggregations