001 package org.hackystat.sensorbase.resource.registration; 002 003 import static org.hackystat.sensorbase.server.ServerProperties.ADMIN_EMAIL_KEY; 004 import static org.hackystat.sensorbase.server.ServerProperties.HOSTNAME_KEY; 005 006 import org.hackystat.sensorbase.mailer.Mailer; 007 import org.hackystat.sensorbase.resource.sensorbase.SensorBaseResource; 008 import org.hackystat.sensorbase.resource.users.jaxb.User; 009 import org.hackystat.utilities.email.ValidateEmailSyntax; 010 import org.restlet.Context; 011 import org.restlet.data.Form; 012 import org.restlet.data.MediaType; 013 import org.restlet.data.Request; 014 import org.restlet.data.Response; 015 import org.restlet.data.Status; 016 import org.restlet.resource.Representation; 017 import org.restlet.resource.StringRepresentation; 018 import org.restlet.resource.Variant; 019 020 /** 021 * Provides registration services for this SensorBase. Implements a simple web page for accepting a 022 * POSTed form containing an email address to register. Sends email with the password to this user. 023 * Note that the email address is always lower-cased regardless of how the user typed it in. 024 * 025 * @author Philip Johnson 026 * 027 */ 028 public class RegistrationResource extends SensorBaseResource { 029 030 /** 031 * The standard constructor. 032 * 033 * @param context The context. 034 * @param request The request object. 035 * @param response The response object. 036 */ 037 public RegistrationResource(Context context, Request request, Response response) { 038 super(context, request, response); 039 } 040 041 /** 042 * Returns a page providing a registration form. This requires no authorization. 043 * 044 * @param variant The representational variant requested. 045 * @return The representation. 046 */ 047 @Override 048 public Representation represent(Variant variant) { 049 String pageHtml = "<html>" + " <body>" + " Welcome to the Hackystat SensorBase." 050 + " <p>Please enter your email address below to register." 051 + " <p>A password to this SensorBase will be emailed to you. " 052 + " <form action=\"register\" method=\"POST\">" 053 + " <input name=\"email\" type=\"text\" size=\"15\"/> " 054 + " <input type=\"submit\" name=\"Submit\" value=\"Register\">" + " </form>" 055 + " </body>" + "</html>"; 056 Representation representation = new StringRepresentation(pageHtml); 057 representation.setMediaType(MediaType.TEXT_HTML); 058 return representation; 059 } 060 061 /** 062 * Indicate the POST method is supported. 063 * 064 * @return True. 065 */ 066 @Override 067 public boolean allowPost() { 068 return true; 069 } 070 071 /** 072 * Implement the POST method that registers a new user. We lower case the email address 073 * automatically. 074 * 075 * @param entity The email address to be registered. 076 */ 077 @Override 078 public void acceptRepresentation(Representation entity) { 079 // server.getLogger().info("Beginning registration."); 080 Form form = new Form(entity); 081 String email = form.getFirstValue("email"); 082 // Return Badness if we don't have the email attribute. 083 if (email == null || "".equals(email)) { 084 setStatusMiscError("Invalid registration request: empty email"); 085 return; 086 } 087 if (!ValidateEmailSyntax.isValid(email)) { 088 setStatusMiscError("Invalid registration request: email appears to be invalid."); 089 return; 090 } 091 try { 092 // Now try to register. 093 User user = super.userManager.registerUser(email); 094 super.projectManager.addDefaultProject(user); 095 // Now send the email to the (non-test) user and the hackystat admin. 096 Mailer mailer = Mailer.getInstance(); 097 String adminEmail = server.getServerProperties().get(ADMIN_EMAIL_KEY); 098 String emailSubject = "Hackystat Version 8 Registration"; 099 String emailBody = "Welcome to Hackystat. " + "\nYou are registered with: " 100 + server.getServerProperties().getFullHost() + "\nYour user name is: " 101 + user.getEmail() + "\nYour password is: " + user.getPassword() 102 + "\nNote that both user name and password are case-sensitive." 103 + "\n\nFor questions, email: " + adminEmail 104 + "\nYou can also see documentation at http://www.hackystat.org/" 105 + "\nWe hope you enjoy using Hackystat!"; 106 107 boolean success = mailer.send(email, emailSubject, emailBody); 108 // server.getLogger().info("Email sent " + (success ? "successfully." : "unsuccessfully.")); 109 if (success) { 110 // Don't send the administrator emails about test user registration. 111 if (!userManager.isTestUser(user)) { 112 mailer.send(adminEmail, "Hackystat 8 Admin Registration", "User " + email 113 + " registered and received password: " + user.getPassword() + "\n" + "for host: " 114 + server.getServerProperties().get(HOSTNAME_KEY)); 115 } 116 117 String responseHtml = "<html>" + " <body>" 118 + " Thank you for registering with this SensorBase. " + " <p>" 119 + " Your password has been sent to: " + email + " </body>" + "</html>"; 120 server.getLogger().info("Registered: " + email + " " + user.getPassword()); 121 getResponse().setStatus(Status.SUCCESS_OK); 122 Representation representation = new StringRepresentation(responseHtml); 123 representation.setMediaType(MediaType.TEXT_HTML); 124 getResponse().setEntity(representation); 125 } 126 } 127 catch (RuntimeException e) { 128 setStatusInternalError(e); 129 } 130 } 131 }