Return pdf file
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package ch.dlmw.swisssignchallenge.controllers;
|
||||
|
||||
import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument;
|
||||
import ch.dlmw.swisssignchallenge.services.PdfService;
|
||||
import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@ -9,6 +10,7 @@ import com.github.fge.jsonpatch.JsonPatchException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.openapitools.api.SigningRequestDocumentApi;
|
||||
import org.openapitools.model.PatchOperation;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@ -19,6 +21,7 @@ import java.util.List;
|
||||
@AllArgsConstructor
|
||||
public class SigningRequestDocumentController implements SigningRequestDocumentApi {
|
||||
private final SigningRequestDocumentService signingRequestDocumentService;
|
||||
private final PdfService pdfService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
@ -38,6 +41,13 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Resource> getSigningRequestDocumentData(String id) {
|
||||
var document = signingRequestDocumentService.getSigningRequestDocument(id);
|
||||
|
||||
return ResponseEntity.ok(pdfService.getPdf(document.getName()));
|
||||
}
|
||||
|
||||
private <T> T applyPatch(List<PatchOperation> patchOperations, T object, Class<T> entityType) throws JsonPatchException, IOException {
|
||||
JsonNode patchNode = objectMapper.valueToTree(patchOperations);
|
||||
JsonPatch jsonPatch = JsonPatch.fromJson(patchNode);
|
||||
|
@ -0,0 +1,7 @@
|
||||
package ch.dlmw.swisssignchallenge.services;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
public interface PdfService {
|
||||
Resource getPdf(String name);
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package ch.dlmw.swisssignchallenge.services.impl;
|
||||
|
||||
import ch.dlmw.swisssignchallenge.services.PdfService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class PdfServiceImpl implements PdfService {
|
||||
private static final String FILE_DIRECTORY = "/srv/pdfs/";
|
||||
|
||||
@Override
|
||||
public Resource getPdf(String name) {
|
||||
if (name.contains("..")) { // this is to avoid traversal attacks
|
||||
throw new IllegalArgumentException("Invalid file name: " + name);
|
||||
}
|
||||
|
||||
Path filePath = Paths.get(FILE_DIRECTORY).resolve(name).normalize();
|
||||
Resource resource;
|
||||
try {
|
||||
resource = new UrlResource(filePath.toUri());
|
||||
} catch (MalformedURLException e) {
|
||||
throw new IllegalArgumentException("Invalid file name: " + name);
|
||||
}
|
||||
if (!resource.exists() || !resource.isReadable()) {
|
||||
throw new NoSuchElementException("Couldn't find file: " + name);
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user