1 package org.paneris.bibliomania;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6
7 import javax.servlet.http.HttpSession;
8
9 import org.melati.Melati;
10 import org.melati.servlet.Form;
11 import org.melati.login.Login;
12 import org.melati.login.LoginHandler;
13 import org.melati.poem.AccessToken;
14 import org.melati.poem.BaseFieldAttributes;
15 import org.melati.poem.Field;
16 import org.melati.poem.FieldAttributes;
17 import org.melati.poem.PoemTask;
18 import org.melati.poem.PoemThread;
19 import org.melati.servlet.TemplateServlet;
20 import org.melati.template.ServletTemplateContext;
21 import org.melati.util.StringUtils;
22 import org.melati.util.UnexpectedExceptionException;
23 import org.webmacro.Context;
24 import org.webmacro.FastWriter;
25 import org.webmacro.WebMacroException;
26 import org.webmacro.servlet.WebContext;
27
28 import sun.net.smtp.SmtpClient;
29
30 public class BibliomaniaLogin extends BibliomaniaServlet {
31
32
33
34
35 private static final long serialVersionUID = 1L;
36
37 protected void fillContext(Melati melati, BibContext it) {
38 it.setLogicalDatabase("bibliomania");
39 }
40
41 public String loginTemplate(String name) {
42 return bibliomaniaTemplate("bibliomanialogin/" + name);
43 }
44
45 public static class Handler extends LoginHandler {
46
47 public Handler(TemplateServlet s) {
48 super(s);
49 }
50
51 public String getLogin(ServletTemplateContext context) {
52 String it = context.getForm("field_login");
53 return it == null ? context.getForm("field_email") : it;
54 }
55
56 protected String loginTemplate(String name) {
57 return bibliomaniaTemplate("bibliomanialogin/" + name);
58 }
59
60 protected String usernameUnknownTemplate() {
61 return loginTemplate("Login");
62 }
63
64 protected String passwordIncorrectTemplate() {
65 return loginTemplate("Login");
66 }
67 }
68
69 Handler loginHandler = null;
70
71 protected Handler getLoginHandler() {
72 if (loginHandler == null)
73 loginHandler = new Handler(this);
74
75 return loginHandler;
76 }
77
78 protected String handleRemind(Melati melati, WebContext context)
79 throws Exception {
80
81 getLoginHandler().setupContext(melati.getServletTemplateContext());
82
83 final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
84
85 UserTable users = (UserTable)db.getUserTable();
86 String login = context.getForm("field_login");
87 if (login == null)
88 login = context.getForm("field_email");
89
90 User u = (User)users.getLoginColumn().firstWhereEq(login);
91 if (u == null)
92 u = (User)users.getEmailColumn().firstWhereEq(login);
93 final User user = u;
94
95 if (user == null)
96 context.put("loginUnknown", Boolean.TRUE);
97 else {
98 final String[] email = new String[1];
99 ByteArrayOutputStream buf = new ByteArrayOutputStream();
100 final FastWriter bufp = new FastWriter(db.getWebMacro().getBroker(),
101 buf, "UTF8");
102
103 PoemThread.withAccessToken(
104 AccessToken.root,
105 new PoemTask() {
106 public void run() {
107 email[0] = user.getEmail();
108 if (email[0] == null)
109 return;
110
111 email[0] = StringUtils.tr(email[0], "\n\r", " ");
112 final org.webmacro.Template messageTemplate =
113 db.getPasswordReminderTemplate();
114 final Context msgContext = db.getWebMacro().getContext();
115
116 msgContext.put("db", db);
117 msgContext.put("user", user);
118
119 try {
120 messageTemplate.write(bufp.getOutputStream(), msgContext);
121 }
122 catch (WebMacroException e) {
123 throw new UnexpectedExceptionException(
124 "Error expanding password reminder message", e);
125 }
126 catch (IOException e) {
127 throw new UnexpectedExceptionException(
128 "Error expanding password reminder message", e);
129 }
130 }
131 });
132
133 if (email[0] == null)
134 context.put("reminded", "(no address)");
135 else {
136 bufp.flush();
137
138 String from = StringUtils.tr(db.getPasswordReminderFrom(),
139 "\n\r", " ");
140 SmtpClient smtp = new SmtpClient(db.getSmtpServer());
141 try {
142 smtp.from(from);
143 smtp.to(email[0]);
144 PrintWriter data = new PrintWriter(smtp.startMessage());
145 data.println(buf.toString().trim());
146 data.flush();
147 }
148 finally {
149 smtp.closeServer();
150 }
151
152 context.put("reminded", email[0]);
153 }
154 }
155
156 return loginTemplate("Login");
157 }
158
159 protected String handleRegister(Melati melati, WebContext context,
160 User template)
161 throws Exception {
162
163 final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
164 UserTable userTable = (UserTable)db.getUserTable();
165 ServletTemplateContext tc = melati.getServletTemplateContext();
166 String email = context.getForm("field_email");
167 if (email == null) email = context.getForm("field_login");
168
169 context.put("email", new Field(email,
170 userTable.getEmailColumn()).withNullable(false));
171 context.put("name", new Field(Form.getFormNulled(tc,"field_name"),
172 new BaseFieldAttributes((FieldAttributes)userTable.getNameColumn(), true)));
173 context.put("password",
174 new Field(Form.getFormNulled(tc,"field_password"),
175 userTable.getPasswordColumn()));
176 context.put("age", new Field(Form.getIntegerField(tc,"field_age"),
177 userTable.getAgeColumn()));
178 context.put("sex", new Field(Form.getIntegerField(tc,"field_sex"),
179 userTable.getSexColumn()));
180 context.put("country",
181 new Field(
182 Form.getIntegerField(tc,"field_country"),
183 userTable.getCountryColumn()));
184 context.put("fulltimeeducation",
185 new Field(Form.getBooleanField(
186 tc, "field_fulltimeeducation"),
187 userTable.getFulltimeeducationColumn()));
188 context.put("wantemailalerts",
189 new Field(Form.getBooleanField(tc,
190 "field_wantemailalerts"),
191 userTable.getWantemailalertsColumn()));
192 context.put("wantspam",
193 new Field(Form.getBooleanField(tc, "field_wantspam"),
194 userTable.getWantspamColumn()));
195
196 if (template == null) {
197 final User[] t = new User[1];
198 PoemThread.withAccessToken(
199 AccessToken.root,
200 new PoemTask() {
201 public void run() {
202 t[0] = (User)db.getTemplateRegisterUser().duplicated();
203 t[0].setName_unsafe(null);
204 }
205 });
206 template = t[0];
207 }
208
209 return loginTemplate("Register");
210 }
211
212 protected String handleRegisterFinish(final Melati melati,
213 final WebContext context)
214 throws Exception {
215 final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
216 final UserTable userTable = (UserTable)db.getUserTable();
217
218 String login = context.getForm("field_login");
219 if (login == null)
220 login = context.getForm("field_email");
221
222 final User existing =
223 (User)userTable.getLoginColumn().firstWhereEq(login);
224
225 if (existing == null) {
226 final User fromForm = (User)userTable.newPersistent();
227 Form.extractFields(melati.getServletTemplateContext(), fromForm);
228 fromForm.generateDefaults();
229 String email = fromForm.getEmail().trim();
230
231
232
233
234
235
236
237
238 if (email != null) fromForm.setLogin(email);
239 PoemThread.withAccessToken(
240 AccessToken.root,
241 new PoemTask() {
242 public void run() {
243 userTable.create(fromForm);
244 }
245 });
246
247 HttpSession session = context.getSession();
248 session.removeAttribute(Login.TRIGGERING_REQUEST_PARAMETERS);
249 session.removeAttribute(Login.TRIGGERING_EXCEPTION);
250 context.remove("continuationURL");
251 return getLoginHandler().loginSuccessfullyAs(melati,
252 melati.getServletTemplateContext(), fromForm);
253 }
254 else if (existing.getPassword_unsafe().equals(
255 context.getForm("field_password"))) {
256 PoemThread.withAccessToken(
257 existing,
258 new PoemTask() {
259 public void run() {
260 Form.extractFields(melati.getServletTemplateContext(), existing);
261 }
262 });
263
264 return getLoginHandler().loginSuccessfullyAs(melati,
265 melati.getServletTemplateContext(), existing);
266 }
267 else {
268 User fromForm = (User)userTable.newPersistent();
269 Form.extractFields(melati.getServletTemplateContext(), fromForm);
270 context.put("loginInUse", Boolean.TRUE);
271 return handleRegister(melati, context, fromForm);
272 }
273 }
274
275 protected String bibliomaniaHandle(Melati melati, final WebContext context)
276 throws Exception {
277 String action = context.getForm("action");
278
279 if (action == null) action = "";
280
281 String login = context.getForm("field_login");
282 if (login == null)
283 login = context.getForm("field_email");
284
285 if (action.equals("remind") && login != null)
286 return handleRemind(melati, context);
287 else if (action.equals("register"))
288 return handleRegister(melati, context, null);
289 else if (action.equals("create"))
290 return handleRegisterFinish(melati, context);
291 else
292 return getLoginHandler().doTemplateRequest(melati,
293 melati.getServletTemplateContext());
294 }
295 }