]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Implement prititive "show email certificates"
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.util.Calendar;
9 import java.util.HashMap;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
16
17 import org.cacert.gigi.pages.LoginPage;
18 import org.cacert.gigi.pages.MainPage;
19 import org.cacert.gigi.pages.Page;
20 import org.cacert.gigi.pages.TestSecure;
21 import org.cacert.gigi.pages.account.MailCertificates;
22 import org.cacert.gigi.pages.account.MyDetails;
23 import org.cacert.gigi.pages.main.RegisterPage;
24 import org.eclipse.jetty.util.log.Log;
25
26 public class Gigi extends HttpServlet {
27         public static final String LOGGEDIN = "loggedin";
28         public static final String USER = "user";
29         private static final long serialVersionUID = -6386785421902852904L;
30         private String[] baseTemplate;
31         private HashMap<String, Page> pages = new HashMap<String, Page>();
32
33         @Override
34         public void init() throws ServletException {
35                 pages.put("/login", new LoginPage("CACert - Login"));
36                 pages.put("/", new MainPage("CACert - Home"));
37                 pages.put("/secure", new TestSecure());
38                 pages.put(MailCertificates.PATH, new MailCertificates());
39                 pages.put(MyDetails.PATH, new MyDetails());
40                 pages.put(RegisterPage.PATH, new RegisterPage());
41                 String templ = "";
42                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(
43                                 new FileInputStream(new File("templates/base.html"))))) {
44                         String tmp;
45                         while ((tmp = reader.readLine()) != null) {
46                                 templ += tmp;
47                         }
48                         baseTemplate = templ.split("\\$content\\$");
49                 } catch (Exception e) {
50                         Log.getLogger(Gigi.class).warn("Error loading template!", e);
51                 }
52                 super.init();
53
54         }
55         @Override
56         protected void service(HttpServletRequest req, HttpServletResponse resp)
57                         throws ServletException, IOException {
58                 HttpSession hs = req.getSession();
59                 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
60                         if (hs != null) {
61                                 hs.setAttribute(LOGGEDIN, null);
62                                 hs.invalidate();
63                         }
64                         resp.sendRedirect("/");
65                         return;
66                 }
67
68                 if (pages.containsKey(req.getPathInfo())) {
69                         Page p = pages.get(req.getPathInfo());
70                         if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
71                                 resp.sendRedirect("/login");
72                                 return;
73                         }
74
75                         String b0 = baseTemplate[0];
76                         b0 = makeDynTempl(b0, p);
77                         resp.setContentType("text/html; charset=utf-8");
78                         resp.getWriter().print(b0);
79                         if (req.getMethod().equals("POST")) {
80                                 p.doPost(req, resp);
81                         } else {
82                                 p.doGet(req, resp);
83                         }
84                         String b1 = baseTemplate[1];
85                         b1 = makeDynTempl(b1, p);
86                         resp.getWriter().print(b1);
87                 } else {
88                         resp.sendError(404, "Page not found.");
89                 }
90
91         }
92         private String makeDynTempl(String in, Page p) {
93                 int year = Calendar.getInstance().get(Calendar.YEAR);
94                 in = in.replaceAll("\\$title\\$", p.getTitle());
95                 in = in.replaceAll("\\$year\\$", year + "");
96                 return in;
97         }
98
99 }