]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/CertificateValiditySelector.java
Merge branch 'libs/jetty/local'
[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("<select name='validFrom'><option value='now'");
30         if (from == null) {
31             out.print(" selected='selected'");
32         }
33         out.print(">");
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='");
41             out.print(d);
42             out.print("'");
43             if (from != null && from.getTime() == date) {
44                 out.print(" selected='selected'");
45             }
46             out.print(">");
47             out.print(d);
48             out.println("</option>");
49         }
50         out.println("</select>");
51
52         out.print("<input type='text' name='validity' value='");
53         out.print(HTMLEncoder.encodeHTML(val));
54         out.println("'>");
55
56         if (from == null) {
57             return;
58         }
59
60     }
61
62     private long getCurrentDayBase() {
63         long base = System.currentTimeMillis();
64         base -= base % DAY;
65         base += DAY;
66         return base;
67     }
68
69     public void update(HttpServletRequest r) throws GigiApiException {
70         String from = r.getParameter("validFrom");
71
72         GigiApiException gae = new GigiApiException();
73         try {
74             saveStartDate(from);
75         } catch (GigiApiException e) {
76             gae.mergeInto(e);
77         }
78         try {
79             String validity = r.getParameter("validity");
80             if (validity != null) {
81                 checkValidityLength(validity);
82                 val = validity;
83             }
84         } catch (GigiApiException e) {
85             gae.mergeInto(e);
86         }
87         if ( !gae.isEmpty()) {
88             throw gae;
89         }
90
91     }
92
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.");
97             }
98             String num = newval.substring(0, newval.length() - 1);
99             try {
100                 int len = Integer.parseInt(num);
101                 if (len <= 0) {
102                     throw new GigiApiException("The validity interval entered is invalid.");
103                 }
104             } catch (NumberFormatException e) {
105                 throw new GigiApiException("The validity interval entered is invalid.");
106             }
107         } else {
108             try {
109                 DateSelector.getDateFormat().parse(newval);
110             } catch (ParseException e) {
111                 throw new GigiApiException("The validity interval entered is invalid.");
112             }
113         }
114     }
115
116     private void saveStartDate(String from) throws GigiApiException {
117         if (from == null || "now".equals(from)) {
118             this.from = null;
119         } else {
120             try {
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.");
124             }
125         }
126     }
127
128     public Date getFrom() {
129         return from;
130     }
131
132     public String getTo() {
133         return val;
134     }
135
136 }