Commit b3ffbc44 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Improve caching

Replaced unnecessary ETAG with if_modified_since

Added cache improvements for RepositoryControllers (v0, v1), Explorer controller
Code cleanup in DescriptorListController

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