Search in sources :

Example 1 with SimpleResult

use of play.mvc.SimpleResult in project japid42 by branaway.

the class JaxrsRouter method handlerFor.

/**
	 * provides a handler for given request
	 * 
	 * @param global
	 *            the global of the current play application
	 * @param r
	 *            request header
	 * @return action handler
	 * 
	 */
public static Handler handlerFor(final play.mvc.Http.RequestHeader r) {
    if (assetServing.length == 2 && r.path().startsWith(assetServing[0])) {
        //			return controllers.Assets.at(assetServing[1], r.path().replaceFirst(assetServing[0], ""));
        return null;
    } else {
        final RouterClass targetRouterClass = RouterUtils.findLongestMatch(routerClasses, r);
        if (targetRouterClass == null)
            return null;
        final Tuple<Method, Object[]> methodWithArgs = targetRouterClass.findMethodAndGenerateArgs(r);
        if (methodWithArgs != null) {
            ResultBuilder resultBuilder = new ResultBuilder() {

                @Override
                public SimpleResult create() {
                    try {
                        Method m = methodWithArgs._1;
                        Class<?> cl = targetRouterClass.clz;
                        Object obj = global.getControllerInstance(cl);
                        if (obj == null && (m.getModifiers() & Modifier.STATIC) != Modifier.STATIC) {
                            throw new RuntimeException("the action method is not static while the target object is null: " + targetRouterClass.clz + "#" + m.getName());
                        }
                        Object[] args = methodWithArgs._2;
                        SimpleResult result = (SimpleResult) m.invoke(obj, args);
                        Produces produces = methodWithArgs._1.getAnnotation(Produces.class);
                        return produces != null ? new WrapProducer(produces.value()[0], result) : result;
                    } catch (InvocationTargetException cause) {
                        System.err.println("Exception occured while trying to invoke: " + targetRouterClass.clz.getName() + "#" + methodWithArgs._1.getName() + " with " + methodWithArgs._2 + " for uri:" + r.path());
                        throw new RuntimeException(cause.getCause());
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e.getCause());
                    }
                }
            };
            JavaActionBridge handler = new JavaActionBridge(targetRouterClass.clz, methodWithArgs._1, resultBuilder);
            return handler;
        }
    }
    JapidFlags.debug("Japid router could not route this request");
    return null;
}
Also used : Method(java.lang.reflect.Method) SimpleResult(play.mvc.SimpleResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Produces(javax.ws.rs.Produces)

Example 2 with SimpleResult

use of play.mvc.SimpleResult 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;
// }
// };
}
Also used : Context(play.mvc.Http.Context) Action(play.mvc.Action) Request(play.mvc.Http.Request) Cached(play.cache.Cached) SimpleResult(play.mvc.SimpleResult)

Aggregations

SimpleResult (play.mvc.SimpleResult)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Produces (javax.ws.rs.Produces)1 Cached (play.cache.Cached)1 Action (play.mvc.Action)1 Context (play.mvc.Http.Context)1 Request (play.mvc.Http.Request)1