1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.PrintWriter;
6 import java.util.Calendar;
7 import java.util.HashMap;
9 import java.util.Properties;
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;
17 import org.cacert.gigi.database.DatabaseConnection;
18 import org.cacert.gigi.email.EmailProvider;
19 import org.cacert.gigi.output.Outputable;
20 import org.cacert.gigi.output.Template;
21 import org.cacert.gigi.pages.LoginPage;
22 import org.cacert.gigi.pages.MainPage;
23 import org.cacert.gigi.pages.Page;
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.cacert.gigi.util.ServerConstants;
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 Template 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(MailOverview.DEFAULT_PATH, new MailOverview(
56 "My email addresses"));
57 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
58 baseTemplate = new Template(new InputStreamReader(
59 Gigi.class.getResourceAsStream("Gigi.templ")));
64 protected void service(final HttpServletRequest req,
65 final HttpServletResponse resp) throws ServletException,
68 if (req.getHeader("Origin") != null) {
69 resp.getWriter().println("No cross domain access allowed.");
72 HttpSession hs = req.getSession();
73 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
75 hs.setAttribute(LOGGEDIN, null);
78 resp.sendRedirect("/");
82 final Page p = getPage(req.getPathInfo());
85 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
86 String request = req.getPathInfo();
87 request = request.split("\\?")[0];
88 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
89 resp.sendRedirect("/login");
92 if (p.beforeTemplate(req, resp)) {
95 HashMap<String, Object> vars = new HashMap<String, Object>();
97 resp.setContentType("text/html; charset=utf-8");
98 Outputable content = new Outputable() {
101 public void output(PrintWriter out, Language l,
102 Map<String, Object> vars) {
104 if (req.getMethod().equals("POST")) {
109 } catch (IOException e) {
115 vars.put("title", p.getTitle());
116 vars.put("static", ServerConstants.getStaticHostNamePort());
117 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
118 vars.put("content", content);
119 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
121 resp.sendError(404, "Page not found.");
125 private Page getPage(String pathInfo) {
126 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
127 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
129 Page page = pages.get(pathInfo);
133 page = pages.get(pathInfo + "/*");
137 int idx = pathInfo.lastIndexOf('/');
138 pathInfo = pathInfo.substring(0, idx);
140 page = pages.get(pathInfo + "/*");
148 public static void addXSSHeaders(HttpServletResponse hsr) {
149 hsr.addHeader("Access-Control-Allow-Origin",
150 "http://cacert.org https://localhost");
151 hsr.addHeader("Access-Control-Max-Age", "60");
152 hsr.addHeader("Content-Security-Policy", "default-src 'self' https://"
153 + ServerConstants.getStaticHostNamePort()
154 + ";frame-ancestors 'none'");
155 // ;report-uri https://felix.dogcraft.de/report.php