@Override public final void handleResource(String resourcePath, RouteContext routeContext) { URL url = getResourceUrl(resourcePath); if (url != null) { streamResource(url, routeContext); } }
@Override public String injectVersion(String resourcePath) { String version = getResourceVersion(resourcePath); if (StringUtils.isNullOrEmpty(version)) { // unversioned, pass-through resource path return resourcePath; } // check for extension int extensionAt = resourcePath.lastIndexOf('.'); StringBuilder versionedResourcePath = new StringBuilder(); if (extensionAt == -1) { versionedResourcePath.append(resourcePath); versionedResourcePath.append("-ver-").append(version); } else { versionedResourcePath.append(resourcePath.substring(0, extensionAt)); versionedResourcePath.append("-ver-").append(version); versionedResourcePath.append(resourcePath.substring(extensionAt, resourcePath.length())); } log.trace("Inject version in resource path: '{}' => '{}'", resourcePath, versionedResourcePath); return versionedResourcePath.toString(); }
protected RouteRegistration GET(UrlResourceHandler resourceHandler) { return registerRoute(resourceHandler.getUriPattern(), HttpMethod.GET, resourceHandler); }
protected String getResourceVersion(String resourcePath) { String version = null; URL resourceUrl = getResourceUrl(resourcePath); if (resourceUrl != null) { try { version = Long.toString(resourceUrl.openConnection().getLastModified()); } catch (NullPointerException | IOException e) { log.error("Failed to read lastModified property for {}", resourceUrl, e); } } return version; }
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 RouteRegistration GET(UrlResourceHandler resourceHandler) { return registerRoute(resourceHandler.getUriPattern(), HttpMethod.GET, resourceHandler); }