/** * Specify Response cache controls. * * @param routeContext */ protected void specifyCacheControls(RouteContext routeContext) { if (isNoCache) { log.debug("NoCache detected, response may not be cached"); routeContext.getResponse().noCache(); } }
public static Response get() { RouteContext routeContext = RouteDispatcher.getRouteContext(); return (routeContext != null) ? routeContext.getResponse() : null; }
public Response getResponse() { return getRouteContext().getResponse(); }
public Response getResponse() { return getRouteContext().getResponse(); }
/** * Specify the Response content-type by... * <ol> * <li>setting the first Produces content type</li> * <li>negotiating with the Request if multiple content-types are specified in Produces</li> * </ol> * * @param routeContext */ protected void specifyContentType(RouteContext routeContext) { if (!declaredProduces.isEmpty()) { // Specify first Produces content-type String defaultContentType = declaredProduces.get(0); routeContext.getResponse().contentType(defaultContentType); if (declaredProduces.size() > 1) { // negotiate content-type from Request Accept/Content-Type routeContext.negotiateContentType(); } } }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); Map<String, String> props = new HashMap<>(); for (String name : System.getProperties().stringPropertyNames()) { props.put(name, System.getProperty(name)); } try (BufferedWriter writer = new BufferedWriter(response.getWriter())) { writeSystemProperties(props, writer); writer.newLine(); writeEnv(System.getenv(), writer); writer.flush(); } catch (IOException e) { log.error(e.getMessage(), e); } }
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { // stream the resource log.debug("Streaming as resource '{}'", resourceUrl); routeContext.getResponse().contentType(mimeType); routeContext.getResponse().ok().resource(resourceUrl.openStream()); } else { // stream the file log.debug("Streaming as file '{}'", resourceUrl); routeContext.getResponse().ok().file(filename, resourceUrl.openStream()); } }
protected void handleDirectoryRequest(RouteContext routeContext, File dir) throws MalformedURLException { File index = getIndexFile(dir); if (index != null) { routeContext.getResponse().contentLength(index.length()); URL url = index.toURI().toURL(); streamResource(url, routeContext); return; } sendDirectoryListing(routeContext, dir); }
@Override public void handle(RouteContext routeContext) { Map<String, String> settingsMap = settingsToMap(routeContext.getSettings()); Response response = routeContext.getResponse().noCache().text(); try (BufferedWriter writer = new BufferedWriter(response.getWriter())) { writeSettings(settingsMap, writer); writer.flush(); } catch (IOException e) { log.error(e.getMessage(), e); } }
protected void streamResource(URL resourceUrl, RouteContext routeContext) { try { long lastModified = resourceUrl.openConnection().getLastModified(); routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified); if (routeContext.getResponse().getStatus() == HttpConstants.StatusCode.NOT_MODIFIED) { // do not stream anything out, simply return 304 routeContext.getResponse().commit(); } else { sendResource(resourceUrl, routeContext); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { // stream the resource log.debug("Streaming as resource '{}'", resourceUrl); routeContext.getResponse().ok().chunked(chunked).resource(resourceUrl.openStream()); } else { // stream the file log.debug("Streaming as file '{}'", resourceUrl); routeContext.getResponse().ok().chunked(chunked).file(filename, resourceUrl.openStream()); } }
@Override public void handle(RouteContext routeContext) { routeContext.getResponse().noCache().text().send("pong"); }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text();
/** * Sets the application language cookie. * <p> * If the language does not match a registered language or language * component an exception is thrown. * </p> * * @param language * @param routeContext * @throws PippoRuntimeException */ public void setLanguageCookie(String language, RouteContext routeContext) { String lang = getLanguageOrDefault(language); if (lang.equals(language)) { Cookie cookie = generateLanguageCookie(language); routeContext.getResponse().cookie(cookie); } else { throw new PippoRuntimeException("'{}' is not a registered language!", language); } }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); SortedMap<String, HealthCheck.Result> healthChecks = healthCheckRegistry.runHealthChecks(); if (healthChecks.isEmpty()) { response.notImplemented().send("The health checks are empty"); } else { boolean notHealthy = healthChecks.values().stream().anyMatch(hc -> !hc.isHealthy()); if (notHealthy) { response.internalError().send("The health is bad"); } else { response.ok().send("The health is good"); } } }
protected void setResponseHeaders(URL resourceUrl, RouteContext routeContext) { try { long lastModified = resourceUrl.openConnection().getLastModified(); routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified); String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { routeContext.getResponse().contentType(mimeType); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
protected void streamResource(URL resourceUrl, RouteContext routeContext) { try { setResponseHeaders(resourceUrl, routeContext); if (routeContext.getResponse().getStatus() == HttpConstants.StatusCode.NOT_MODIFIED) { // do not stream anything out, simply return 304 routeContext.getResponse().commit(); } else { sendResource(resourceUrl, routeContext); } } catch (IOException e) { String message = e.getMessage(); if (!StringUtils.isNullOrEmpty(message)) { log.warn("Error sending resource {} to {}: {}", resourceUrl, routeContext.getRequest().getClientIp(), message); } else { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
protected void processRouteInterceptors(RouteContext routeContext) { if (interceptors.isEmpty()) { return; } List<RouteMatch> chain = new ArrayList<>(); for (RouteHandler interceptor : interceptors) { // create a route for interceptor Route route = new Route(routeContext.getRequestMethod(), routeContext.getRequestUri(), interceptor); route.setName(StringUtils.format("{}<{}>", Interceptor.class.getSimpleName(), route.getRouteHandler().getClass().getSimpleName())); route.bindAll(routeContext.getRoute().getAttributes()); // add route in chain RouteMatch match = new RouteMatch(route, null); chain.add(match); } // TODO DefaultRouteContext is hardcoded RouteContext context = new DefaultRouteContext(routeContext.getApplication(), routeContext.getRequest(), routeContext.getResponse(), chain); context.next(); }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); if (threadDump != null) { threadDump.dump(response.getOutputStream()); } else { response.internalError().send("Sorry your runtime environment does not allow to dump threads"); } }
@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(); }