
io.mangoo.routing.bindings.Flash Maven / Gradle / Ivy
The newest version!
package io.mangoo.routing.bindings;
import io.mangoo.constants.NotNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Flash {
private static final Logger LOG = LogManager.getLogger(Flash.class);
private static final Set INVALID_CHARACTERS = Set.of("|", ":", "&", " ");
private static final String ERROR = "error";
private static final String WARNING = "warning";
private static final String SUCCESS = "success";
private Map values = new HashMap<>();
private boolean discard;
private boolean invalid;
public Flash() {
//Empty constructor required for Google Guice
}
public static Flash create() {
return new Flash();
}
public Flash withContent(Map values) {
Objects.requireNonNull(values, NotNull.VALUES);
this.values = values;
return this;
}
/**
* Sets a specific error message available with
* the key 'error'
*
* @param value The message
*/
public void setError(String value) {
if (validCharacters(value)) {
values.put(ERROR, value);
}
}
/**
* Sets a specific warning message available with
* the key 'warning'
*
* @param value The message
*/
public void setWarning(String value) {
if (validCharacters(value)) {
values.put(WARNING, value);
}
}
/**
* Sets a specific success message available with
* the key 'success'
*
* @param value The message
*/
public void setSuccess(String value) {
if (validCharacters(value)) {
values.put(SUCCESS, value);
}
}
/**
* Adds a value with a specific key to the flash overwriting an
* existing value
*
* @param key The key
* @param value The value
*/
public void put(String key, String value) {
if (validCharacters(key) && validCharacters(value)) {
values.put(key, value);
}
}
/**
* Invalidates the flash by sending expiring the client cookie
*/
public void invalidate() {
invalid = true;
}
/**
* Retrieves a specific value from the flash
*
* @param key The key
* @return The value or null if not found
*/
public String get(String key) {
return values.get(key);
}
/**
* Remove a specific value from the flash
* @param key The key
*/
public String remove(String key) {
return values.remove(key);
}
public Map getValues() {
return values;
}
public boolean isDiscard() {
return discard;
}
public Flash setDiscard(boolean discard) {
this.discard = discard;
return this;
}
public boolean isInvalid() {
return invalid;
}
public boolean hasContent() {
return !values.isEmpty();
}
/**
* Checks if the given value contains characters that are not allowed
* in the key or value of a flash cookie
*
* @param value The value to check
* @return True if the given string is valid, false otherwise
*/
private boolean validCharacters(String value) {
if (INVALID_CHARACTERS.contains(value)) {
LOG.error("Flash key or value can not contain the following characters: spaces, |, & or :");
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy