Commit 22cbe2b6 authored by Matija Obreza's avatar Matija Obreza

Fix: Repository downloads cache headers

- Use actual image update date for Last-Modified header
- Use sha1 sum as ETag
parent f99620f6
......@@ -29,6 +29,7 @@ import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.BytesStorageService;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.spring.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -67,20 +68,42 @@ public class RepositoryDownloadController {
}
data = this.byteStorageService.get(Paths.get(path, filename));
if (data == null) {
throw new NotFoundElement("No thumbnail at " + Paths.get(path, filename));
}
try {
final RepositoryFile repositoryFile = this.repositoryService.getFile(UUID.fromString(uuid));
// TODO check Request Cache headers (Modified-Since, ETag)
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, repositoryFile.getSha1Sum());
} catch (NoSuchRepositoryFileException e) {
throw new NotFoundElement("No file for thumb " + uuid);
}
response.setContentType("image/png");
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
} else {
// Regular repo file
// Regular repository file
try {
final RepositoryFile repositoryFile = this.repositoryService.getFile(UUID.fromString(uuid));
sanityCheck(Paths.get(path), ext, repositoryFile);
// TODO check Request Cache headers (Modified-Since, ETag)
data = this.repositoryService.getFileBytes(repositoryFile);
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
response.setHeader(HttpHeaders.PRAGMA, "");
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, repositoryFile.getSha1Sum());
response.setContentType(repositoryFile.getContentType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", repositoryFile.getOriginalFilename()));
......@@ -96,8 +119,11 @@ public class RepositoryDownloadController {
if (data != null) {
response.setContentLength(data.length);
response.getOutputStream().write(data);
response.flushBuffer();
} else {
throw new NotFoundElement("No such thing. Sorry");
}
response.flushBuffer();
}
private void sanityCheck(final Path path, final String ext, final RepositoryFile repositoryFile) {
......
......@@ -29,6 +29,7 @@ import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.BytesStorageService;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.spring.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -62,7 +63,25 @@ public class RepositoryDownloadController extends BaseController {
}
data = this.byteStorageService.get(Paths.get(path, filename));
if (data == null) {
throw new NotFoundElement("No thumbnail at " + Paths.get(path, filename));
}
try {
final RepositoryFile repositoryFile = this.repositoryService.getFile(UUID.fromString(uuid));
// TODO check Request Cache headers (Modified-Since, ETag)
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, repositoryFile.getSha1Sum());
} catch (NoSuchRepositoryFileException e) {
throw new NotFoundElement("No file for thumb " + uuid);
}
response.setContentType("image/png");
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
} else {
// Regular repo file
......@@ -70,12 +89,16 @@ public class RepositoryDownloadController extends BaseController {
final RepositoryFile repositoryFile = this.repositoryService.getFile(UUID.fromString(uuid));
sanityCheck(Paths.get(path), ext, repositoryFile);
// TODO check Request Cache headers (Modified-Since, ETag)
data = this.repositoryService.getFileBytes(repositoryFile);
// Cache for 24hrs
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
response.setHeader(HttpHeaders.PRAGMA, "");
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, repositoryFile.getSha1Sum());
response.setContentType(repositoryFile.getContentType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", repositoryFile.getOriginalFilename()));
......
......@@ -27,6 +27,7 @@ import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.firewall.RequestRejectedException;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
......@@ -75,7 +76,7 @@ public class UserControllerAdvice extends BaseController {
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = { MaxPageLimitException.class })
@ExceptionHandler(value = { MaxPageLimitException.class, RequestRejectedException.class })
public ModelAndView handleMaxPageLimitException(final MaxPageLimitException e) {
LOG.error(e.getMessage());
final ModelAndView mav = new ModelAndView("/errors/error");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment