1 package org.cacert.gigi.output;
3 import java.io.PrintWriter;
5 import java.text.ParseException;
8 import javax.servlet.http.HttpServletRequest;
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.util.HTMLEncoder;
14 public class CertificateValiditySelector implements Outputable {
16 private static final long DAY = 1000 * 60 * 60 * 24;
20 private String val = "2y";
22 public CertificateValiditySelector() {
27 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
28 out.print("<select name='validFrom'><option value='now'");
30 out.print(" selected='selected'");
33 out.print(l.getTranslation("now"));
34 out.print("</option>");
35 long base = getCurrentDayBase();
36 for (int i = 0; i < 14; i++) {
37 long date = base + DAY * i;
38 String d = DateSelector.getDateFormat().format(new Date(date));
39 out.print("<option value='");
42 if (from != null && from.getTime() == date) {
43 out.print(" selected='selected'");
47 out.println("</option>");
49 out.println("</select>");
51 out.print("<input type='text' name='validity' value='");
52 out.print(HTMLEncoder.encodeHTML(val));
61 private long getCurrentDayBase() {
62 long base = System.currentTimeMillis();
68 public void update(HttpServletRequest r) throws GigiApiException {
69 String from = r.getParameter("validFrom");
71 GigiApiException gae = new GigiApiException();
74 } catch (GigiApiException e) {
78 String validity = r.getParameter("validity");
79 if (validity != null) {
80 checkValidityLength(validity);
83 } catch (GigiApiException e) {
86 if ( !gae.isEmpty()) {
92 public static void checkValidityLength(String newval) throws GigiApiException {
93 if (newval.endsWith("y") || newval.endsWith("m")) {
94 if (newval.length() > 10) { // for database
95 throw new GigiApiException("The validity interval entered is invalid.");
97 String num = newval.substring(0, newval.length() - 1);
99 int len = Integer.parseInt(num);
101 throw new GigiApiException("The validity interval entered is invalid.");
103 } catch (NumberFormatException e) {
104 throw new GigiApiException("The validity interval entered is invalid.");
108 DateSelector.getDateFormat().parse(newval);
109 } catch (ParseException e) {
110 throw new GigiApiException("The validity interval entered is invalid.");
115 private void saveStartDate(String from) throws GigiApiException {
116 if (from == null || "now".equals(from)) {
120 this.from = new Date(DateSelector.getDateFormat().parse(from).getTime());
121 } catch (ParseException e) {
122 throw new GigiApiException("The validity start date entered is invalid.");
127 public Date getFrom() {
131 public String getTo() {