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.output.template.Outputable;
13 import org.cacert.gigi.util.HTMLEncoder;
15 public class CertificateValiditySelector implements Outputable {
17 private static final long DAY = 1000 * 60 * 60 * 24;
21 private String val = "2y";
23 public CertificateValiditySelector() {
28 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
29 out.print("<select name='validFrom'><option value='now'");
31 out.print(" selected='selected'");
34 out.print(l.getTranslation("now"));
35 out.print("</option>");
36 long base = getCurrentDayBase();
37 for (int i = 0; i < 14; i++) {
38 long date = base + DAY * i;
39 String d = DateSelector.getDateFormat().format(new Date(date));
40 out.print("<option value='");
43 if (from != null && from.getTime() == date) {
44 out.print(" selected='selected'");
48 out.println("</option>");
50 out.println("</select>");
52 out.print("<input type='text' name='validity' value='");
53 out.print(HTMLEncoder.encodeHTML(val));
62 private long getCurrentDayBase() {
63 long base = System.currentTimeMillis();
69 public void update(HttpServletRequest r) throws GigiApiException {
70 String from = r.getParameter("validFrom");
72 GigiApiException gae = new GigiApiException();
75 } catch (GigiApiException e) {
79 String validity = r.getParameter("validity");
80 if (validity != null) {
81 checkValidityLength(validity);
84 } catch (GigiApiException e) {
87 if ( !gae.isEmpty()) {
93 public static void checkValidityLength(String newval) throws GigiApiException {
94 if (newval.endsWith("y") || newval.endsWith("m")) {
95 if (newval.length() > 10) { // for database
96 throw new GigiApiException("The validity interval entered is invalid.");
98 String num = newval.substring(0, newval.length() - 1);
100 int len = Integer.parseInt(num);
102 throw new GigiApiException("The validity interval entered is invalid.");
104 } catch (NumberFormatException e) {
105 throw new GigiApiException("The validity interval entered is invalid.");
109 DateSelector.getDateFormat().parse(newval);
110 } catch (ParseException e) {
111 throw new GigiApiException("The validity interval entered is invalid.");
116 private void saveStartDate(String from) throws GigiApiException {
117 if (from == null || "now".equals(from)) {
121 this.from = new Date(DateSelector.getDateFormat().parse(from).getTime());
122 } catch (ParseException e) {
123 throw new GigiApiException("The validity start date entered is invalid.");
128 public Date getFrom() {
132 public String getTo() {