From 0e585ed1cd117ec646b3291abfffeedf4177cea4 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 1 Jun 2024 21:21:55 +0100 Subject: [PATCH] redirect on invalid paste --- .../backend/controller/IndexController.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/cc/fascinated/backend/controller/IndexController.java b/src/main/java/cc/fascinated/backend/controller/IndexController.java index 6cdae45..3beb445 100644 --- a/src/main/java/cc/fascinated/backend/controller/IndexController.java +++ b/src/main/java/cc/fascinated/backend/controller/IndexController.java @@ -1,5 +1,6 @@ package cc.fascinated.backend.controller; +import cc.fascinated.backend.exception.impl.ResourceNotFoundException; import cc.fascinated.backend.model.Paste; import cc.fascinated.backend.service.PasteService; import org.springframework.beans.factory.annotation.Autowired; @@ -30,18 +31,29 @@ public class IndexController { @GetMapping(value = "/{id}") public String paste(@PathVariable String id, Model model) { - Paste paste = pasteService.getPaste(id); - model.addAttribute("paste", paste); - model.addAttribute("title", "Paste - " + paste.getId()); - model.addAttribute("rawUrl", "/raw/" + paste.getId()); - return "paste"; + try { + Paste paste = pasteService.getPaste(id); + if (paste == null) { // If the paste does not exist, redirect to the home page + return "redirect:/"; + } + model.addAttribute("paste", paste); + model.addAttribute("title", "Paste - " + paste.getId()); + model.addAttribute("rawUrl", "/raw/" + paste.getId()); + return "paste"; + } catch (ResourceNotFoundException ex) { + return "redirect:/"; + } } @GetMapping(value = "/raw/{id}") public String pasteRaw(@PathVariable String id, Model model) { - Paste paste = pasteService.getPaste(id); - model.addAttribute("paste", paste); - model.addAttribute("title", "Paste - " + paste.getId() + " (Raw)"); - return "paste-raw"; + try { + Paste paste = pasteService.getPaste(id); + model.addAttribute("paste", paste); + model.addAttribute("title", "Paste - " + paste.getId() + " (Raw)"); + return "paste-raw"; + } catch (ResourceNotFoundException ex) { + return "redirect:/"; + } } }