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); } }
public Application(PippoSettings settings) { this.pippoSettings = settings; this.languages = new Languages(settings); this.messages = new Messages(languages); this.mimeTypes = new MimeTypes(settings); this.httpCacheToolkit = new HttpCacheToolkit(settings); this.engines = new ContentTypeEngines(); this.initializers = new ArrayList<>(); this.webSocketRouter = new WebSocketRouter(); registerContentTypeEngine(TextPlainEngine.class); }
public void addEtag(RouteContext routeContext, long lastModified) { if (pippoSettings.isProd()) { String maxAge = pippoSettings.getString(PippoConstants.SETTING_HTTP_CACHE_CONTROL, "3600"); if (maxAge.equals("0")) { routeContext.setHeader(HttpConstants.Header.CACHE_CONTROL, "no-cache"); } else { routeContext.setHeader(HttpConstants.Header.CACHE_CONTROL, "max-age=" + maxAge); } } else { routeContext.setHeader(HttpConstants.Header.CACHE_CONTROL, "no-cache"); } // Use etag on demand: String etag = null; boolean useEtag = pippoSettings.getBoolean(PippoConstants.SETTING_HTTP_USE_ETAG, true); if (useEtag) { // ETag right now is only lastModified long. // maybe we change that in the future. etag = "\"" + lastModified + "\""; routeContext.setHeader(HttpConstants.Header.ETAG, etag); } if (isModified(etag, lastModified, routeContext)) { routeContext.setHeader(HttpConstants.Header.LAST_MODIFIED, DateUtils.formatForHttpHeader(lastModified)); } else if (routeContext.isRequestMethod(HttpConstants.Method.GET)) { routeContext.status(HttpConstants.StatusCode.NOT_MODIFIED); } }
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 serveSpecification(RouteContext ctx, String specificationName) { String extension = Files.getFileExtension(specificationName); if (extension.isEmpty()) { // default to json specificationName += ".json"; } final String finalDocumentName = specificationName; final String finalExtension = Files.getFileExtension(finalDocumentName); String document = specifications.get(finalDocumentName.toLowerCase()); if (document == null) { ctx.getResponse().notFound(); } else { ctx.getResponse().ok(); httpCacheToolkit.addEtag(ctx, startTime); if ("json".equals(finalExtension)) { ctx.json(); } else if ("yaml".equals(finalExtension)) { ctx.yaml(); } else { ctx.text(); } if (HttpMethod.GET.equals(ctx.getRequestMethod())) { ctx.getResponse().send(document); } } }
protected void serveSpecification(RouteContext ctx, String specificationName) { String extension = Files.getFileExtension(specificationName); if (extension.isEmpty()) { // default to json specificationName += ".json"; } final String finalDocumentName = specificationName; final String finalExtension = Files.getFileExtension(finalDocumentName); String document = specifications.get(finalDocumentName.toLowerCase()); if (document == null) { ctx.getResponse().notFound(); } else { ctx.getResponse().ok(); httpCacheToolkit.addEtag(ctx, startTime); if ("json".equals(finalExtension)) { ctx.json(); } else if ("yaml".equals(finalExtension)) { ctx.yaml(); } else { ctx.text(); } if (HttpMethod.GET.equals(ctx.getRequestMethod())) { ctx.getResponse().send(document); } } }