1 package org.cacert.gigi;
3 import java.io.BufferedReader;
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 import java.util.Properties;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.email.EmailProvider;
20 import org.cacert.gigi.pages.LoginPage;
21 import org.cacert.gigi.pages.MainPage;
22 import org.cacert.gigi.pages.Page;
23 import org.cacert.gigi.pages.PolicyRedir;
24 import org.cacert.gigi.pages.TestSecure;
25 import org.cacert.gigi.pages.Verify;
26 import org.cacert.gigi.pages.account.MailAdd;
27 import org.cacert.gigi.pages.account.MailCertificates;
28 import org.cacert.gigi.pages.account.MailOverview;
29 import org.cacert.gigi.pages.account.MyDetails;
30 import org.cacert.gigi.pages.main.RegisterPage;
31 import org.cacert.gigi.pages.wot.AssurePage;
32 import org.eclipse.jetty.util.log.Log;
34 public class Gigi extends HttpServlet {
35 public static final String LOGGEDIN = "loggedin";
36 public static final String USER = "user";
37 private static final long serialVersionUID = -6386785421902852904L;
38 private String[] baseTemplate;
39 private HashMap<String, Page> pages = new HashMap<String, Page>();
41 public Gigi(Properties conf) {
42 EmailProvider.init(conf);
43 DatabaseConnection.init(conf);
46 public void init() throws ServletException {
47 pages.put("/login", new LoginPage("CACert - Login"));
48 pages.put("/", new MainPage("CACert - Home"));
49 pages.put("/secure", new TestSecure());
50 pages.put(Verify.PATH, new Verify());
51 pages.put(AssurePage.PATH + "/*", new AssurePage());
52 pages.put(MailCertificates.PATH, new MailCertificates());
53 pages.put(MyDetails.PATH, new MyDetails());
54 pages.put(RegisterPage.PATH, new RegisterPage());
55 pages.put(PolicyRedir.PATH, new PolicyRedir());
56 pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
57 "My email addresses"));
58 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
60 try (BufferedReader reader = new BufferedReader(new InputStreamReader(
61 new FileInputStream(new File("templates/base.html"))))) {
63 while ((tmp = reader.readLine()) != null) {
66 baseTemplate = templ.split("\\$content\\$");
67 } catch (Exception e) {
68 Log.getLogger(Gigi.class).warn("Error loading template!", e);
74 protected void service(HttpServletRequest req, HttpServletResponse resp)
75 throws ServletException, IOException {
76 HttpSession hs = req.getSession();
77 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
79 hs.setAttribute(LOGGEDIN, null);
82 resp.sendRedirect("/");
86 Page p = getPage(req.getPathInfo());
89 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
90 String request = req.getPathInfo();
91 request = request.split("\\?")[0];
92 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
93 resp.sendRedirect("/login");
96 if (p.beforeTemplate(req, resp)) {
100 String b0 = baseTemplate[0];
101 b0 = makeDynTempl(b0, p);
102 resp.setContentType("text/html; charset=utf-8");
103 resp.getWriter().print(b0);
104 if (req.getMethod().equals("POST")) {
109 String b1 = baseTemplate[1];
110 b1 = makeDynTempl(b1, p);
111 resp.getWriter().print(b1);
113 resp.sendError(404, "Page not found.");
117 private Page getPage(String pathInfo) {
118 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
119 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
121 Page page = pages.get(pathInfo);
125 page = pages.get(pathInfo + "/*");
129 int idx = pathInfo.lastIndexOf('/');
130 pathInfo = pathInfo.substring(0, idx);
132 page = pages.get(pathInfo + "/*");
139 private String makeDynTempl(String in, Page p) {
140 int year = Calendar.getInstance().get(Calendar.YEAR);
141 in = in.replaceAll("\\$title\\$", p.getTitle());
142 in = in.replaceAll("\\$year\\$", year + "");