public static void deleteFileAsync(Long id, String path) throws IOException { models.Upload upload = Uploads.getUpload(id); File uploadsDir = Util.getUploadDir(upload.id); File file = new File(uploadsDir, path); deleteFileImpl(path, file, uploadsDir, upload); String message = Scope.Flash.current().get("message"); renderJSON("{\"message\":\"" + message + "\"}"); }
@SuppressWarnings("unchecked") public static void resolve(Http.Request request, Http.Response response) throws NotFound, RenderStatic { if (!Play.started) { return; } Http.Request.current.set(request); Http.Response.current.set(response); Scope.Params.current.set(request.params); Scope.RenderArgs.current.set(new Scope.RenderArgs()); Scope.RouteArgs.current.set(new Scope.RouteArgs()); Scope.Session.current.set(Scope.Session.restore()); Scope.Flash.current.set(Scope.Flash.restore()); CachedBoundActionMethodArgs.init(); ControllersEnhancer.currentAction.set(new Stack<String>()); if (request.resolved) { return; } // Route and resolve format if not already done if (request.action == null) { Play.pluginCollection.routeRequest(request); Route route = Router.route(request); Play.pluginCollection.onRequestRouting(route); } request.resolveFormat(); // Find the action method try { Method actionMethod = null; Object[] ca = getActionMethod(request.action); actionMethod = (Method) ca[1]; request.controller = ((Class) ca[0]).getName().substring(12).replace("$", ""); request.controllerClass = ((Class) ca[0]); request.actionMethod = actionMethod.getName(); request.action = request.controller + "." + request.actionMethod; request.invokedMethod = actionMethod; if (Logger.isTraceEnabled()) { Logger.trace("------- %s", actionMethod); } request.resolved = true; } catch (ActionNotFoundException e) { // Logger.error(e, "%s action not found", e.getAction()); // bran: avoid excessive messages Logger.error("%s action not found", e.getAction()); throw new NotFound(String.format("%s action not found", e.getAction())); } }
/** * Returns the user url (eg: http://user.wordpress.com) for an OpenID service. The method checks * if the userFormat has a {username} tag and if it does then looks for the username value in the * request parameters. * * <p>If there is no {username} tag the userFormat is used a passed by the subclass. This is * because some providers (eg: google or yahoo) do not need the username in the url. * * @return The url representing the user. */ protected String getUser() { final String user; if (needsUsername) { final String username = Scope.Params.current().get(OPENID_USER); if (username == null || username.trim().length() == 0) { Scope.Flash.current().error(Messages.get(SECURESOCIAL_OPEN_ID_USER_NOT_SPECIFIED)); throw new AuthenticationException(); } user = userFormat.replaceFirst(USERNAME_REGEX, username); } else { user = userFormat; } return user; }
protected static Map<String, Object> getBindingForErrors(Exception e, boolean isError) { Map<String, Object> binding = new HashMap<String, Object>(); if (!isError) { binding.put("result", e); } else { binding.put("exception", e); } binding.put("session", Scope.Session.current()); binding.put("request", Http.Request.current()); binding.put("flash", Scope.Flash.current()); binding.put("params", Scope.Params.current()); binding.put("play", new Play()); try { binding.put("errors", Validation.errors()); } catch (Exception ex) { // Logger.error(ex, "Error when getting Validation errors"); } return binding; }
public void serve404( HttpServletRequest servletRequest, HttpServletResponse servletResponse, NotFound e) { Logger.warn( "404 -> %s %s (%s)", servletRequest.getMethod(), servletRequest.getRequestURI(), e.getMessage()); servletResponse.setStatus(404); servletResponse.setContentType("text/html"); Map<String, Object> binding = new HashMap<String, Object>(); binding.put("result", e); binding.put("session", Scope.Session.current()); binding.put("request", Http.Request.current()); binding.put("flash", Scope.Flash.current()); binding.put("params", Scope.Params.current()); binding.put("play", new Play()); try { binding.put("errors", Validation.errors()); } catch (Exception ex) { // } String format = Request.current().format; servletResponse.setStatus(404); // Do we have an ajax request? If we have then we want to display some text even if it is html // that is requested if ("XMLHttpRequest".equals(servletRequest.getHeader("X-Requested-With")) && (format == null || format.equals("html"))) { format = "txt"; } if (format == null) { format = "txt"; } servletResponse.setContentType(MimeTypes.getContentType("404." + format, "text/plain")); String errorHtml = TemplateLoader.load("errors/404." + format).render(binding); try { servletResponse.getOutputStream().write(errorHtml.getBytes(Response.current().encoding)); } catch (Exception fex) { Logger.error(fex, "(encoding ?)"); } }
public void serve500(Exception e, HttpServletRequest request, HttpServletResponse response) { try { Map<String, Object> binding = new HashMap<String, Object>(); if (!(e instanceof PlayException)) { e = new play.exceptions.UnexpectedException(e); } // Flush some cookies try { Map<String, Http.Cookie> cookies = Response.current().cookies; for (Http.Cookie cookie : cookies.values()) { if (cookie.sendOnError) { Cookie c = new Cookie(cookie.name, cookie.value); c.setSecure(cookie.secure); c.setPath(cookie.path); if (cookie.domain != null) { c.setDomain(cookie.domain); } response.addCookie(c); } } } catch (Exception exx) { // humm ? } binding.put("exception", e); binding.put("session", Scope.Session.current()); binding.put("request", Http.Request.current()); binding.put("flash", Scope.Flash.current()); binding.put("params", Scope.Params.current()); binding.put("play", new Play()); try { binding.put("errors", Validation.errors()); } catch (Exception ex) { // } response.setStatus(500); String format = "html"; if (Request.current() != null) { format = Request.current().format; } // Do we have an ajax request? If we have then we want to display some text even if it is html // that is requested if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With")) && (format == null || format.equals("html"))) { format = "txt"; } if (format == null) { format = "txt"; } response.setContentType(MimeTypes.getContentType("500." + format, "text/plain")); try { String errorHtml = TemplateLoader.load("errors/500." + format).render(binding); response.getOutputStream().write(errorHtml.getBytes(Response.current().encoding)); Logger.error(e, "Internal Server Error (500)"); } catch (Throwable ex) { Logger.error(e, "Internal Server Error (500)"); Logger.error(ex, "Error during the 500 response generation"); throw ex; } } catch (Throwable exxx) { if (exxx instanceof RuntimeException) { throw (RuntimeException) exxx; } throw new RuntimeException(exxx); } }
public static void invoke(Http.Request request, Http.Response response) { Monitor monitor = null; try { resolve(request, response); Method actionMethod = request.invokedMethod; // 1. Prepare request params Scope.Params.current().__mergeWith(request.routeArgs); // add parameters from the URI query string String encoding = Http.Request.current().encoding; Scope.Params.current() ._mergeWith( UrlEncodedParser.parseQueryString( new ByteArrayInputStream(request.querystring.getBytes(encoding)))); // 2. Easy debugging ... if (Play.mode == Play.Mode.DEV) { Class<Controller> cclass = Controller.class; cclass.getDeclaredField("params").set(null, Scope.Params.current()); cclass.getDeclaredField("request").set(null, Http.Request.current()); cclass.getDeclaredField("response").set(null, Http.Response.current()); cclass.getDeclaredField("session").set(null, Scope.Session.current()); cclass.getDeclaredField("flash").set(null, Scope.Flash.current()); cclass.getDeclaredField("renderArgs").set(null, Scope.RenderArgs.current()); cclass.getDeclaredField("routeArgs").set(null, Scope.RouteArgs.current()); cclass.getDeclaredField("validation").set(null, Validation.current()); } ControllerInstrumentation.stopActionCall(); Play.pluginCollection.beforeActionInvocation(actionMethod); // Monitoring monitor = MonitorFactory.start(request.action + "()"); // 3. Invoke the action try { // @Before handleBefores(request); // Action Result actionResult = null; String cacheKey = null; // Check the cache (only for GET or HEAD) if ((request.method.equals("GET") || request.method.equals("HEAD")) && actionMethod.isAnnotationPresent(CacheFor.class)) { cacheKey = actionMethod.getAnnotation(CacheFor.class).id(); if ("".equals(cacheKey)) { cacheKey = "urlcache:" + request.url + request.querystring; } actionResult = (Result) play.cache.Cache.get(cacheKey); } if (actionResult == null) { ControllerInstrumentation.initActionCall(); try { inferResult(invokeControllerMethod(actionMethod)); } catch (Result result) { actionResult = result; // Cache it if needed if (cacheKey != null) { play.cache.Cache.set( cacheKey, actionResult, actionMethod.getAnnotation(CacheFor.class).value()); } } catch (InvocationTargetException ex) { // It's a Result ? (expected) if (ex.getTargetException() instanceof Result) { actionResult = (Result) ex.getTargetException(); // Cache it if needed if (cacheKey != null) { play.cache.Cache.set( cacheKey, actionResult, actionMethod.getAnnotation(CacheFor.class).value()); } } else { // @Catch Object[] args = new Object[] {ex.getTargetException()}; List<Method> catches = Java.findAllAnnotatedMethods(Controller.getControllerClass(), Catch.class); ControllerInstrumentation.stopActionCall(); for (Method mCatch : catches) { Class[] exceptions = mCatch.getAnnotation(Catch.class).value(); if (exceptions.length == 0) { exceptions = new Class[] {Exception.class}; } for (Class exception : exceptions) { if (exception.isInstance(args[0])) { mCatch.setAccessible(true); inferResult(invokeControllerMethod(mCatch, args)); break; } } } throw ex; } } } // @After handleAfters(request); monitor.stop(); monitor = null; // OK, re-throw the original action result if (actionResult != null) { throw actionResult; } throw new NoResult(); } catch (IllegalAccessException ex) { throw ex; } catch (IllegalArgumentException ex) { throw ex; } catch (InvocationTargetException ex) { // It's a Result ? (expected) if (ex.getTargetException() instanceof Result) { throw (Result) ex.getTargetException(); } // Re-throw the enclosed exception if (ex.getTargetException() instanceof PlayException) { throw (PlayException) ex.getTargetException(); } StackTraceElement element = PlayException.getInterestingStackTraceElement(ex.getTargetException()); if (element != null) { throw new JavaExecutionException( Play.classes.getApplicationClass(element.getClassName()), element.getLineNumber(), ex.getTargetException()); } throw new JavaExecutionException(Http.Request.current().action, ex); } } catch (Result result) { Play.pluginCollection.onActionInvocationResult(result); // OK there is a result to apply // Save session & flash scope now Scope.Session.current().save(); Scope.Flash.current().save(); result.apply(request, response); Play.pluginCollection.afterActionInvocation(); // @Finally handleFinallies(request, null); } catch (PlayException e) { handleFinallies(request, e); throw e; } catch (Throwable e) { handleFinallies(request, e); throw new UnexpectedException(e); } finally { Play.pluginCollection.onActionInvocationFinally(); if (monitor != null) { monitor.stop(); } } }