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; }
@Override public CompletionStage<Result> apply( Function<Http.RequestHeader, CompletionStage<Result>> nextFilter, Http.RequestHeader requestHeader) { long startTime = System.currentTimeMillis(); return nextFilter .apply(requestHeader) .thenApply( result -> { long endTime = System.currentTimeMillis(); long requestTime = endTime - startTime; Logger.info( "{} {} took {}ms and returned {}", requestHeader.method(), requestHeader.uri(), requestTime, result.status()); return result.withHeader("Request-Time", "" + requestTime); }); }
/** * スラッシュなし 参考 * 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; }
@Override public F.Promise<Result> onBadRequest(Http.RequestHeader requestHeader, String s) { CustomLogger.warn(requestHeader.remoteAddress(), "Bad Request: " + Json.toJson(requestHeader)); return super.onBadRequest(requestHeader, s); }
@Override public F.Promise<Result> onHandlerNotFound(Http.RequestHeader requestHeader) { CustomLogger.warn( requestHeader.remoteAddress(), "Handler not Found for: " + requestHeader.uri()); return super.onHandlerNotFound(requestHeader); }
@Override public F.Promise<Result> onError(Http.RequestHeader requestHeader, Throwable throwable) { CustomLogger.error( requestHeader.remoteAddress(), "Internal Server Error: " + Json.toJson(requestHeader)); return super.onError(requestHeader, throwable); }