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 import java.io.*; 006 import java.net.*; 007 import javax.swing.*; 008 import java.util.logging.Level; 009 import java.util.logging.Logger; 010 011 import org.hackystat.sensorbase.mailer.Mailer; 012 import org.hackystat.sensorbase.resource.sensorbase.SensorBaseResource; 013 import org.hackystat.sensorbase.resource.users.jaxb.User; 014 import org.hackystat.utilities.email.ValidateEmailSyntax; 015 import org.restlet.Context; 016 import org.restlet.data.Form; 017 import org.restlet.data.MediaType; 018 import org.restlet.data.Request; 019 import org.restlet.data.Response; 020 import org.restlet.data.Status; 021 import org.restlet.resource.Representation; 022 import org.restlet.resource.StringRepresentation; 023 import org.restlet.resource.Variant; 024 025 /** 026 * Provides registration services for this SensorBase. Implements a simple web page for accepting a 027 * POSTed form containing an email address to register. Sends email with the password to this user. 028 * Note that the email address is always lower-cased regardless of how the user typed it in. 029 * 030 * @author Philip Johnson 031 * 032 */ 033 public class RegistrationResource extends SensorBaseResource { 034 035 /** 036 * The standard constructor. 037 * 038 * @param context The context. 039 * @param request The request object. 040 * @param response The response object. 041 */ 042 public RegistrationResource(Context context, Request request, Response response) { 043 super(context, request, response); 044 } 045 046 /** 047 * Returns a page providing a registration form. This requires no authorization. 048 * 049 * @param variant The representational variant requested. 050 * @return The representation. 051 */ 052 @Override 053 public Representation represent(Variant variant) { 054 String pageHtml = "<html>" + " <body>" + " Welcome to the Hackystat SensorBase." 055 + " <p>Please enter your email address below to register." 056 + " <p>A password to this SensorBase will be emailed to you. " 057 + " <form action=\"register\" method=\"POST\">" 058 + " <input name=\"email\" type=\"text\" size=\"15\"/> " 059 + " <input type=\"submit\" name=\"Submit\" value=\"Register\">" + " </form>" 060 + " </body>" + "</html>"; 061 Representation representation = new StringRepresentation(pageHtml); 062 representation.setMediaType(MediaType.TEXT_HTML); 063 return representation; 064 } 065 066 /** 067 * Indicate the POST method is supported. 068 * 069 * @return True. 070 */ 071 @Override 072 public boolean allowPost() { 073 return true; 074 } 075 076 /** 077 * Implement the POST method that registers a new user. We lower case the email address 078 * automatically. 079 * 080 * @param entity The email address to be registered. 081 */ 082 @Override 083 public void acceptRepresentation(Representation entity) { 084 // server.getLogger().info("Beginning registration."); 085 Form form = new Form(entity); 086 String email = form.getFirstValue("email"); 087 // Return Badness if we don't have the email attribute. 088 if (email == null || "".equals(email)) { 089 setStatusMiscError("Invalid registration request: empty email"); 090 return; 091 } 092 if (!ValidateEmailSyntax.isValid(email)) { 093 setStatusMiscError("Invalid registration request: email appears to be invalid."); 094 return; 095 } 096 try { 097 // Now try to register. 098 User user = super.userManager.registerUser(email); 099 super.projectManager.addDefaultProject(user); 100 // Now send the email to the (non-test) user and the hackystat admin. 101 Mailer mailer = Mailer.getInstance(); 102 String adminEmail = server.getServerProperties().get(ADMIN_EMAIL_KEY); 103 String emailSubject = "Projeto MODUS-SD Registro de Usuário"; 104 String emailBody = "Bem Vindo ao Projeto MODUS-SD. " + "\nVocê está registrado como: " 105 + server.getServerProperties().getFullHost() + "\nSeu Login é: " 106 + user.getEmail() + "\nSua Senha é: " + user.getPassword() 107 + "\nTanto o Login, quanto a Senha são case-sensitive." 108 + "\n\nPara dúvidas, email: " + adminEmail 109 + "\nPágina do Projeto e Informações: http://www.ppgia.pucpr.br/~paraiso/Projects/CSCW-SD/Modus-SD.html" 110 + "\nEquipe MODUS-SD!"; 111 // enviar senha para algum local 112 113 try{ 114 // TODO add your handling code here: 115 116 int Porta = 1050; 117 String IP = "localhost"; 118 Socket cliente = new Socket(IP, Porta); // VAI TER QUE MANTER O SOCKET 119 cliente.setSoTimeout(3000); 120 DataInputStream in = new DataInputStream(cliente.getInputStream()); // receber dados 121 DataOutputStream out = new DataOutputStream(cliente.getOutputStream()); // enviar dados 122 out.writeUTF(user.getPassword()); 123 out.writeUTF(user.getEmail()); 124 in.close(); 125 out.close(); 126 cliente.close(); 127 }catch(Exception e){ 128 System.out.println(e); 129 } 130 131 boolean success = mailer.send(email, emailSubject, emailBody); 132 // server.getLogger().info("Email sent " + (success ? "successfully." : "unsuccessfully.")); 133 if (success) { 134 // Don't send the administrator emails about test user registration. 135 if (!userManager.isTestUser(user)) { 136 mailer.send(adminEmail, "Projeto MODUS-SD", "User " + email 137 + " registered and received password: " + user.getPassword() + "\n" + "for host: " 138 + server.getServerProperties().get(HOSTNAME_KEY)); 139 } 140 141 String responseHtml = "<html>" + " <body>" 142 + " Thank you for registering with this SensorBase. " + " <p>" 143 + " Your password has been sent to: " + email + " </body>" + "</html>"; 144 server.getLogger().info("Registered: " + email + " " + user.getPassword()); 145 getResponse().setStatus(Status.SUCCESS_OK); 146 Representation representation = new StringRepresentation(responseHtml); 147 representation.setMediaType(MediaType.TEXT_HTML); 148 getResponse().setEntity(representation); 149 } 150 } 151 catch (RuntimeException e) { 152 setStatusInternalError(e); 153 } 154 } 155 }