@Override public Object wrap(final Req req, final HandlerInvocation invocation) throws Exception { TokenAuthData auth = HttpUtils.getAuth(req); String username = auth != null ? auth.user : null; if (U.isEmpty(username)) { HttpUtils.clearUserData(req); } Set<String> roles = userRoles(req, username); Set<String> scope = auth != null ? auth.scope : null; if (U.notEmpty(requiredRoles) && !Secure.hasAnyRole(username, roles, requiredRoles)) { throw new SecurityException("The user doesn't have the required roles!"); } Ctx ctx = Ctxs.required(); ctx.setUser(new UserInfo(username, roles, scope)); return invocation.invoke(); }
@Override public Object wrap(final Req req, final HandlerInvocation invocation) { final AtomicReference<Object> resultHolder = new AtomicReference<>(); U.must(txMode != null && txMode != TransactionMode.NONE); boolean readOnly = (txMode == TransactionMode.AUTO) ? HttpUtils.isGetReq(req) : txMode == TransactionMode.READ_ONLY; try { JPA.transaction(() -> { Object res = invocation.invokeAndTransformResultCatchingErrors(result -> { if (result instanceof Throwable) { return result; } // serialize the result into a HTTP response body, while still inside tx (see #153) RespBody body = BodyRenderer.resultToRespBody(req.response(), result); return body; }); if (res instanceof Throwable) { // throw to rollback Throwable err = (Throwable) res; throw U.rte("Error occurred inside the transactional web handler!", err); } else { resultHolder.set(res); } }, readOnly); } catch (Throwable e) { resultHolder.set(e); } return resultHolder.get(); }
@Override public Object wrap(final Req req, final HandlerInvocation invocation) throws Exception { TokenAuthData auth = HttpUtils.getAuth(req); String username = auth != null ? auth.user : null; if (U.isEmpty(username)) { HttpUtils.clearUserData(req); } Set<String> roles = userRoles(req, username); Set<String> scope = auth != null ? auth.scope : null; if (U.notEmpty(requiredRoles) && !Secure.hasAnyRole(username, roles, requiredRoles)) { throw new SecurityException("The user doesn't have the required roles!"); } Ctx ctx = Ctxs.required(); ctx.setUser(new UserInfo(username, roles, scope)); return invocation.invoke(); }
@Override public void run() { Object res = invocation.invokeAndTransformResultCatchingErrors(new Mapper<Object, Object>() { @Override public Object map(Object result) { if (result instanceof Throwable) { return result; } // serialize the result into a HTTP response body, while still inside tx (see #153) RespBody body = BodyRenderer.resultToRespBody(req.response(), result); return body; } }); if (res instanceof Throwable) { // throw to rollback Throwable err = (Throwable) res; throw U.rte("Error occurred inside the transactional web handler!", err); } else { resultHolder.set(res); } } }, readOnly);