]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
b6aa90ebd5386b26f3742d41e7090a10829a297b
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
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;
8 import java.util.Map;
9 import java.util.Properties;
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.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;
33
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>();
40
41         public Gigi(Properties conf) {
42                 EmailProvider.init(conf);
43                 DatabaseConnection.init(conf);
44         }
45         @Override
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")));
60                 super.init();
61
62         }
63         @Override
64         protected void service(final HttpServletRequest req,
65                         final HttpServletResponse resp) throws ServletException,
66                         IOException {
67                 addXSSHeaders(resp);
68                 if (req.getHeader("Origin") != null) {
69                         resp.getWriter().println("No cross domain access allowed.");
70                         return;
71                 }
72                 HttpSession hs = req.getSession();
73                 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
74                         if (hs != null) {
75                                 hs.setAttribute(LOGGEDIN, null);
76                                 hs.invalidate();
77                         }
78                         resp.sendRedirect("/");
79                         return;
80                 }
81
82                 final Page p = getPage(req.getPathInfo());
83                 if (p != null) {
84
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");
90                                 return;
91                         }
92                         if (p.beforeTemplate(req, resp)) {
93                                 return;
94                         }
95                         HashMap<String, Object> vars = new HashMap<String, Object>();
96
97                         resp.setContentType("text/html; charset=utf-8");
98                         Outputable content = new Outputable() {
99
100                                 @Override
101                                 public void output(PrintWriter out, Language l,
102                                                 Map<String, Object> vars) {
103                                         try {
104                                                 if (req.getMethod().equals("POST")) {
105                                                         p.doPost(req, resp);
106                                                 } else {
107                                                         p.doGet(req, resp);
108                                                 }
109                                         } catch (IOException e) {
110                                                 e.printStackTrace();
111                                         }
112
113                                 }
114                         };
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);
120                 } else {
121                         resp.sendError(404, "Page not found.");
122                 }
123
124         }
125         private Page getPage(String pathInfo) {
126                 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
127                         pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
128                 }
129                 Page page = pages.get(pathInfo);
130                 if (page != null) {
131                         return page;
132                 }
133                 page = pages.get(pathInfo + "/*");
134                 if (page != null) {
135                         return page;
136                 }
137                 int idx = pathInfo.lastIndexOf('/');
138                 pathInfo = pathInfo.substring(0, idx);
139
140                 page = pages.get(pathInfo + "/*");
141                 if (page != null) {
142                         return page;
143                 }
144                 return null;
145
146         }
147
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
156
157         }
158 }