List of usage examples for com.vaadin.server VaadinResponse setDateHeader
public void setDateHeader(String name, long timestamp);
From source file:com.haulmont.cuba.web.sys.CubaWebJarsHandler.java
License:Apache License
@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { String path = request.getPathInfo(); if (StringUtils.isEmpty(path) || StringUtils.isNotEmpty(path) && !path.startsWith(VAADIN_WEBJARS_PREFIX)) { return false; }//from w w w .ja va 2 s .c o m log.trace("WebJar resource requested: {}", path.replace(VAADIN_WEBJARS_PREFIX, "")); String errorMessage = checkResourcePath(path); if (StringUtils.isNotEmpty(errorMessage)) { log.warn(errorMessage); response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMessage); return false; } URL resourceUrl = getStaticResourceUrl(path); if (resourceUrl == null) { resourceUrl = getClassPathResourceUrl(path); } if (resourceUrl == null) { String msg = String.format("Requested WebJar resource is not found: %s", path); response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); log.warn(msg); return false; } String resourceName = getResourceName(path); String mimeType = servletContext.getMimeType(resourceName); response.setContentType(mimeType != null ? mimeType : FileTypesHelper.DEFAULT_MIME_TYPE); String cacheControl = "public, max-age=0, must-revalidate"; int resourceCacheTime = getCacheTime(resourceName); if (resourceCacheTime > 0) { cacheControl = "max-age=" + String.valueOf(resourceCacheTime); } response.setHeader("Cache-Control", cacheControl); response.setDateHeader("Expires", System.currentTimeMillis() + (resourceCacheTime * 1000)); InputStream inputStream = null; try { URLConnection connection = resourceUrl.openConnection(); long lastModifiedTime = connection.getLastModified(); // Remove milliseconds to avoid comparison problems (milliseconds // are not returned by the browser in the "If-Modified-Since" // header). lastModifiedTime = lastModifiedTime - lastModifiedTime % 1000; response.setDateHeader("Last-Modified", lastModifiedTime); if (browserHasNewestVersion(request, lastModifiedTime)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } inputStream = connection.getInputStream(); copy(inputStream, response.getOutputStream()); return true; } finally { if (inputStream != null) { inputStream.close(); } } }