Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose flash keys ERROR and SUCCESS #468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions framework/src/play/mvc/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public static class Flash {
final Map<String, String> data;
final Map<String, String> out = new HashMap<>();

private final String ERROR = "error";
private final String SUCCESS = "success";

public Flash() {
this(new HashMap<>(2));
}
Expand Down Expand Up @@ -96,11 +99,27 @@ private void validateKey(@Nonnull String key) {
}

public void error(@Nonnull String value, Object... args) {
put("error", Messages.get(value, args));
put(ERROR, Messages.get(value, args));
}

public String getErrorMessage() {
return get(ERROR);
}

public boolean containsError() {
return contains(ERROR);
}

public void success(@Nonnull String value, Object... args) {
put("success", Messages.get(value, args));
put(SUCCESS, Messages.get(value, args));
}

public String getSuccessMessage() {
return get(SUCCESS);
}

public boolean containsSuccess() {
return contains(SUCCESS);
}

public void discard(@Nonnull String key) {
Expand Down Expand Up @@ -210,7 +229,7 @@ public void put(@Nonnull String key, @Nullable String value) {
public void put(@Nonnull String key, @Nullable Object value) {
change();
if (value == null) {
put(key, (String) null);
put(key, null);
} else {
put(key, value.toString());
}
Expand Down Expand Up @@ -292,10 +311,8 @@ public void checkAndParse() {
__mergeWith(request.routeArgs);

if (request.querystring != null) {
_mergeWith(
UrlEncodedParser.parseQueryString(
new ByteArrayInputStream(request.querystring.getBytes(request.encoding)),
request.encoding));
var inputStream = new ByteArrayInputStream(request.querystring.getBytes(request.encoding));
_mergeWith(UrlEncodedParser.parseQueryString(inputStream, request.encoding));
}
String contentType = request.contentType;
if (contentType != null) {
Expand All @@ -310,21 +327,21 @@ public void checkAndParse() {
public void put(@Nonnull String key, @Nullable String value) {
checkAndParse();
data.put(key, new String[] {value});
// make sure rootsParamsNode is regenerated if needed
// To ensure rootsParamsNode is regenerated if needed
rootParamsNodeIsGenerated = false;
}

public void put(@Nonnull String key, @Nonnull String[] values) {
checkAndParse();
data.put(key, values);
// make sure rootsParamsNode is regenerated if needed
// To ensure rootsParamsNode is regenerated if needed
rootParamsNodeIsGenerated = false;
}

public void remove(@Nonnull String key) {
checkAndParse();
data.remove(key);
// make sure rootsParamsNode is regenerated if needed
// To ensure rootsParamsNode is regenerated if needed
rootParamsNodeIsGenerated = false;
}

Expand Down Expand Up @@ -447,7 +464,7 @@ public Object get(@Nonnull String key) {

@Nullable
@SuppressWarnings("unchecked")
public <T> T get(String key, Class<T> clazz) {
public <T> T get(String key, @SuppressWarnings("unused") Class<T> clazz) {
return (T) this.get(key);
}

Expand Down
Loading