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