From 855fae08a2f992bdeb8d92715f1665e9961d9282 Mon Sep 17 00:00:00 2001 From: NikolayLarin Date: Mon, 14 Feb 2022 13:52:16 +0200 Subject: [PATCH] immutable resumes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit если находимся не в режиме дебага, то - скрываем кнопки уделения и редактирования для неизменных резюме - для особо умных, чтобы не удалили через прямой запрос, делается доп проверка в сервлете --- .gitignore | 4 +-- config/resumes.properties | 4 +++ src/ru/javawebinar/basejava/Config.java | 31 +++++++++++++++++++ .../basejava/web/ResumeServlet.java | 12 ++++++- test/ru/javawebinar/basejava/TestData.java | 7 +++++ .../basejava/storage/AbstractStorageTest.java | 3 ++ web/WEB-INF/jsp/edit.jsp | 8 +++-- web/WEB-INF/jsp/list.jsp | 9 ++++-- 8 files changed, 69 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index a64f0357..50d27906 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,4 @@ out log lib - - - +storage \ No newline at end of file diff --git a/config/resumes.properties b/config/resumes.properties index ff9f0f81..57b1109d 100644 --- a/config/resumes.properties +++ b/config/resumes.properties @@ -1,5 +1,9 @@ storage.dir=C:/projects/basejava/storage +immutable.uuid.one = 11111111-c3d9-4a5c-1111-ed49a6da0a0f +immutable.uuid.two = 22222222-c3d9-4a5c-2222-ed49a6da0a0f +debug.mode = true + # Replace by Heroku credentials db.url=jdbc:postgresql://localhost:5432/resumes db.user=postgres diff --git a/src/ru/javawebinar/basejava/Config.java b/src/ru/javawebinar/basejava/Config.java index 5111b689..343ed54e 100644 --- a/src/ru/javawebinar/basejava/Config.java +++ b/src/ru/javawebinar/basejava/Config.java @@ -6,7 +6,10 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; +import java.util.HashSet; import java.util.Properties; +import java.util.Set; public class Config { private static final String PROPS = "/resumes.properties"; @@ -15,6 +18,12 @@ public class Config { private final File storageDir; private final Storage storage; + private final boolean debugMode; + + private final Set immutableUuids = new HashSet<>(); + private final String immutableUuidOne; + private final String immutableUuidTwo; + public static Config get() { return INSTANCE; } @@ -25,6 +34,12 @@ private Config() { props.load(is); storageDir = new File(props.getProperty("storage.dir")); storage = new SqlStorage(props.getProperty("db.url"), props.getProperty("db.user"), props.getProperty("db.password")); + + debugMode = Boolean.parseBoolean(props.getProperty("debug.mode")); + + immutableUuidOne = props.getProperty("immutable.uuid.one"); + immutableUuidTwo = props.getProperty("immutable.uuid.two"); + immutableUuids.addAll(Arrays.asList(immutableUuidOne, immutableUuidTwo)); } catch (IOException e) { throw new IllegalStateException("Invalid config file " + PROPS); } @@ -37,4 +52,20 @@ public File getStorageDir() { public Storage getStorage() { return storage; } + + public Set getImmutableUuids() { + return immutableUuids; + } + + public boolean debugMode() { + return debugMode; + } + + public String getImmutableUuidOne() { + return immutableUuidOne; + } + + public String getImmutableUuidTwo() { + return immutableUuidTwo; + } } \ No newline at end of file diff --git a/src/ru/javawebinar/basejava/web/ResumeServlet.java b/src/ru/javawebinar/basejava/web/ResumeServlet.java index fdf4164f..05813de0 100644 --- a/src/ru/javawebinar/basejava/web/ResumeServlet.java +++ b/src/ru/javawebinar/basejava/web/ResumeServlet.java @@ -26,13 +26,19 @@ private enum THEME { private Storage storage; // = Config.get().getStorage(); private final Set themes = new HashSet<>(); // https://stackoverflow.com/a/4936895/548473 + private Set immutableUuids; + private boolean debugMode; + @Override public void init(ServletConfig config) throws ServletException { super.init(config); - storage = Config.get().getStorage(); + final Config cfg = Config.get(); + storage = cfg.getStorage(); for (THEME t : THEME.values()) { themes.add(t.name()); } + immutableUuids = cfg.getImmutableUuids(); + debugMode = cfg.debugMode(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { @@ -40,6 +46,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) String uuid = request.getParameter("uuid"); String fullName = request.getParameter("fullName"); + if (!debugMode && immutableUuids.contains(uuid) ) { + response.sendRedirect("resume?theme=" + getTheme(request)); + } + final boolean isCreate = (uuid == null || uuid.length() == 0); Resume r; if (isCreate) { diff --git a/test/ru/javawebinar/basejava/TestData.java b/test/ru/javawebinar/basejava/TestData.java index c55d85da..b2246a3c 100644 --- a/test/ru/javawebinar/basejava/TestData.java +++ b/test/ru/javawebinar/basejava/TestData.java @@ -11,12 +11,19 @@ public class TestData { public static final String UUID_3 = UUID.randomUUID().toString(); public static final String UUID_4 = UUID.randomUUID().toString(); + public static final Resume R1_IMMUTABLE; + public static final Resume R2_IMMUTABLE; + public static final Resume R1; public static final Resume R2; public static final Resume R3; public static final Resume R4; static { + Config config = Config.get(); + R1_IMMUTABLE = new Resume(config.getImmutableUuidOne(), "Григорий Кислин"); + R2_IMMUTABLE = new Resume(config.getImmutableUuidTwo(), "Николай Ларин"); + R1 = new Resume(UUID_1, "Name1"); R2 = new Resume(UUID_2, "Name2"); R3 = new Resume(UUID_3, "Name3"); diff --git a/test/ru/javawebinar/basejava/storage/AbstractStorageTest.java b/test/ru/javawebinar/basejava/storage/AbstractStorageTest.java index 9eefcc7f..36acb86e 100644 --- a/test/ru/javawebinar/basejava/storage/AbstractStorageTest.java +++ b/test/ru/javawebinar/basejava/storage/AbstractStorageTest.java @@ -75,6 +75,9 @@ public void save() throws Exception { storage.save(R4); assertSize(4); assertGet(R4); + storage.save(R1_IMMUTABLE); + storage.save(R2_IMMUTABLE); + assertSize(6); } @Test(expected = ExistStorageException.class) diff --git a/web/WEB-INF/jsp/edit.jsp b/web/WEB-INF/jsp/edit.jsp index ddc55367..bb0d6c3e 100644 --- a/web/WEB-INF/jsp/edit.jsp +++ b/web/WEB-INF/jsp/edit.jsp @@ -3,6 +3,7 @@ <%@ page import="ru.javawebinar.basejava.model.OrganizationSection" %> <%@ page import="ru.javawebinar.basejava.model.SectionType" %> <%@ page import="ru.javawebinar.basejava.util.DateUtil" %> +<%@ page import="ru.javawebinar.basejava.Config" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> @@ -91,8 +92,11 @@
- - + + <% Config cfg = Config.get(); %> + + +
diff --git a/web/WEB-INF/jsp/list.jsp b/web/WEB-INF/jsp/list.jsp index a6854c55..b35c296d 100644 --- a/web/WEB-INF/jsp/list.jsp +++ b/web/WEB-INF/jsp/list.jsp @@ -1,4 +1,5 @@ <%@ page import="ru.javawebinar.basejava.model.ContactType" %> +<%@ page import="ru.javawebinar.basejava.Config" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> @@ -58,9 +59,11 @@ - - - + + + + +