@Override
public void output(PrintWriter out, Language l, Map<String, Object> vars) {
out.println("<form method='POST' autocomplete='off'>");
+ failed = false;
outputContent(out, l, vars);
out.print("<input type='hidden' name='" + CSRF_FIELD + "' value='");
out.print(getCSRFToken());
protected abstract void outputContent(PrintWriter out, Language l, Map<String, Object> vars);
- protected void outputError(PrintWriter out, ServletRequest req, String text) {
+ boolean failed;
+
+ protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
+ if ( !failed) {
+ failed = true;
+ out.println("<div class='formError'>");
+ }
out.print("<div>");
- out.print(Page.translate(req, text));
+ if (contents.length == 0) {
+ out.print(Page.translate(req, text));
+ } else {
+ out.print(String.format(Page.translate(req, text), contents));
+ }
out.println("</div>");
}
+ protected void outputErrorPlain(PrintWriter out, String text) {
+ if ( !failed) {
+ failed = true;
+ out.println("<div class='formError'>");
+ }
+ out.print("<div>");
+ out.print(text);
+ out.println("</div>");
+ }
+
+ public boolean isFailed(PrintWriter out) {
+ if (failed) {
+ out.println("</div>");
+ }
+ return failed;
+ }
+
protected String getCSRFToken() {
return csrf;
}
@Override
public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
update(req);
- boolean failed = false;
- out.println("<div class='formError'>");
if (buildup.getFname().equals("") || buildup.getLname().equals("")) {
outputError(out, req, "First and/or last names were blank.");
- failed = true;
}
if ( !myDoB.isValid()) {
outputError(out, req, "Invalid date of birth");
- failed = true;
}
if ( !"1".equals(req.getParameter("cca_agree"))) {
outputError(out, req, "You have to agree to the CAcert Community agreement.");
- failed = true;
}
if (buildup.getEmail().equals("")) {
outputError(out, req, "Email Address was blank");
- failed = true;
}
String pw1 = req.getParameter("pword1");
String pw2 = req.getParameter("pword2");
if (pw1 == null || pw1.equals("")) {
outputError(out, req, "Pass Phrases were blank");
- failed = true;
} else if ( !pw1.equals(pw2)) {
outputError(out, req, "Pass Phrases don't match");
- failed = true;
}
int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildup);
if (pwpoints < 3) {
outputError(out, req, "The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address.");
- failed = true;
}
- if (failed) {
- out.println("</div>");
+ if (isFailed(out)) {
return false;
}
try {
ResultSet r2 = q2.executeQuery();
if (r1.next() || r2.next()) {
outputError(out, req, "This email address is currently valid in the system.");
- failed = true;
}
r1.close();
r2.close();
ResultSet r3 = q3.executeQuery();
if (r3.next()) {
String domain = r3.getString(1);
- out.print("<div>");
- out.print(String.format(Page.translate(req, "We don't allow signups from people using email addresses from %s"), domain));
- out.println("</div>");
- failed = true;
+ outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
}
r3.close();
} catch (SQLException e) {
e.printStackTrace();
- failed = true;
+ outputError(out, req, "an internal error happened");
}
String mailResult = EmailProvider.FAIL;
try {
if (mailResult.equals(EmailProvider.FAIL)) {
outputError(out, req, "Failed to make a connection to the mail server");
} else {
- out.print("<div>");
- out.print(mailResult);
- out.println("</div>");
+ outputErrorPlain(out, mailResult);
}
- failed = true;
}
- out.println("</div>");
- if (failed) {
+ if (isFailed(out)) {
return false;
}
try {
@Override
public boolean submit(PrintWriter out, HttpServletRequest req) {
- out.println("<div class='formError'>");
- boolean failed = false;
-
if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("CCAAgreed")) || !"1".equals(req.getParameter("assertion"))) {
outputError(out, req, "You failed to check all boxes to validate" + " your adherence to the rules and policies of CAcert");
- failed = true;
}
if (req.getParameter("date") == null || req.getParameter("date").equals("")) {
outputError(out, req, "You must enter the date when you met the assuree.");
- failed = true;
} else {
try {
Date d = sdf.parse(req.getParameter("date"));
if (d.getTime() > System.currentTimeMillis()) {
outputError(out, req, "You must not enter a date in the future.");
- failed = true;
}
} catch (ParseException e) {
outputError(out, req, "You must enter the date in this format: YYYY-MM-DD.");
- failed = true;
}
}
// check location, min 3 characters
if (req.getParameter("location") == null || req.getParameter("location").equals("")) {
outputError(out, req, "You failed to enter a location of your meeting.");
- failed = true;
} else if (req.getParameter("location").length() <= 2) {
outputError(out, req, "You must enter a location with at least 3 characters eg town and country.");
- failed = true;
}
// TODO checkPoints
String points = req.getParameter("points");
if (points == null || "".equals(points)) {
- // TODO message
- failed = true;
+ outputError(out, req, "For an assurance, you need to enter points.");
}
- if (failed) {
- out.println("</div>");
+ if (isFailed(out)) {
return false;
}
try {
if (success != AssuranceResult.ASSURANCE_SUCCEDED) {
outputError(out, req, success.getMessage());
}
- out.println("</div>");
return success == AssuranceResult.ASSURANCE_SUCCEDED;
} catch (SQLException e) {
e.printStackTrace();
}
- out.println("</div>");
return false;
}
String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1";
String data = fetchStartErrorMessage(runRegister(query));
- assertTrue(data, data.startsWith("</div>"));
+ assertNull(data);
assertSuccessfullRegMail();
getMailReciever().setEmailCheckError("400 Greylisted");
getMailReciever().setApproveRegex(Pattern.compile("a"));
query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1";
data = fetchStartErrorMessage(runRegister(query));
- assertFalse(data, data.startsWith("</div>"));
+ assertNotNull(data);
assertNull(getMailReciever().recieve());
@Test
public void testAssureForm() throws IOException {
String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
- assertTrue(error, error.startsWith("</div>"));
+ assertNull(error);
}
@Test
try {
String query = "fname=" + URLEncoder.encode(firstName, "UTF-8") + "&lname=" + URLEncoder.encode(lastName, "UTF-8") + "&email=" + URLEncoder.encode(email, "UTF-8") + "&pword1=" + URLEncoder.encode(password, "UTF-8") + "&pword2=" + URLEncoder.encode(password, "UTF-8") + "&day=1&month=1&year=1910&cca_agree=1";
String data = fetchStartErrorMessage(runRegister(query));
- assertTrue(data, data.startsWith("</div>"));
+ assertNull(data);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
} catch (IOException e) {
static int count = 0;
public static String createUniqueName() {
- return "test" + System.currentTimeMillis() + "a" + (count++);
+ return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
}
private String stripCookie(String headerField) {