public HttpStatus temporaryRedirect307() { return new HttpStatus(307, "The requested URL resides temporarily under a different URL"); } public HttpStatus permanentRedirect308() {
@Override public Object intercept(final ActionRequest actionRequest) throws Exception { final ActionRuntime actionRuntime = actionRequest.getActionRuntime(); if (actionRuntime.isAuthenticated()) { // action requires user to be authenticated T grantedAuthToken = authenticateUserViaHttpSession(actionRequest); if (grantedAuthToken == null) { grantedAuthToken = authenticateUserViaToken(actionRequest); } if (authenticateViaBasicAuth && grantedAuthToken == null) { grantedAuthToken = authenticateUserViaBasicAuth(actionRequest); } if (grantedAuthToken == null) { return JsonResult.of(return404instead401 ? HttpStatus.error404().notFound() : HttpStatus.error401().unauthorized("Not authorized")); } if (!authorized(actionRequest)) { return JsonResult.of(HttpStatus.error403().forbidden()); } } return actionRequest.invoke(); }
/** * Logout hook. */ protected JsonResult logout() { log.debug("logout user"); UserSession.stop(servletRequest, servletResponse); return JsonResult.of(HttpStatus.ok()); }
/** * Authenticate user and start user session. */ protected JsonResult login() { T authToken; authToken = loginViaBasicAuth(servletRequest); if (authToken == null) { authToken = loginViaRequestParams(servletRequest); } if (authToken == null) { log.warn("Login failed."); return JsonResult.of(HttpStatus.error401().unauthorized("Login failed.")); } log.info("login OK!"); final UserSession<T> userSession = new UserSession<>(authToken, userAuth.tokenValue(authToken)); userSession.start(servletRequest, servletResponse); // return token return tokenAsJson(authToken); }
public TextResult status(final HttpStatus httpStatus) { this.status = httpStatus.status(); return this; }
/** * Creates a JSON response from an exception. Response body will have information about the * exception and response status will be set to 500. */ public static JsonResult of(final Exception exception) { final HashMap<String, Object> errorMap = new HashMap<>(); errorMap.put("message", ExceptionUtil.message(exception)); errorMap.put("error", exception.getClass().getName()); errorMap.put("cause", exception.getCause() != null ? exception.getCause().getClass().getName() : null); final ArrayList<String> details = new ArrayList<>(); final StackTraceElement[] ste = ExceptionUtil.getStackTrace(exception, null, null); for (StackTraceElement stackTraceElement : ste) { details.add(stackTraceElement.toString()); } errorMap.put("details", details); final String json = JsonSerializer.create().deep(true).serialize(errorMap); return new JsonResult(json).status(HttpStatus.error500().internalError()); }
@Override public Object intercept(final ActionRequest actionRequest) { try { return actionRequest.invoke(); } catch (ResponseException rex) { return JsonResult.of(HttpStatus.of(rex.getStatus(), rex.getMessage())); } catch (Exception ex) { log.error("Action execution failed:", ex); return JsonResult.of(ex); } } }
@Override public Object intercept(final ActionRequest actionRequest) throws Exception { final ActionRuntime actionRuntime = actionRequest.getActionRuntime(); if (actionRuntime.isAuthenticated()) { // action requires user to be authenticated T grantedAuthToken = authenticateUserViaHttpSession(actionRequest); if (grantedAuthToken == null) { grantedAuthToken = authenticateUserViaToken(actionRequest); } if (authenticateViaBasicAuth && grantedAuthToken == null) { grantedAuthToken = authenticateUserViaBasicAuth(actionRequest); } if (grantedAuthToken == null) { return JsonResult.of(return404instead401 ? HttpStatus.error404().notFound() : HttpStatus.error401().unauthorized("Not authorized")); } if (!authorized(actionRequest)) { return JsonResult.of(HttpStatus.error403().forbidden()); } } return actionRequest.invoke(); }
/** * Logout hook. */ protected JsonResult logout() { log.debug("logout user"); UserSession.stop(servletRequest, servletResponse); return JsonResult.of(HttpStatus.ok()); }
/** * Authenticate user and start user session. */ protected JsonResult login() { T authToken; authToken = loginViaBasicAuth(servletRequest); if (authToken == null) { authToken = loginViaRequestParams(servletRequest); } if (authToken == null) { log.warn("Login failed."); return JsonResult.of(HttpStatus.error401().unauthorized("Login failed.")); } log.info("login OK!"); final UserSession<T> userSession = new UserSession<>(authToken, userAuth.tokenValue(authToken)); userSession.start(servletRequest, servletResponse); // return token return tokenAsJson(authToken); }
public HttpStatus movedPermanently301() { return new HttpStatus(301, "The requested resource has been assigned a new permanent URL."); } public HttpStatus movedTemporarily302() {
public HttpStatus notModified304() { return new HttpStatus(304, "The contents of the requested web page have not been modified since the last access."); } public HttpStatus temporaryRedirect307() {
public HttpStatus permanentRedirect308() { return new HttpStatus(308, "All future requests should be sent using a different URI."); } }
public static HttpStatus ok() { return new HttpStatus(200, "OK"); }
public HttpStatus multipleChoice300() { return new HttpStatus(300, "The requested resource points to a destination with multiple representations."); }
public HttpStatus movedTemporarily302() { return new HttpStatus(302, "The requested resource has been temporarily moved to a different URL."); } public HttpStatus notModified304() {
public static HttpStatus of(final int status, final String message) { return new HttpStatus(status, message); }
public HttpStatus permanentRedirect308() { return new HttpStatus(308, "All future requests should be sent using a different URI."); } }