Commit ae831885 authored by Matija Obreza's avatar Matija Obreza

Merge branch '430-improve-caching'

* 430-improve-caching:
  Improve caching
parents c49a8704 b3ffbc44
......@@ -18,6 +18,7 @@ package org.genesys2.server.api.v0;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.genesys.filerepository.NoSuchRepositoryFileException;
......@@ -27,6 +28,7 @@ import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -111,12 +113,27 @@ public class RepositoryController {
* @throws IOException Signals that an I/O exception has occurred.
*/
@GetMapping(value = "/download/{fileUuid:\\w{8}\\-\\w{4}.+}")
public void downloadFile(@PathVariable("fileUuid") final UUID fileUuid, final HttpServletResponse response) throws NoSuchRepositoryFileException, IOException {
public void downloadFile(@PathVariable("fileUuid") final UUID fileUuid, final HttpServletRequest request, final HttpServletResponse response) throws NoSuchRepositoryFileException, IOException {
final RepositoryFile repositoryFile = repositoryService.getFile(fileUuid);
String eTag = repositoryFile.getSha1Sum();
if (eTag.equals(request.getHeader(HttpHeaders.IF_NONE_MATCH))) {
response.setStatus(HttpStatus.NOT_MODIFIED.value());
response.flushBuffer();
return;
}
long sinceDate = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (sinceDate >= -1 && repositoryFile.getLastModifiedDate().getTime() < sinceDate) {
response.setStatus(HttpStatus.NOT_MODIFIED.value());
response.flushBuffer();
return;
}
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, eTag);
response.setContentType(repositoryFile.getContentType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", repositoryFile.getOriginalFilename()));
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.api.v1;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.EOFException;
import java.io.IOException;
......@@ -50,6 +51,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
......@@ -131,11 +133,27 @@ public class DescriptorListController {
*/
@Transactional(readOnly = true)
@GetMapping(value = "/{uuid}/html", produces = { "text/html" })
public void generateDocument(@PathVariable("uuid") final UUID uuid, final HttpServletResponse response) throws IOException {
public void generateDocument(@PathVariable("uuid") final UUID uuid, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
final DescriptorList descriptorList = descriptorListService.getDescriptorList(uuid);
final String descriptorListHtml = exporter.htmlDescriptorList(descriptorList);
String eTag = descriptorList.getUuid() + "-" + descriptorList.getVersion();
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
response.setDateHeader(HttpHeaders.LAST_MODIFIED, descriptorList.getLastModifiedDate().getTime());
response.setHeader(HttpHeaders.ETAG, eTag);
if (eTag.equals(request.getHeader(HttpHeaders.IF_NONE_MATCH))) {
response.setStatus(HttpStatus.NOT_MODIFIED.value());
response.flushBuffer();
return;
}
long sinceDate = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (sinceDate >= -1 && descriptorList.getLastModifiedDate().getTime() < sinceDate) {
response.setStatus(HttpStatus.NOT_MODIFIED.value());
response.flushBuffer();
return;
}
response.setHeader(HttpHeaders.PRAGMA, "");
response.setContentType("text/html");
// response.addHeader("Content-Disposition", String.format("attachment;
......
......@@ -45,6 +45,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
......@@ -203,12 +204,26 @@ public class RepositoryController {
* @throws IOException Signals that an I/O exception has occurred.
*/
@GetMapping(value = "/download/{fileUuid:\\w{8}\\-\\w{4}.+}")
public void downloadFile(@PathVariable("fileUuid") final UUID fileUuid, final HttpServletResponse response) throws NoSuchRepositoryFileException, IOException {
public void downloadFile(@PathVariable("fileUuid") final UUID fileUuid, final HttpServletRequest request, final HttpServletResponse response) throws NoSuchRepositoryFileException, IOException {
final RepositoryFile repositoryFile = repositoryService.getFile(fileUuid);
String eTag = repositoryFile.getSha1Sum();
if (eTag.equals(request.getHeader(HttpHeaders.IF_NONE_MATCH))) {
response.setStatus(HttpStatus.NOT_MODIFIED.value());
response.flushBuffer();
return;
}
long sinceDate = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (sinceDate >= -1 && repositoryFile.getLastModifiedDate().getTime() < sinceDate) {
response.setStatus(HttpStatus.NOT_MODIFIED.value());
response.flushBuffer();
return;
}
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, eTag);
response.setContentType(repositoryFile.getContentType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", repositoryFile.getOriginalFilename()));
......
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