private void checkForRecursion(RouteContext routeContext) { Integer depth = routeContext.removeLocal("__errorHandlerDepth"); if (depth == null) { depth = 0; } depth += 1; routeContext.setLocal("__errorHandlerDepth", depth); if (depth > 4) { throw new PippoRuntimeException("Recursion in error handler"); } }
@Override public void handle(Exception exception, RouteContext routeContext) { StatusCodeException statusCodeException = (StatusCodeException) exception; routeContext.setLocal("message", statusCodeException.getMessage()); errorHandler.handle(statusCodeException.getStatusCode(), routeContext); } }
private void sendDirectoryListing(RouteContext routeContext, File dir) { String absoluteDirUri = getUrlPath() + StringUtils.addStart(directory.toPath().relativize(dir.toPath()).toString(), "/"); if (StringUtils.isNullOrEmpty(directoryTemplate)) { // Generate primitive, default directory listing String page = generateDefaultDirectoryListing(routeContext, dir, absoluteDirUri); routeContext.html().send(page); } else { // Render directory listing template int numFiles = 0; int numDirs = 0; long diskUsage = 0; List<DirEntry> dirEntries = getDirEntries(routeContext, dir, absoluteDirUri); for (DirEntry dirEntry : dirEntries) { if (dirEntry.isFile()) { numFiles++; diskUsage += dirEntry.getSize(); } else if (dirEntry.isDirectory() && !dirEntry.getName().contains("..")) { numDirs++; } } routeContext.setLocal("dirUrl", absoluteDirUri); routeContext.setLocal("dirPath", absoluteDirUri.substring(getUrlPath().length())); routeContext.setLocal("dirEntries", dirEntries); routeContext.setLocal("numDirs", numDirs); routeContext.setLocal("numFiles", numFiles); routeContext.setLocal("diskUsage", diskUsage); routeContext.render(directoryTemplate); } }
@Override public void handle(RouteContext routeContext) { String language = enableQueryParameter ? routeContext.getParameter(PippoConstants.REQUEST_PARAMETER_LANG).toString() : null; if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(routeContext); } Locale locale = languages.getLocaleOrDefault(language); routeContext.setLocal(PippoConstants.REQUEST_PARAMETER_LANG, language); routeContext.setLocal(PippoConstants.REQUEST_PARAMETER_LOCALE, locale); if (setCookie) { if (routeContext.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, routeContext); } } routeContext.next(); }
/** * Removes a Flash instance from the session, binds it to the RouteContext, * and creates a new Flash instance. * * @param routeContext */ private void processFlash(RouteContext routeContext) { Flash flash = null; if (routeContext.hasSession()) { // get flash from session flash = routeContext.removeSession("flash"); // put an empty flash (outgoing flash) in session; defense against session.get("flash") routeContext.setSession("flash", new Flash()); } if (flash == null) { flash = new Flash(); } // make current flash available to templates routeContext.setLocal("flash", flash); }
@Override public void init() throws Exception { setSecurityManager(env.getWebSecurityManager()); FilterChainResolver resolver = env.getFilterChainResolver(); if (resolver != null) { setFilterChainResolver(resolver); } errorHandler.setExceptionHandler(AuthenticationException.class, (exception, ctx) -> { ctx.setLocal("message", exception.getMessage()); errorHandler.handle(401, ctx); }); errorHandler.setExceptionHandler(AuthorizationException.class, (exception, ctx) -> { ctx.setLocal("message", exception.getMessage()); errorHandler.handle(403, ctx); }); }
String swaggerTemplate = settings.getString("swagger.ui.template", "swagger/index"); GET(swaggerPath, (ctx) -> { ctx.setLocal("apiTitle", settings.getString("swagger.api.title", settings.getApplicationName())); ctx.setLocal("bannerText", settings.getString("swagger.ui.bannerText", "swagger")); ctx.setLocal("swaggerPath", StringUtils.removeStart(swaggerPath, "/")); ctx.setLocal("hideApiKey", hideApiKey); ctx.setLocal("apiKeyName", apiKeyName); ctx.setLocal("apiKeyType", apiKeyType); Account local = ctx.getLocal(AuthConstants.ACCOUNT_ATTRIBUTE); Account account = Optional.fromNullable(session).or(Optional.fromNullable(local).or(Account.GUEST)); ctx.setLocal("apiKey", Optional.fromNullable(account.getToken()).or(""));
String swaggerTemplate = settings.getString("swagger.ui.template", "swagger/index"); GET(swaggerPath, (ctx) -> { ctx.setLocal("apiTitle", settings.getString("swagger.api.title", settings.getApplicationName())); ctx.setLocal("bannerText", settings.getString("swagger.ui.bannerText", "swagger")); ctx.setLocal("swaggerPath", StringUtils.removeStart(swaggerPath, "/")); ctx.setLocal("hideApiKey", hideApiKey); ctx.setLocal("apiKeyName", apiKeyName); ctx.setLocal("apiKeyType", apiKeyType); Account local = ctx.getLocal(AuthConstants.ACCOUNT_ATTRIBUTE); Account account = Optional.fromNullable(session).or(Optional.fromNullable(local).or(Account.GUEST)); ctx.setLocal("apiKey", Optional.fromNullable(account.getToken()).or(""));
private void initializeApplication() { // hook-up the Metrics dispatch listener MetricsDispatchListener metricsDispatchListener = new MetricsDispatchListener(metricRegistry); application.getRoutePreDispatchListeners().add(metricsDispatchListener); application.getRoutePostDispatchListeners().add(metricsDispatchListener); // set the RedirectException handler application.getErrorHandler().setExceptionHandler(RedirectException.class, (exception, ctx) -> { RedirectException redirectException = (RedirectException) exception; if (!Strings.isNullOrEmpty(redirectException.getSessionUrlAttribute())) { String requestUri = ctx.getRequest().getApplicationUriWithQuery(); ctx.setSession(redirectException.getSessionUrlAttribute(), requestUri); } ctx.redirect(redirectException.getPath()); }); // set the StatusCodeException handler application.getErrorHandler().setExceptionHandler(StatusCodeException.class, (exception, ctx) -> { StatusCodeException statusCodeException = (StatusCodeException) exception; ctx.setLocal("message", statusCodeException.getMessage()); application.getErrorHandler().handle(statusCodeException.getStatusCode(), ctx); }); }
private void initializeApplication() { // hook-up the Metrics dispatch listener MetricsDispatchListener metricsDispatchListener = new MetricsDispatchListener(metricRegistry); application.getRoutePreDispatchListeners().add(metricsDispatchListener); application.getRoutePostDispatchListeners().add(metricsDispatchListener); // set the RedirectException handler application.getErrorHandler().setExceptionHandler(RedirectException.class, (exception, ctx) -> { RedirectException redirectException = (RedirectException) exception; if (!Strings.isNullOrEmpty(redirectException.getSessionUrlAttribute())) { String requestUri = ctx.getRequest().getApplicationUriWithQuery(); ctx.setSession(redirectException.getSessionUrlAttribute(), requestUri); } ctx.redirect(redirectException.getPath()); }); // set the StatusCodeException handler application.getErrorHandler().setExceptionHandler(StatusCodeException.class, (exception, ctx) -> { StatusCodeException statusCodeException = (StatusCodeException) exception; ctx.setLocal("message", statusCodeException.getMessage()); application.getErrorHandler().handle(statusCodeException.getStatusCode(), ctx); }); }