]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Merge branch 'autotesting'
[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 import java.util.Properties;
11
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;
17
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.TestSecure;
24 import org.cacert.gigi.pages.Verify;
25 import org.cacert.gigi.pages.account.MailCertificates;
26 import org.cacert.gigi.pages.account.MyDetails;
27 import org.cacert.gigi.pages.main.RegisterPage;
28 import org.eclipse.jetty.util.log.Log;
29
30 public class Gigi extends HttpServlet {
31         public static final String LOGGEDIN = "loggedin";
32         public static final String USER = "user";
33         private static final long serialVersionUID = -6386785421902852904L;
34         private String[] baseTemplate;
35         private HashMap<String, Page> pages = new HashMap<String, Page>();
36
37         public Gigi(Properties conf) {
38                 EmailProvider.init(conf);
39                 DatabaseConnection.init(conf);
40         }
41         @Override
42         public void init() throws ServletException {
43                 pages.put("/login", new LoginPage("CACert - Login"));
44                 pages.put("/", new MainPage("CACert - Home"));
45                 pages.put("/secure", new TestSecure());
46                 pages.put(Verify.PATH, new Verify());
47                 pages.put(MailCertificates.PATH, new MailCertificates());
48                 pages.put(MyDetails.PATH, new MyDetails());
49                 pages.put(RegisterPage.PATH, new RegisterPage());
50                 String templ = "";
51                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(
52                                 new FileInputStream(new File("templates/base.html"))))) {
53                         String tmp;
54                         while ((tmp = reader.readLine()) != null) {
55                                 templ += tmp;
56                         }
57                         baseTemplate = templ.split("\\$content\\$");
58                 } catch (Exception e) {
59                         Log.getLogger(Gigi.class).warn("Error loading template!", e);
60                 }
61                 super.init();
62
63         }
64         @Override
65         protected void service(HttpServletRequest req, HttpServletResponse resp)
66                         throws ServletException, IOException {
67                 HttpSession hs = req.getSession();
68                 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
69                         if (hs != null) {
70                                 hs.setAttribute(LOGGEDIN, null);
71                                 hs.invalidate();
72                         }
73                         resp.sendRedirect("/");
74                         return;
75                 }
76
77                 if (pages.containsKey(req.getPathInfo())) {
78                         Page p = pages.get(req.getPathInfo());
79                         if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
80                                 String request = req.getPathInfo();
81                                 request = request.split("\\?")[0];
82                                 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
83                                 resp.sendRedirect("/login");
84                                 return;
85                         }
86                         if (p.beforeTemplate(req, resp)) {
87                                 return;
88                         }
89
90                         String b0 = baseTemplate[0];
91                         b0 = makeDynTempl(b0, p);
92                         resp.setContentType("text/html; charset=utf-8");
93                         resp.getWriter().print(b0);
94                         if (req.getMethod().equals("POST")) {
95                                 p.doPost(req, resp);
96                         } else {
97                                 p.doGet(req, resp);
98                         }
99                         String b1 = baseTemplate[1];
100                         b1 = makeDynTempl(b1, p);
101                         resp.getWriter().print(b1);
102                 } else {
103                         resp.sendError(404, "Page not found.");
104                 }
105
106         }
107         private String makeDynTempl(String in, Page p) {
108                 int year = Calendar.getInstance().get(Calendar.YEAR);
109                 in = in.replaceAll("\\$title\\$", p.getTitle());
110                 in = in.replaceAll("\\$year\\$", year + "");
111                 return in;
112         }
113
114 }