]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Fixed / path
[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.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;
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 String[] 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(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"));
59                 String templ = "";
60                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(
61                                 new FileInputStream(new File("templates/base.html"))))) {
62                         String tmp;
63                         while ((tmp = reader.readLine()) != null) {
64                                 templ += tmp;
65                         }
66                         baseTemplate = templ.split("\\$content\\$");
67                 } catch (Exception e) {
68                         Log.getLogger(Gigi.class).warn("Error loading template!", e);
69                 }
70                 super.init();
71
72         }
73         @Override
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")) {
78                         if (hs != null) {
79                                 hs.setAttribute(LOGGEDIN, null);
80                                 hs.invalidate();
81                         }
82                         resp.sendRedirect("/");
83                         return;
84                 }
85
86                 Page p = getPage(req.getPathInfo());
87                 if (p != null) {
88
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");
94                                 return;
95                         }
96                         if (p.beforeTemplate(req, resp)) {
97                                 return;
98                         }
99
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")) {
105                                 p.doPost(req, resp);
106                         } else {
107                                 p.doGet(req, resp);
108                         }
109                         String b1 = baseTemplate[1];
110                         b1 = makeDynTempl(b1, p);
111                         resp.getWriter().print(b1);
112                 } else {
113                         resp.sendError(404, "Page not found.");
114                 }
115
116         }
117         private Page getPage(String pathInfo) {
118                 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
119                         pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
120                 }
121                 Page page = pages.get(pathInfo);
122                 if (page != null) {
123                         return page;
124                 }
125                 page = pages.get(pathInfo + "/*");
126                 if (page != null) {
127                         return page;
128                 }
129                 int idx = pathInfo.lastIndexOf('/');
130                 pathInfo = pathInfo.substring(0, idx);
131
132                 page = pages.get(pathInfo + "/*");
133                 if (page != null) {
134                         return page;
135                 }
136                 return null;
137
138         }
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 + "");
143                 return in;
144         }
145
146 }