From a438587d83f7420b6bc9ddef2663014dc48313d9 Mon Sep 17 00:00:00 2001 From: Sonia Zorba Date: Fri, 12 Feb 2021 15:20:42 +0100 Subject: [PATCH] Added URL encoding for special chars --- .../transfer/controller/FileController.java | 21 +++++++++++++++++++ .../controller/GetFileController.java | 11 +++++----- .../controller/PutFileController.java | 16 +++++++------- src/main/resources/application.properties | 2 ++ 4 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 src/main/java/it/inaf/ia2/transfer/controller/FileController.java diff --git a/src/main/java/it/inaf/ia2/transfer/controller/FileController.java b/src/main/java/it/inaf/ia2/transfer/controller/FileController.java new file mode 100644 index 0000000..f8baf55 --- /dev/null +++ b/src/main/java/it/inaf/ia2/transfer/controller/FileController.java @@ -0,0 +1,21 @@ +package it.inaf.ia2.transfer.controller; + +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.stream.Collectors; +import javax.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; + +public abstract class FileController { + + @Autowired + protected HttpServletRequest request; + + public String getPath() { + String[] parts = request.getRequestURI().split("/"); + return String.join("/", Arrays.stream(parts) + .map(p -> URLDecoder.decode(p, StandardCharsets.UTF_8)) + .collect(Collectors.toList())); + } +} diff --git a/src/main/java/it/inaf/ia2/transfer/controller/GetFileController.java b/src/main/java/it/inaf/ia2/transfer/controller/GetFileController.java index b4a3a16..cbc52d7 100644 --- a/src/main/java/it/inaf/ia2/transfer/controller/GetFileController.java +++ b/src/main/java/it/inaf/ia2/transfer/controller/GetFileController.java @@ -10,7 +10,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UncheckedIOException; import java.util.Optional; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,7 +22,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController -public class GetFileController { +public class GetFileController extends FileController { private static final Logger LOG = LoggerFactory.getLogger(GetFileController.class); @@ -33,16 +32,16 @@ public class GetFileController { @Autowired private GmsClient gmsClient; - @Autowired - private HttpServletRequest request; - @Autowired private HttpServletResponse response; @GetMapping("/**") public ResponseEntity getFile() { - String path = request.getServletPath(); + String path = getPath(); + + LOG.debug("getFile called for path {}", path); + Optional optFileInfo = fileDAO.getFileInfo(path); if (optFileInfo.isPresent()) { diff --git a/src/main/java/it/inaf/ia2/transfer/controller/PutFileController.java b/src/main/java/it/inaf/ia2/transfer/controller/PutFileController.java index 4c9c724..98fedc8 100644 --- a/src/main/java/it/inaf/ia2/transfer/controller/PutFileController.java +++ b/src/main/java/it/inaf/ia2/transfer/controller/PutFileController.java @@ -11,7 +11,8 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import javax.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import static org.springframework.http.HttpStatus.NOT_FOUND; import org.springframework.http.ResponseEntity; @@ -21,21 +22,22 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController -public class PutFileController { +public class PutFileController extends FileController { + private static final Logger LOG = LoggerFactory.getLogger(PutFileController.class); + @Autowired private FileDAO fileDAO; @Autowired private ListOfFilesDAO listOfFilesDAO; - @Autowired - private HttpServletRequest request; - @PutMapping("/**") public ResponseEntity putFile(@RequestParam("file") MultipartFile file) throws IOException { - - String path = request.getServletPath(); + + String path = getPath(); + LOG.debug("putFile called for path {}", path); + Optional optFileInfo = fileDAO.getFileInfo(path); if (optFileInfo.isPresent()) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9e5a988..5379521 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -14,3 +14,5 @@ cors.allowed.origin=http://localhost:8080,http://localhost:8085 spring.servlet.multipart.max-file-size=10GB spring.servlet.multipart.max-request-size=10GB + +logging.level.it.inaf=TRACE -- GitLab