Skip to content

Commit 4a20fbf

Browse files
author
manico.james
committed
Moving to a ThreadLocal solution for the ValidationErrorList
1 parent a9d6c7a commit 4a20fbf

5 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/org/owasp/esapi/ESAPI.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ public class ESAPI {
5858
private static SecurityConfiguration securityConfiguration = null;
5959

6060
private static Validator validator = null;
61-
62-
private static ValidatorErrorList validatorErrorList = null;
6361

6462
/**
6563
* prevent instantiation of this class
@@ -270,13 +268,4 @@ public static Validator validator() {
270268
public static void setValidator(Validator validator) {
271269
ESAPI.validator = validator;
272270
}
273-
274-
/**
275-
* @return the validatorErrorList
276-
*/
277-
public static ValidatorErrorList validatorErrorList() {
278-
if (ESAPI.validatorErrorList == null)
279-
ESAPI.validatorErrorList = new DefaultValidatorErrorList();
280-
return ESAPI.validatorErrorList;
281-
}
282271
}

src/org/owasp/esapi/Validator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
* @since June 1, 2007
4747
*/
4848
public interface Validator {
49+
50+
/**
51+
* Returns the ThreadLocal instance of the ValidatorErrorList which can be called
52+
* anywhere in the application without needing to pass the list around.
53+
* @return
54+
*/
55+
public ValidatorErrorList getValidatorErrorList();
4956

5057
/**
5158
* Returns true if input is valid according to the specified type. The type parameter must be the name

src/org/owasp/esapi/ValidatorErrorList.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,10 @@ public interface ValidatorErrorList {
6464
* @param ve
6565
*/
6666
public void addError(String context, ValidationException ve);
67+
68+
/**
69+
* Removes all errors from error list.
70+
*/
71+
public void clearErrors();
6772
}
6873

src/org/owasp/esapi/reference/DefaultValidator.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ public class DefaultValidator implements org.owasp.esapi.Validator {
7171
private static final int MAX_CREDIT_CARD_LENGTH = 19;
7272
private static final int MAX_PARAMETER_NAME_LENGTH = 100;
7373
private static final int MAX_PARAMETER_VALUE_LENGTH = 65535; //max length of MySQL "text" column type
74+
75+
/** list of errors collection class */
76+
private ThreadLocalValidatorErrorList validatorErrorList = new ThreadLocalValidatorErrorList();
77+
78+
private class ThreadLocalValidatorErrorList extends InheritableThreadLocal {
79+
80+
public ValidatorErrorList getValidatorErrorList() {
81+
if ((ValidatorErrorList)super.get() == null) {
82+
super.set(new DefaultValidatorErrorList());
83+
}
84+
return (ValidatorErrorList)super.get();
85+
}
86+
87+
public void setValidatorErrorList(ValidatorErrorList validatorErrorList) {
88+
super.set(validatorErrorList);
89+
}
90+
};
91+
92+
/**
93+
* @return the validatorErrorList
94+
*/
95+
public ValidatorErrorList getValidatorErrorList() {
96+
return (ValidatorErrorList)validatorErrorList.get();
97+
}
7498

7599
public DefaultValidator() {
76100
}

src/org/owasp/esapi/reference/DefaultValidatorErrorList.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,8 @@ public boolean isEmpty() {
9393
if ((errorList == null) || (errorList.size() == 0)) return true;
9494
return false;
9595
}
96+
97+
public void clearErrors() {
98+
errorList.clear();
99+
}
96100
}

0 commit comments

Comments
 (0)