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

Encrypt error cookie #69

Merged
merged 5 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
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
44 changes: 25 additions & 19 deletions framework/src/play/data/validation/Error.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package play.data.validation;

import net.sf.oval.ConstraintViolation;
import play.i18n.Messages;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.Collection;

import static java.util.Collections.emptyList;

/**
* A validation error.
* A validation error
*/
public class Error {

String message;
String key;
String[] variables;
int severity = 0;

public Error(String key, String message, String[] variables) {
this(key, message, variables, 0);
}
private final String message;
private final String key;
private final Collection<?> variables;

public Error(String key, String message, String[] variables, int severity) {
public Error(String key, String message, Collection<?> variables) {
this.message = message;
this.key = key;
this.variables = variables;
this.severity = severity;
}

/**
Expand All @@ -33,7 +34,7 @@ public String message() {
/**
* @return The field name
*/
public String getKey() {
String getKey() {
return key;
}

Expand All @@ -43,9 +44,12 @@ public String getKey() {
*/
public String message(String key) {
key = Messages.get(key);
Object[] args = new Object[variables.length + 1];
System.arraycopy(variables, 0, args, 1, variables.length);
Object[] args = new Object[variables.size() + 1];
args[0] = key;
int i = 1;
for (Object variable : variables) {
args[i++] = variable;
}
return Messages.get(message, args);
}

Expand All @@ -54,12 +58,14 @@ public String toString() {
return message();
}

public int getSeverity() {
return severity;
}

public String getMessageKey() {
String getMessageKey() {
return message;
}

@Nonnull
@CheckReturnValue
static Error toValidationError(String key, ConstraintViolation violation) {
Collection<?> variables = violation.getMessageVariables() == null ? emptyList() : violation.getMessageVariables().values();
return new Error(key, violation.getMessage(), variables);
}
}
17 changes: 6 additions & 11 deletions framework/src/play/data/validation/ValidCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.util.Collection;
import java.util.List;

@SuppressWarnings("serial")
import static java.util.Collections.emptyList;
import static play.data.validation.Error.toValidationError;

public class ValidCheck extends AbstractAnnotationCheck<Required> {

static final String mes = "validation.object";
Expand Down Expand Up @@ -48,7 +50,7 @@ public boolean isSatisfied(Object validatedObject, Object value, OValContext con
int index = 0;
for(Object item : valueCollection) {
if(!validateObject(key + "[" + (index) + "]", item)) {
Validation.current().errors.add(new Error(key + "[" + (index) + "]", mes, new String[0]));
Validation.current().errors.add(new Error(key + "[" + (index) + "]", mes, emptyList()));
everythingIsValid = false;
}
index++;
Expand All @@ -62,26 +64,19 @@ public boolean isSatisfied(Object validatedObject, Object value, OValContext con
boolean validateObject(String key, Object value) {
ValidationPlugin.keys.get().put(value, key);
List<ConstraintViolation> violations = new Validator().validate(value);
//

if (violations.isEmpty()) {
return true;
} else {
for (ConstraintViolation violation : violations) {
if (violation.getContext() instanceof FieldContext) {
FieldContext ctx = (FieldContext) violation.getContext();
String fkey = (key == null ? "" : key + ".") + ctx.getField().getName();
Error error = new Error(
fkey,
violation.getMessage(),
violation.getMessageVariables() == null ? new String[0]
: violation.getMessageVariables().values()
.toArray(new String[0]),
violation.getSeverity());
Error error = toValidationError(fkey, violation);
Validation.current().errors.add(error);
}
}
return false;
}
}

}
46 changes: 15 additions & 31 deletions framework/src/play/data/validation/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.util.*;
import java.util.regex.Pattern;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;

public class Validation {

public static final ThreadLocal<Validation> current = new ThreadLocal<>();
Expand All @@ -27,11 +30,11 @@ public static Validation current() {
/**
* @return The list of all errors
*/
@SuppressWarnings({"serial", "unused"})
@SuppressWarnings({"unused"})
public static List<Error> errors() {
Validation validation = current.get();
if (validation == null)
return Collections.emptyList();
return emptyList();

return new ArrayList<Error>(validation.errors) {

Expand All @@ -51,7 +54,7 @@ public List<Error> allForKey(String key) {
public Map<String, List<Error>> errorsMap() {
Map<String, List<Error>> result = new LinkedHashMap<>();
for (Error error : errors()) {
result.put(error.key, errors(error.key));
result.put(error.getKey(), errors(error.getKey()));
}
return result;
}
Expand All @@ -63,7 +66,7 @@ public Map<String, List<Error>> errorsMap() {
* @param variables Message variables
*/
public static void addError(String field, String message, String... variables) {
insertError(Validation.current().errors.size(), field, message, variables);
insertError(Validation.current().errors.size(), field, message, variables);
}

/**
Expand All @@ -75,8 +78,8 @@ public static void addError(String field, String message, String... variables) {
*/
public static void insertError(int index, String field, String message, String... variables) {
Error error = error(field);
if (error == null || !error.message.equals(message)) {
Validation.current().errors.add(index, new Error(field, message, variables));
if (error == null || !error.getMessageKey().equals(message)) {
Validation.current().errors.add(index, new Error(field, message, asList(variables)));
}
}

Expand All @@ -91,7 +94,7 @@ public static void removeErrors(String field, String message) {
Iterator<Error> it = validation.errors.iterator();
while (it.hasNext()) {
Error error = it.next();
if (error.key != null && error.key.equals(field) && error.message.equals(message)) {
if (error.getKey() != null && error.getKey().equals(field) && error.getMessageKey().equals(message)) {
it.remove();
}
}
Expand All @@ -105,7 +108,7 @@ public static void removeErrors(String field, String message) {
public static void removeErrors(String field) {
Validation validation = current.get();
if (validation != null) {
validation.errors.removeIf(error -> error.key != null && error.key.equals(field));
validation.errors.removeIf(error -> error.getKey() != null && error.getKey().equals(field));
}
}

Expand Down Expand Up @@ -137,7 +140,7 @@ public static Error error(String field) {
return null;

for (Error error : validation.errors) {
if (error.key!=null && error.key.equals(field)) {
if (error.getKey() != null && error.getKey().equals(field)) {
return error;
}
}
Expand All @@ -151,11 +154,11 @@ public static Error error(String field) {
public static List<Error> errors(String field) {
Validation validation = current.get();
if (validation == null)
return Collections.emptyList();
return emptyList();

List<Error> errors = new ArrayList<>();
for (Error error : validation.errors) {
if (error.key!=null && error.key.equals(field)) {
if (error.getKey() != null && error.getKey().equals(field)) {
errors.add(error);
}
}
Expand Down Expand Up @@ -261,25 +264,9 @@ public Validator(Annotation annotation) {
}
}

// ~~~~ Validations
public static class ValidationResult {

public boolean ok;
public Error error;

public ValidationResult message(String message) {
if (error != null) {
error.message = message;
}
return this;
}

public ValidationResult key(String key) {
if (error != null) {
error.key = key;
}
return this;
}
}

public static ValidationResult required(String key, Object o) {
Expand Down Expand Up @@ -391,10 +378,7 @@ static ValidationResult applyCheck(AbstractAnnotationCheck<?> check, String key,
Error error = new Error(key, check.getClass()
.getDeclaredField("mes").get(null)
+ "",
check.getMessageVariables() == null ? new String[0]
: check.getMessageVariables().values()
.toArray(new String[0]),
check.getSeverity());
check.getMessageVariables() == null ? emptyList() : check.getMessageVariables().values());
Validation.current().errors.add(error);
result.error = error;
result.ok = false;
Expand Down
Loading