forked from ESAPI/esapi-java-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationErrorList.java
More file actions
143 lines (133 loc) · 4.29 KB
/
Copy pathValidationErrorList.java
File metadata and controls
143 lines (133 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project. For details, please see
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
*
* Copyright (c) 2007 - The OWASP Foundation
*
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
* LICENSE before you use, modify, and/or redistribute this software.
*
* @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
*
* @created 2007
*/
package org.owasp.esapi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.owasp.esapi.errors.ValidationException;
/**
* The ValidationErrorList class defines a well-formed collection of
* ValidationExceptions so that groups of validation functions can be
* called in a non-blocking fashion.
*
* <P>
* <img src="doc-files/Validator.jpg" height="600">
* <P>
*
* To use the ValidationErrorList to execute groups of validation
* attempts, your controller code would look something like:
*
* <PRE>
* ValidationErrorList() errorList = new ValidationErrorList();.
* String name = getValidInput("Name", form.getName(), "SomeESAPIRegExName1", 255, false, errorList);
* String address = getValidInput("Address", form.getAddress(), "SomeESAPIRegExName2", 255, false, errorList);
* Integer weight = getValidInteger("Weight", form.getWeight(), 1, 1000000000, false, errorList);
* Integer sortOrder = getValidInteger("Sort Order", form.getSortOrder(), -100000, +100000, false, errorList);
* request.setAttribute( "ERROR_LIST", errorList );
* </PRE>
*
* The at your view layer you would be able to retrieve all
* of your error messages via a helper function like:
*
* <PRE>
* public static ValidationErrorList getErrors() {
* HttpServletRequest request = ESAPI.httpUtilities().getCurrentRequest();
* ValidationErrorList errors = new ValidationErrorList();
* if (request.getAttribute(Constants.ERROR_LIST) != null) {
* errors = (ValidationErrorList)request.getAttribute("ERROR_LIST");
* }
* return errors;
* }
* </PRE>
*
* You can list all errors like:
*
* <PRE>
* <%
* for (Object vo : errorList.errors()) {
* ValidationException ve = (ValidationException)vo;
* %>
* <%= ESAPI.encoder().encodeForHTML(ve.getMessage()) %><br/>
* <%
* }
* %>
* </PRE>
*
* And even check if a specific UI component is in error via calls like:
*
* <PRE>
* ValidationException e = errorList.getError("Name");
* </PRE>
*
* @author Jim Manico (jim.manico .at. aspectsecurity.com)
* <a href="http://www.aspectsecurity.com">Aspect Security</a>
* @since August 15, 2008
*/
public class ValidationErrorList {
/**
* Error list of ValidationException's
*/
private HashMap errorList = new HashMap();
/**
* Adds a new error to list with a unique named context.
* No action taken if either element is null.
* Existing contexts will be overwritten.
*
* @param context unique named context for this ValidationErrorList
* @param ve
*/
public void addError(String context, ValidationException ve) {
if (getError(context) != null) throw new RuntimeException("Context (" + context + ") already exists, programmer error");
if ((context != null) && (ve != null)) {
errorList.put(context, ve);
}
}
/**
* Returns list of ValidationException, or empty list of no errors exist.
*
* @return List
*/
public List errors() {
return new ArrayList( errorList.values() );
}
/**
* Retrieves ValidationException for given context if one exists.
*
* @param context unique name for each error
* @return ValidationException or null for given context
*/
public ValidationException getError(String context) {
if (context == null) return null;
return (ValidationException)errorList.get(context);
}
/**
* Returns true if no error are present.
*
* @return boolean
*/
public boolean isEmpty() {
return errorList.isEmpty();
}
/**
* Returns the numbers of errors present.
*
* @return boolean
*/
public int size() {
return errorList.size();
}
}