Coverage Report - org.paneris.bibliomania.BibliomaniaLogin
 
Classes in this File Line Coverage Branch Coverage Complexity
BibliomaniaLogin
0%
0/87
0%
0/34
2.812
BibliomaniaLogin$1
0%
0/16
0%
0/2
2.812
BibliomaniaLogin$2
0%
0/4
N/A
2.812
BibliomaniaLogin$3
0%
0/3
N/A
2.812
BibliomaniaLogin$4
0%
0/3
N/A
2.812
BibliomaniaLogin$Handler
0%
0/7
0%
0/2
2.812
 
 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.Email;
 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  0
 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  0
     it.setLogicalDatabase("bibliomania");
 39  0
   }
 40  
 
 41  
   public String loginTemplate(String name) {
 42  0
     return bibliomaniaTemplate("bibliomanialogin/" + name);
 43  
   }
 44  
 
 45  
   public static class Handler extends LoginHandler {
 46  
 
 47  
     public Handler(TemplateServlet s) {
 48  0
       super(s);
 49  0
     }
 50  
 
 51  
     public String getLogin(ServletTemplateContext context) {
 52  0
       String it = context.getFormField("field_login");
 53  0
       return it == null ? context.getFormField("field_email") : it;
 54  
     }
 55  
   
 56  
     protected String loginTemplate(String name) {
 57  0
       return bibliomaniaTemplate("bibliomanialogin/" + name);
 58  
     }
 59  
 
 60  
     protected String usernameUnknownTemplate() {
 61  0
       return loginTemplate("Login");
 62  
     }
 63  
 
 64  
     protected String passwordIncorrectTemplate() {
 65  0
       return loginTemplate("Login");
 66  
     }
 67  
   }
 68  
 
 69  0
   Handler loginHandler = null;
 70  
 
 71  
   protected Handler getLoginHandler() {
 72  0
     if (loginHandler == null)
 73  0
       loginHandler = new Handler(this);
 74  
 
 75  0
     return loginHandler;
 76  
   }
 77  
 
 78  
   protected String handleRemind(Melati melati, WebContext context)
 79  
       throws Exception {
 80  
 
 81  0
     getLoginHandler().setupContext(melati.getServletTemplateContext());
 82  
 
 83  0
     final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
 84  
 
 85  0
     UserTable users = (UserTable)db.getUserTable();
 86  0
     String login = context.getForm("field_login");
 87  0
     if (login == null)
 88  0
       login = context.getForm("field_email");
 89  
 
 90  0
     User u = (User)users.getLoginColumn().firstWhereEq(login);
 91  0
     if (u == null)
 92  0
       u = (User)users.getEmailColumn().firstWhereEq(login);
 93  0
     final User user = u;
 94  
 
 95  0
     if (user == null)
 96  0
       context.put("loginUnknown", Boolean.TRUE);
 97  
     else {
 98  0
       final String[] email = new String[1];
 99  0
       ByteArrayOutputStream buf = new ByteArrayOutputStream();
 100  0
       final FastWriter bufp = new FastWriter(db.getWebMacro().getBroker(),
 101  
                                              buf, "UTF8");
 102  
 
 103  0
       PoemThread.withAccessToken(
 104  
           AccessToken.root,
 105  0
           new PoemTask() {
 106  
             public void run() {
 107  0
               email[0] = user.getEmail();
 108  0
               if (email[0] == null)
 109  0
                 return;
 110  
 
 111  0
               email[0] = email[0].replace("\n\r", "  ");
 112  0
               final org.webmacro.Template messageTemplate =
 113  
                   db.getPasswordReminderTemplate();
 114  0
               final Context msgContext = db.getWebMacro().getContext();
 115  
 
 116  0
               msgContext.put("db", db);
 117  0
               msgContext.put("user", user);
 118  
 
 119  
               try {
 120  0
                 messageTemplate.write(bufp.getOutputStream(), msgContext);
 121  
               }
 122  0
               catch (WebMacroException e) {
 123  0
                 throw new UnexpectedExceptionException(
 124  
                     "Error expanding password reminder message", e);
 125  
               }
 126  0
               catch (IOException e) {
 127  0
                 throw new UnexpectedExceptionException(
 128  
                     "Error expanding password reminder message", e);
 129  0
               }
 130  0
             }
 131  
           });
 132  
 
 133  0
       if (email[0] == null)
 134  0
         context.put("reminded", "(no address)");
 135  
       else {
 136  0
         bufp.flush();
 137  
 
 138  0
         String from = db.getPasswordReminderFrom().replace("\n\r", "  ");
 139  
 /*
 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  0
         Email.send(db.getSmtpServer(), from, email[0], null, "Your bibliomania.com password", buf.toString().trim());
 153  0
         context.put("reminded", email[0]);
 154  
       }
 155  
     }
 156  
 
 157  0
     return loginTemplate("Login");
 158  
   }
 159  
 
 160  
   protected String handleRegister(Melati melati, WebContext context,
 161  
                                   User template)
 162  
       throws Exception {
 163  
 
 164  0
     final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
 165  0
     UserTable userTable = (UserTable)db.getUserTable();
 166  0
     ServletTemplateContext tc = melati.getServletTemplateContext();
 167  0
     String email = context.getForm("field_email");
 168  0
     if (email == null) email = context.getForm("field_login");
 169  
 
 170  0
     context.put("email", new Field(email,
 171  
                 userTable.getEmailColumn()).withNullable(false));
 172  0
     context.put("name", new Field(Form.getFieldNulled(tc,"field_name"),
 173  
                 new BaseFieldAttributes((FieldAttributes)userTable.getNameColumn(), true)));
 174  0
     context.put("password", 
 175  
                 new Field(Form.getFieldNulled(tc,"field_password"),
 176  
                 userTable.getPasswordColumn()));
 177  0
     context.put("age", new Field(Form.getIntegerField(tc,"field_age"),
 178  
                                  userTable.getAgeColumn()));
 179  0
     context.put("sex", new Field(Form.getIntegerField(tc,"field_sex"),
 180  
                                  userTable.getSexColumn()));
 181  0
     context.put("country",
 182  
                 new Field(
 183  
                     Form.getIntegerField(tc,"field_country"),
 184  
                     userTable.getCountryColumn()));
 185  0
     context.put("fulltimeeducation",
 186  
                 new Field(Form.getBooleanField(
 187  
                               tc, "field_fulltimeeducation"),
 188  
                               userTable.getFulltimeeducationColumn()));
 189  0
     context.put("wantemailalerts",
 190  
                 new Field(Form.getBooleanField(tc,
 191  
                                                     "field_wantemailalerts"),
 192  
                           userTable.getWantemailalertsColumn()));
 193  0
     context.put("wantspam",
 194  
                 new Field(Form.getBooleanField(tc, "field_wantspam"),
 195  
                           userTable.getWantspamColumn()));
 196  
 
 197  0
     if (template == null) {
 198  0
       final User[] t = new User[1];
 199  0
       PoemThread.withAccessToken(
 200  
           AccessToken.root,
 201  0
           new PoemTask() {
 202  
             public void run() {
 203  0
               t[0] = (User)db.getTemplateRegisterUser().duplicated();
 204  0
               t[0].setName_unsafe(null);
 205  0
             }
 206  
           });
 207  0
       template = t[0];
 208  
     }
 209  
     
 210  0
     return loginTemplate("Register");
 211  
   }
 212  
 
 213  
   protected String handleRegisterFinish(final Melati melati,
 214  
                                         final WebContext context)
 215  
       throws Exception {
 216  0
     final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
 217  0
     final UserTable userTable = (UserTable)db.getUserTable();
 218  
 
 219  0
     String login = context.getForm("field_login");
 220  0
     if (login == null)
 221  0
       login = context.getForm("field_email");
 222  
 
 223  0
     final User existing =
 224  
         (User)userTable.getLoginColumn().firstWhereEq(login);
 225  
 
 226  0
     if (existing == null) {
 227  0
       final User fromForm = (User)userTable.newPersistent();
 228  0
       Form.extractFields(melati.getServletTemplateContext(), fromForm);
 229  0
       fromForm.generateDefaults();
 230  0
       String email = fromForm.getEmail().trim();
 231  
 /*
 232  
       if (fromForm.getName() == null || fromForm.getName().equals("")) {
 233  
         String name = email;
 234  
         int index = email.indexOf("@");
 235  
         if (index != -1) name = email.substring(0,index);
 236  
         fromForm.setName(name);
 237  
       }
 238  
 */
 239  0
       if (email != null) fromForm.setLogin(email);
 240  0
       PoemThread.withAccessToken(
 241  
           AccessToken.root,
 242  0
           new PoemTask() {
 243  
             public void run() {
 244  0
               userTable.create(fromForm);
 245  0
             }
 246  
           });
 247  
           // gosh, it should be easier to do this!
 248  0
       HttpSession session = context.getSession();
 249  0
       session.removeAttribute(Login.TRIGGERING_REQUEST_PARAMETERS);
 250  0
       session.removeAttribute(Login.TRIGGERING_EXCEPTION);
 251  0
       context.remove("continuationURL");
 252  0
       return getLoginHandler().loginSuccessfullyAs(melati, 
 253  
                                         melati.getServletTemplateContext(), fromForm);
 254  
     }
 255  0
     else if (existing.getPassword_unsafe().equals(
 256  
                  context.getForm("field_password"))) {
 257  0
       PoemThread.withAccessToken(
 258  
           existing,
 259  0
           new PoemTask() {
 260  
             public void run() {
 261  0
               Form.extractFields(melati.getServletTemplateContext(), existing);
 262  0
             }
 263  
           });
 264  
 
 265  0
       return getLoginHandler().loginSuccessfullyAs(melati, 
 266  
                                         melati.getServletTemplateContext(), existing);
 267  
     }
 268  
     else {
 269  0
       User fromForm = (User)userTable.newPersistent();
 270  0
       Form.extractFields(melati.getServletTemplateContext(), fromForm);
 271  0
       context.put("loginInUse", Boolean.TRUE);
 272  0
       return handleRegister(melati, context, fromForm);
 273  
     }
 274  
   }
 275  
 
 276  
   protected String bibliomaniaHandle(Melati melati, final WebContext context)
 277  
       throws Exception {
 278  0
     String action = context.getForm("action");
 279  
 
 280  0
     if (action == null) action = "";
 281  
 
 282  0
     String login = context.getForm("field_login");
 283  0
     if (login == null)
 284  0
       login = context.getForm("field_email");
 285  
 
 286  0
     if (action.equals("remind") && login != null)
 287  0
       return handleRemind(melati, context);
 288  0
     else if (action.equals("register"))
 289  0
       return handleRegister(melati, context, null);
 290  0
     else if (action.equals("create"))
 291  0
       return handleRegisterFinish(melati, context);
 292  
     else
 293  0
       return getLoginHandler().doTemplateRequest(melati,
 294  
                                                  melati.getServletTemplateContext());
 295  
   }
 296  
 }