1 package org.cacert.gigi.output;
3 import java.io.PrintWriter;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
8 import java.util.TimeZone;
10 import javax.servlet.http.HttpServletRequest;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.util.HTMLEncoder;
16 public class CertificateValiditySelector implements Outputable {
18 private static ThreadLocal<SimpleDateFormat> fmt = new ThreadLocal<>();
20 private static final int DAY = 1000 * 60 * 60 * 24;
24 private String val = "2y";
26 public CertificateValiditySelector() {
30 public static SimpleDateFormat getDateFormat() {
31 SimpleDateFormat local = fmt.get();
33 local = new SimpleDateFormat("yyyy-MM-dd");
34 local.setTimeZone(TimeZone.getTimeZone("UTC"));
41 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
42 out.print("<select name='validFrom'><option value='now'");
44 out.print(" selected='selected'");
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='");
56 if (from != null && from.getTime() == date) {
57 out.print(" selected='selected'");
61 out.println("</option>");
63 out.println("</select>");
65 out.print("<input type='text' name='validity' value='");
66 out.print(HTMLEncoder.encodeHTML(val));
75 private long getCurrentDayBase() {
76 long base = System.currentTimeMillis();
82 public void update(HttpServletRequest r) throws GigiApiException {
83 String from = r.getParameter("validFrom");
85 GigiApiException gae = new GigiApiException();
88 } catch (GigiApiException e) {
92 String validity = r.getParameter("validity");
93 if (validity != null) {
94 checkValidityLength(validity);
97 } catch (GigiApiException e) {
100 if ( !gae.isEmpty()) {
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.");
111 String num = newval.substring(0, newval.length() - 1);
113 int len = Integer.parseInt(num);
115 throw new GigiApiException("The validity interval entered is invalid.");
117 } catch (NumberFormatException e) {
118 throw new GigiApiException("The validity interval entered is invalid.");
122 getDateFormat().parse(newval);
123 } catch (ParseException e) {
124 throw new GigiApiException("The validity interval entered is invalid.");
129 private void saveStartDate(String from) throws GigiApiException {
130 if (from == null || "now".equals(from)) {
134 this.from = new Date(getDateFormat().parse(from).getTime());
135 } catch (ParseException e) {
136 throw new GigiApiException("The validity start date entered is invalid.");
141 public Date getFrom() {
145 public String getTo() {