|
| 1 | +package org.ladocuploader.app.interceptors; |
| 2 | + |
| 3 | +import formflow.library.data.SubmissionRepositoryService; |
| 4 | +import jakarta.servlet.http.HttpServletRequest; |
| 5 | +import jakarta.servlet.http.HttpServletResponse; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | +import org.springframework.util.AntPathMatcher; |
| 9 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | +import java.util.UUID; |
| 13 | + |
| 14 | +@Component |
| 15 | +@Slf4j |
| 16 | +public class DataRequiredInterceptor implements HandlerInterceptor { |
| 17 | + public static final String PATH_FORMAT = "/flow/{flow}/{screen}"; |
| 18 | + private final SubmissionRepositoryService submissionRepositoryService; |
| 19 | + private static final Map<String, String> REQUIRED_DATA = Map.ofEntries( |
| 20 | + // Step 1 |
| 21 | + Map.entry("howToAddDocuments", "firstName"), |
| 22 | + Map.entry("uploadDocuments", "firstName"), |
| 23 | + Map.entry("docSubmitConfirmation", "firstName"), |
| 24 | + Map.entry("finalConfirmation", "firstName") |
| 25 | + ); |
| 26 | + |
| 27 | + public DataRequiredInterceptor(SubmissionRepositoryService submissionRepositoryService) { |
| 28 | + this.submissionRepositoryService = submissionRepositoryService; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
| 33 | + try { |
| 34 | + var parsedUrl = new AntPathMatcher().extractUriTemplateVariables(PATH_FORMAT, request.getRequestURI()); |
| 35 | + var requiredData = REQUIRED_DATA.get(parsedUrl.get("screen")); |
| 36 | + String redirect_url = String.format("/flow/laDocUpload/clientInfo?intercepted=%s", parsedUrl.get("screen")); |
| 37 | + if (requiredData == null) { |
| 38 | + return true; // There are no data requirements for this page |
| 39 | + } |
| 40 | + |
| 41 | + var session = request.getSession(false); |
| 42 | + if (session == null) { |
| 43 | + log.info("No session present (missing field data %s), redirecting to clientInfo page".formatted(requiredData)); |
| 44 | + response.sendRedirect(redirect_url); |
| 45 | + |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + var submissionId = (UUID) session.getAttribute("id"); |
| 50 | + if (submissionId != null) { |
| 51 | + var submissionMaybe = this.submissionRepositoryService.findById(submissionId); |
| 52 | + if (submissionMaybe.isPresent()) { |
| 53 | + var submission = submissionMaybe.get(); |
| 54 | + if (submission.getInputData().getOrDefault(requiredData, "").toString().isBlank()) { |
| 55 | + log.error("Submission %s missing field data %s, redirecting to clientInfo page".formatted(submissionId, requiredData)); |
| 56 | + response.sendRedirect(redirect_url); |
| 57 | + return false; |
| 58 | + } |
| 59 | + } else { |
| 60 | + log.error("Submission %s not found in database (required field %s), redirecting to clientInfo page".formatted(submissionId, requiredData)); |
| 61 | + response.sendRedirect(redirect_url); |
| 62 | + return false; |
| 63 | + } |
| 64 | + } else { |
| 65 | + log.error("No submission ID in session (required field %s), redirecting to clientInfo page".formatted(requiredData)); |
| 66 | + response.sendRedirect(redirect_url); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + return true; |
| 71 | + } catch (IllegalStateException e) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments