From 8e9fece97fa9d7e5f052b63a5c09c026e8b06605 Mon Sep 17 00:00:00 2001 From: Sven Kubiak Date: Wed, 6 Nov 2019 08:41:58 +0100 Subject: [PATCH] #442 Minor refactorings in Form object to retrieve the value of an element more fluent --- .../java/io/mangoo/routing/bindings/Form.java | 34 +++++++++++++++++-- .../handlers/OutboundCookiesHandler.java | 4 +-- .../java/io/mangoo/utils/CodecUtilsTest.java | 2 +- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/mangooio-core/src/main/java/io/mangoo/routing/bindings/Form.java b/mangooio-core/src/main/java/io/mangoo/routing/bindings/Form.java index 21f0639b5c..4c42f89d97 100644 --- a/mangooio-core/src/main/java/io/mangoo/routing/bindings/Form.java +++ b/mangooio-core/src/main/java/io/mangoo/routing/bindings/Form.java @@ -24,7 +24,7 @@ public class Form extends Validator { private transient List files = new ArrayList<>(); private Map> valueMap = new HashMap<>(); private boolean submitted; - private boolean flash; + private boolean keep; public Form() { //Empty constructor for google guice @@ -46,6 +46,23 @@ public Optional getString(String key) { return Optional.empty(); } + + /** + * Retrieves a string value corresponding to the name of the form element + * + * @param key The name of the form element + * @return String with the value of the form element or an empty value if blank + */ + public String getValue(String key) { + Objects.requireNonNull(key, Required.KEY.toString()); + + String value = this.values.get(key); + if (StringUtils.isNotBlank(value)) { + return value; + } + + return ""; + } /** * Retrieves an optional boolean value corresponding to the name of the form element @@ -177,7 +194,7 @@ public void addFile(InputStream inputStream) { * Adds the form values to the flash scope */ public void keep() { - this.flash = true; + this.keep = true; } /** @@ -217,8 +234,19 @@ public List getValueList(String key) { * Checks if the form values are to put in the flash scope * @return True if form values should be put into flash scope, false otherwise */ + public boolean isKept() { + return this.keep; + } + + /** + * @deprecated Use {@link #isKept()} instead + * + * Checks if the form values are to put in the flash scope + * @return True if form values should be put into flash scope, false otherwise + */ + @Deprecated(since = "5.12.0", forRemoval = true) public boolean flashify() { - return this.flash; + return this.keep; } /** diff --git a/mangooio-core/src/main/java/io/mangoo/routing/handlers/OutboundCookiesHandler.java b/mangooio-core/src/main/java/io/mangoo/routing/handlers/OutboundCookiesHandler.java index 0aa68d136d..e6eba21279 100644 --- a/mangooio-core/src/main/java/io/mangoo/routing/handlers/OutboundCookiesHandler.java +++ b/mangooio-core/src/main/java/io/mangoo/routing/handlers/OutboundCookiesHandler.java @@ -199,12 +199,12 @@ protected void setFlashCookie(HttpServerExchange exchange) { .setMaxAge(0); exchange.setResponseCookie(cookie); - } else if (flash.hasContent() || form.flashify()) { + } else if (flash.hasContent() || form.isKept()) { try { JwtClaims jwtClaims = new JwtClaims(); jwtClaims.setClaim(ClaimKey.DATA.toString(), flash.getValues()); - if (form.flashify()) { + if (form.isKept()) { jwtClaims.setClaim(ClaimKey.FORM.toString(), CodecUtils.serializeToBase64(form)); } diff --git a/mangooio-integration-test/src/test/java/io/mangoo/utils/CodecUtilsTest.java b/mangooio-integration-test/src/test/java/io/mangoo/utils/CodecUtilsTest.java index 6ccf06483b..682322bf24 100644 --- a/mangooio-integration-test/src/test/java/io/mangoo/utils/CodecUtilsTest.java +++ b/mangooio-integration-test/src/test/java/io/mangoo/utils/CodecUtilsTest.java @@ -22,7 +22,7 @@ * */ public class CodecUtilsTest { - private static final String SERIALIZED = "rO0ABXNyAB9pby5tYW5nb28ucm91dGluZy5iaW5kaW5ncy5Gb3Jtr0x38MTUh5ACAANaAAVmbGFzaFoACXN1Ym1pdHRlZEwACHZhbHVlTWFwdAAPTGphdmEvdXRpbC9NYXA7eHIAJGlvLm1hbmdvby5yb3V0aW5nLmJpbmRpbmdzLlZhbGlkYXRvct3BGEIQSQk8AgADTAAGZXJyb3JzcQB+AAFMAAhtZXNzYWdlc3QAGUxpby9tYW5nb28vaTE4bi9NZXNzYWdlcztMAAZ2YWx1ZXNxAH4AAXhwc3IAEWphdmEudXRpbC5IYXNoTWFwBQfawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA/QAAAAAAAAHcIAAAAEAAAAAB4c3IAF2lvLm1h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dVUkxfS0VZdAAXezB9IG11c3QgYmUgYSB2YWxpZCBVUkx0AAhJUFY0X0tFWXQAIHswfSBtdXN0IGJlIGEgdmFsaWQgSVB2NCBhZGRyZXNzdAAHTUlOX0tFWXQAI3swfSBtdXN0IGhhdmUgYSBsZWFzdCBhIHNpemUgb2YgezF9dAAJUkVHRVhfS0VZdAAOezB9IGlzIGludmFsaWR4c3EAfgAFP0AAAAAAAAx3CAAAABAAAAABdAADZm9vdAADYmFyeAAAc3EAfgAFP0AAAAAAAAB3CAAAABAAAAAAeA=="; private static final String PLAIN = "this is a plain text"; private static final String JBCRYPT_HASH = "$2a$12$I.tRIbGLB82DDLUHTz.IUOSGeHCwUgX/MnGj67SRFvfzoNZzx2je6";