Skip to content

Commit

Permalink
#442 Minor refactorings in Form object to retrieve the value of an el…
Browse files Browse the repository at this point in the history
…ement more fluent
  • Loading branch information
svenkubiak committed Nov 6, 2019
1 parent 7096b4f commit 8e9fece
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
34 changes: 31 additions & 3 deletions mangooio-core/src/main/java/io/mangoo/routing/bindings/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Form extends Validator {
private transient List<InputStream> files = new ArrayList<>();
private Map<String, List<String>> valueMap = new HashMap<>();
private boolean submitted;
private boolean flash;
private boolean keep;

public Form() {
//Empty constructor for google guice
Expand All @@ -46,6 +46,23 @@ public Optional<String> 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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -217,8 +234,19 @@ public List<String> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/
public class CodecUtilsTest {
private static final String SERIALIZED = "rO0ABXNyAB9pby5tYW5nb28ucm91dGluZy5iaW5kaW5ncy5Gb3Jtr0x38MTUh5ACAANaAAVmbGFzaFoACXN1Ym1pdHRlZEwACHZhbHVlTWFwdAAPTGphdmEvdXRpbC9NYXA7eHIAJGlvLm1hbmdvby5yb3V0aW5nLmJpbmRpbmdzLlZhbGlkYXRvct3BGEIQSQk8AgADTAAGZXJyb3JzcQB+AAFMAAhtZXNzYWdlc3QAGUxpby9tYW5nb28vaTE4bi9NZXNzYWdlcztMAAZ2YWx1ZXNxAH4AAXhwc3IAEWphdmEudXRpbC5IYXNoTWFwBQfawcMWYNEDAAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA/QAAAAAAAAHcIAAAAEAAAAAB4c3IAF2lvLm1hbmdvby5pMThuLk1lc3NhZ2VzaOrotu6i5LoCAAFMAAhkZWZhdWx0c3EAfgABeHBzcQB+AAU/QAAAAAAAGHcIAAAAIAAAAA50AAxSRVFVSVJFRF9LRVl0AA97MH0gaXMgcmVxdWlyZWR0AAlNQVRDSF9LRVl0ABJ7MH0gbXVzdCBtYXRjaCB7MX10AAlSQU5HRV9LRVl0ACh7MH0gbXVzdCBoYXZlIGEgc2l6ZSBiZXR3ZWVuIHsxfSBhbmQgezJ9dAAISVBWNl9LRVl0ACB7MH0gbXVzdCBiZSBhIHZhbGlkIElQdjYgYWRkcmVzc3QAC05VTUVSSUNfS0VZdAAbezB9IG11c3QgYmUgYSBudW1lcmljIHZhbHVldAAHTUFYX0tFWXQAH3swfSBtdXN0IGhhdmUgYSBzaXplIG9mIG1heCB7MX10AA9ET01BSU5fTkFNRV9LRVl0AB97MH0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UkVEX0tFWXQAD3swfSBpcyByZXF1aXJlZHQACU1BVENIX0tFWXQAEnswfSBtdXN0IG1hdGNoIHsxfXQACVJBTkdFX0tFWXQAKHswfSBtdXN0IGhhdmUgYSBzaXplIGJldHdlZW4gezF9IGFuZCB7Mn10AAhJUFY2X0tFWXQAIHswfSBtdXN0IGJlIGEgdmFsaWQgSVB2NiBhZGRyZXNzdAALTlVNRVJJQ19LRVl0ABt7MH0gbXVzdCBiZSBhIG51bWVyaWMgdmFsdWV0AAdNQVhfS0VZdAAfezB9IG11c3QgaGF2ZSBhIHNpemUgb2YgbWF4IHsxfXQAD0RPTUFJTl9OQU1FX0tFWXQAH3swfSBtdXN0IGJlIGEgdmFsaWQgZG9tYWluIG5hbWV0AA9FWEFDVF9NQVRDSF9LRVl0ABp7MH0gbXVzdCBleGFjdGx5IG1hdGNoIHsxfXQACUVNQUlMX0tFWXQAIXswfSBtdXN0IGJlIGEgdmFsaWQgZU1haWwgYWRkcmVzc3QAEE1BVENIX1ZBTFVFU19LRVl0AB5UaGUgdmFsdWVzIG9mIHswfSBpcyBub3QgdmFsaWR0AAdVUkxfS0VZdAAXezB9IG11c3QgYmUgYSB2YWxpZCBVUkx0AAhJUFY0X0tFWXQAIHswfSBtdXN0IGJlIGEgdmFsaWQgSVB2NCBhZGRyZXNzdAAHTUlOX0tFWXQAI3swfSBtdXN0IGhhdmUgYSBsZWFzdCBhIHNpemUgb2YgezF9dAAJUkVHRVhfS0VZdAAOezB9IGlzIGludmFsaWR4c3EAfgAFP0AAAAAAAAx3CAAAABAAAAABdAADZm9vdAADYmFyeAAAc3EAfgAFP0AAAAAAAAB3CAAAABAAAAAAeA==";
private static final String PLAIN = "this is a plain text";
private static final String JBCRYPT_HASH = "$2a$12$I.tRIbGLB82DDLUHTz.IUOSGeHCwUgX/MnGj67SRFvfzoNZzx2je6";

Expand Down

0 comments on commit 8e9fece

Please sign in to comment.