1 package org.cacert.gigi.pages.wot;
3 import java.io.PrintWriter;
4 import java.text.SimpleDateFormat;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.LinkedList;
10 import javax.servlet.http.HttpServletRequest;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.IterableDataset;
19 import org.cacert.gigi.output.template.Template;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.pages.PasswordResetPage;
22 import org.cacert.gigi.util.DayDate;
23 import org.cacert.gigi.util.Notary;
25 public class AssuranceForm extends Form {
29 private Name assureeName;
33 private String location = "";
35 private String date = "";
41 private AssuranceType type = AssuranceType.FACE_TO_FACE;
43 private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
45 public AssuranceForm(HttpServletRequest hsr, User assuree) {
47 assurer = Page.getUser(hsr);
48 this.assuree = assuree;
49 assureeName = this.assuree.getName();
50 dob = this.assuree.getDoB();
53 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
55 SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
58 public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
59 HashMap<String, Object> res = new HashMap<String, Object>();
61 res.put("nameExplicit", assuree.getName());
62 res.put("name", assuree.getName().toString());
63 res.put("maxpoints", assurer.getMaxAssurePoints());
64 res.put("dob", sdf.format(assuree.getDoB().toDate()));
65 res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
66 res.put("location", location);
67 res.put("date", date);
68 res.put("aword", aword);
69 final LinkedList<AssuranceType> ats = new LinkedList<>();
70 for (AssuranceType at : AssuranceType.values()) {
72 Notary.may(assurer, assuree, at);
74 } catch (GigiApiException e) {
77 res.put("ats", new IterableDataset() {
79 Iterator<AssuranceType> t = ats.iterator();
82 public boolean next(Language l, Map<String, Object> vars) {
86 AssuranceType t1 = t.next();
87 vars.put("type", t1.getDescription());
88 vars.put("id", t1.toString());
89 vars.put("sel", t1 == type ? " selected" : "");
93 templ.output(out, l, res);
97 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
98 location = req.getParameter("location");
99 date = req.getParameter("date");
100 GigiApiException gae = new GigiApiException();
101 if (date == null || location == null) {
102 gae.mergeInto(new GigiApiException("You need to enter location and date!"));
105 if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
106 gae.mergeInto(new GigiApiException("You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA"));
108 if ("1".equals(req.getParameter("passwordReset"))) {
109 aword = req.getParameter("passwordResetValue");
110 if ("".equals(aword)) {
116 String val = req.getParameter("assuranceType");
119 type = AssuranceType.valueOf(val);
120 } catch (IllegalArgumentException e) {
121 gae.mergeInto(new GigiApiException("Assurance Type wrong."));
126 String points = req.getParameter("points");
127 if (points == null || "".equals(points)) {
128 gae.mergeInto(new GigiApiException("For an assurance, you need to enter points."));
131 pointsI = Integer.parseInt(points);
132 } catch (NumberFormatException e) {
133 gae.mergeInto(new GigiApiException("The points entered were not a number."));
137 if ( !gae.isEmpty()) {
140 Notary.assure(assurer, assuree, assureeName, dob, pointsI, location, req.getParameter("date"), type);
141 if (aword != null && !aword.equals("")) {
142 Language l = Language.getInstance(assuree.getPreferredLocale());
143 String method = l.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form:");
144 String subject = l.getTranslation("Password reset by assurance");
145 PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, l, method, subject);
150 public User getAssuree() {