]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/CertificateValiditySelector.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / org / cacert / gigi / output / CertificateValiditySelector.java
1 package org.cacert.gigi.output;
2
3 import java.io.PrintWriter;
4 import java.sql.Date;
5 import java.text.ParseException;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9
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;
14
15 public class CertificateValiditySelector implements Outputable {
16
17     private static final long DAY = 1000 * 60 * 60 * 24;
18
19     private Date from;
20
21     private String val = "2y";
22
23     public CertificateValiditySelector() {
24
25     }
26
27     @Override
28     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
29         out.print("<label for='validFrom'>");
30         out.println(l.getTranslation("start: "));
31         out.print("</label><select name='validFrom'><option value='now'");
32         if (from == null) {
33             out.print(" selected='selected'");
34         }
35         out.print(">");
36         out.print(l.getTranslation("now"));
37         out.print("</option>");
38         long base = getCurrentDayBase();
39         for (int i = 0; i < 14; i++) {
40             long date = base + DAY * i;
41             String d = DateSelector.getDateFormat().format(new Date(date));
42             out.print("<option value='");
43             out.print(d);
44             out.print("'");
45             if (from != null && from.getTime() == date) {
46                 out.print(" selected='selected'");
47             }
48             out.print(">");
49             out.print(d);
50             out.println("</option>");
51         }
52         out.println("</select>");
53
54         out.print("<label for='validity'>");
55         out.println(l.getTranslation("end: "));
56         out.print("</label>");
57         out.print("<input type='text' name='validity' value='");
58         out.print(HTMLEncoder.encodeHTML(val));
59         out.println("'>");
60
61         if (from == null) {
62             return;
63         }
64
65     }
66
67     private long getCurrentDayBase() {
68         long base = System.currentTimeMillis();
69         base -= base % DAY;
70         base += DAY;
71         return base;
72     }
73
74     public void update(HttpServletRequest r) throws GigiApiException {
75         String from = r.getParameter("validFrom");
76
77         GigiApiException gae = new GigiApiException();
78         try {
79             saveStartDate(from);
80         } catch (GigiApiException e) {
81             gae.mergeInto(e);
82         }
83         try {
84             String validity = r.getParameter("validity");
85             if (validity != null) {
86                 checkValidityLength(validity);
87                 val = validity;
88             }
89         } catch (GigiApiException e) {
90             gae.mergeInto(e);
91         }
92         if ( !gae.isEmpty()) {
93             throw gae;
94         }
95
96     }
97
98     public static void checkValidityLength(String newval) throws GigiApiException {
99         if (newval.endsWith("y") || newval.endsWith("m")) {
100             if (newval.length() > 10) { // for database
101                 throw new GigiApiException("The validity interval entered is invalid.");
102             }
103             String num = newval.substring(0, newval.length() - 1);
104             try {
105                 int len = Integer.parseInt(num);
106                 if (len <= 0) {
107                     throw new GigiApiException("The validity interval entered is invalid.");
108                 }
109             } catch (NumberFormatException e) {
110                 throw new GigiApiException("The validity interval entered is invalid.");
111             }
112         } else {
113             try {
114                 DateSelector.getDateFormat().parse(newval);
115             } catch (ParseException e) {
116                 throw new GigiApiException("The validity interval entered is invalid.");
117             }
118         }
119     }
120
121     private void saveStartDate(String from) throws GigiApiException {
122         if (from == null || "now".equals(from)) {
123             this.from = null;
124         } else {
125             try {
126                 this.from = new Date(DateSelector.getDateFormat().parse(from).getTime());
127             } catch (ParseException e) {
128                 throw new GigiApiException("The validity start date entered is invalid.");
129             }
130         }
131     }
132
133     public Date getFrom() {
134         return from;
135     }
136
137     public String getTo() {
138         return val;
139     }
140
141 }