]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/CertificateValiditySelector.java
[EMPTY] Organize all imports
[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.util.HTMLEncoder;
13
14 public class CertificateValiditySelector implements Outputable {
15
16     private static final long DAY = 1000 * 60 * 60 * 24;
17
18     private Date from;
19
20     private String val = "2y";
21
22     public CertificateValiditySelector() {
23
24     }
25
26     @Override
27     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
28         out.print("<select name='validFrom'><option value='now'");
29         if (from == null) {
30             out.print(" selected='selected'");
31         }
32         out.print(">");
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='");
40             out.print(d);
41             out.print("'");
42             if (from != null && from.getTime() == date) {
43                 out.print(" selected='selected'");
44             }
45             out.print(">");
46             out.print(d);
47             out.println("</option>");
48         }
49         out.println("</select>");
50
51         out.print("<input type='text' name='validity' value='");
52         out.print(HTMLEncoder.encodeHTML(val));
53         out.println("'>");
54
55         if (from == null) {
56             return;
57         }
58
59     }
60
61     private long getCurrentDayBase() {
62         long base = System.currentTimeMillis();
63         base -= base % DAY;
64         base += DAY;
65         return base;
66     }
67
68     public void update(HttpServletRequest r) throws GigiApiException {
69         String from = r.getParameter("validFrom");
70
71         GigiApiException gae = new GigiApiException();
72         try {
73             saveStartDate(from);
74         } catch (GigiApiException e) {
75             gae.mergeInto(e);
76         }
77         try {
78             String validity = r.getParameter("validity");
79             if (validity != null) {
80                 checkValidityLength(validity);
81                 val = validity;
82             }
83         } catch (GigiApiException e) {
84             gae.mergeInto(e);
85         }
86         if ( !gae.isEmpty()) {
87             throw gae;
88         }
89
90     }
91
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.");
96             }
97             String num = newval.substring(0, newval.length() - 1);
98             try {
99                 int len = Integer.parseInt(num);
100                 if (len <= 0) {
101                     throw new GigiApiException("The validity interval entered is invalid.");
102                 }
103             } catch (NumberFormatException e) {
104                 throw new GigiApiException("The validity interval entered is invalid.");
105             }
106         } else {
107             try {
108                 DateSelector.getDateFormat().parse(newval);
109             } catch (ParseException e) {
110                 throw new GigiApiException("The validity interval entered is invalid.");
111             }
112         }
113     }
114
115     private void saveStartDate(String from) throws GigiApiException {
116         if (from == null || "now".equals(from)) {
117             this.from = null;
118         } else {
119             try {
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.");
123             }
124         }
125     }
126
127     public Date getFrom() {
128         return from;
129     }
130
131     public String getTo() {
132         return val;
133     }
134
135 }