static play.mvc.Result invokeMethod( Class<?> targetClass, play.GlobalSettings global, Method method, Map<String, Object> extractedArgs, play.mvc.Http.RequestHeader r) { try { Object[] argValues = new Object[0]; List<Object> argVals = new ArrayList<Object>(); Annotation[][] annos = method.getParameterAnnotations(); for (Annotation[] ans : annos) { PathParam pathParam = null; QueryParam queryParam = null; for (Annotation an : ans) { if (an instanceof PathParam) pathParam = (PathParam) an; else if (an instanceof QueryParam) queryParam = (QueryParam) an; } if (pathParam != null) { Object v = extractedArgs.get(pathParam.value()); if (v != null) argVals.add(v); else throw new IllegalArgumentException( "can not find annotation value for argument " + pathParam.value() + "in " + targetClass + "#" + method); } else if (queryParam != null) { String queryString = r.getQueryString(queryParam.value()); argVals.add(queryString); // string type conversion? } else throw new IllegalArgumentException( "can not find an appropriate JAX-RC annotation for an argument for method:" + targetClass + "#" + method); } argValues = argVals.toArray(argValues); return (play.mvc.Result) method.invoke(global.getControllerInstance(targetClass), argValues); } catch (InvocationTargetException cause) { System.err.println( "Exception occured while trying to invoke: " + targetClass.getName() + "#" + method.getName() + " with " + extractedArgs + " for uri:" + r.path()); throw new RuntimeException(cause.getCause()); } catch (Exception e) { throw new RuntimeException(e.getCause()); } }
static RouterClass findLongestMatch( List<RouterClass> classes, play.mvc.Http.RequestHeader reqHeader) { RouterClass ret = null; String rpath = reqHeader.path(); for (RouterClass c : classes) { if (!RegMatch.findAllIn(c.absPathPatternForValues, rpath).isEmpty()) if (ret == null || c.absPath.length() > ret.absPath.length()) { ret = c; } } return ret; }
/** * スラッシュなし 参考 * http://stackoverflow.com/questions/10004746/how-to-route-urls-in-play-2-0-so-theyre-indifferent-to-an-ending-slash * * @param requestHeader * @return */ @Override public F.Promise<SimpleResult> onHandlerNotFound(Http.RequestHeader requestHeader) { // System.out.println("onHandlerNotFound:" + requestHeader.path()); if (hasTrailingSlash(requestHeader.path())) { final String path = requestHeader.path(); return F.Promise.promise( new F.Function0<SimpleResult>() { public SimpleResult apply() { return Action.redirect(removeLastChar(path)); } }); } else { // System.out.println("onHandlerNotFound action定義なしのため、トップに遷移させます"); return F.Promise.promise( new F.Function0<SimpleResult>() { public SimpleResult apply() { return Action.redirect("/"); } }); } // return super.onHandlerNotFound(requestHeader); }
static TargetClassWithPath findLongestMatch( Set<Class<?>> classes, play.mvc.Http.RequestHeader reqHeader, String appPath) { TargetClassWithPath ret = null; String rpath = reqHeader.path(); for (Class<?> c : classes) { String path = appPath + JaxrsRouter.prefixSlash(c.getAnnotation(Path.class).value()); if (isResourcePath(rpath, path)) { if (ret == null || path.length() > ret._2().length()) { ret = new TargetClassWithPath(c, path); } } } return ret; }