Author: vjrj
Date: Wed Jan 16 16:19:07 2013
New Revision: 1434014

URL: http://svn.apache.org/viewvc?rev=1434014&view=rev
Log:
RegistrationUtil new checkNewUsername function.
https://reviews.apache.org/r/7903/

Modified:
    
incubator/wave/trunk/src/org/waveprotocol/box/server/rpc/UserRegistrationServlet.java
    
incubator/wave/trunk/src/org/waveprotocol/box/server/util/RegistrationUtil.java

Modified: 
incubator/wave/trunk/src/org/waveprotocol/box/server/rpc/UserRegistrationServlet.java
URL: 
http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/box/server/rpc/UserRegistrationServlet.java?rev=1434014&r1=1434013&r2=1434014&view=diff
==============================================================================
--- 
incubator/wave/trunk/src/org/waveprotocol/box/server/rpc/UserRegistrationServlet.java
 (original)
+++ 
incubator/wave/trunk/src/org/waveprotocol/box/server/rpc/UserRegistrationServlet.java
 Wed Jan 16 16:19:07 2013
@@ -24,17 +24,14 @@ import com.google.inject.Inject;
 import com.google.inject.name.Named;
 
 import org.waveprotocol.box.server.CoreSettings;
-import org.waveprotocol.box.server.account.HumanAccountDataImpl;
 import 
org.waveprotocol.box.server.authentication.HttpRequestBasedCallbackHandler;
 import org.waveprotocol.box.server.authentication.PasswordDigest;
 import org.waveprotocol.box.server.gxp.UserRegistrationPage;
 import org.waveprotocol.box.server.persistence.AccountStore;
-import org.waveprotocol.box.server.persistence.PersistenceException;
 import org.waveprotocol.box.server.robots.agent.welcome.WelcomeRobot;
 import org.waveprotocol.box.server.util.RegistrationUtil;
 import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
 import org.waveprotocol.wave.model.wave.ParticipantId;
-import org.waveprotocol.wave.util.logging.Log;
 
 import java.io.IOException;
 import java.util.Locale;
@@ -59,8 +56,6 @@ public final class UserRegistrationServl
   private final boolean registrationDisabled;
   private final String analyticsAccount;
 
-  private final Log LOG = Log.get(UserRegistrationServlet.class);
-
   @Inject
   public UserRegistrationServlet(AccountStore accountStore,
       @Named(CoreSettings.WAVE_SERVER_DOMAIN) String domain, WelcomeRobot 
welcomeBot,
@@ -107,30 +102,10 @@ public final class UserRegistrationServl
    */
   private String tryCreateUser(String username, String password) {
     ParticipantId id = null;
-
     try {
-      // First, some cleanup on the parameters.
-      if (username == null) {
-        return "Username portion of address cannot be empty";
-      }
-      username = username.trim().toLowerCase();
-      if (username.contains(ParticipantId.DOMAIN_PREFIX)) {
-        id = ParticipantId.of(username);
-      } else {
-        id = ParticipantId.of(username + ParticipantId.DOMAIN_PREFIX + domain);
-      }
-      if (id.getAddress().indexOf("@") < 1) {
-        return "Username portion of address cannot be empty";
-      }
-      String[] usernameSplit = id.getAddress().split("@");
-      if (usernameSplit.length != 2 || !usernameSplit[0].matches("[\\w\\.]+")) 
{
-        return "Only letters (a-z), numbers (0-9), and periods (.) are allowed 
in Username";
-      }
-      if (!id.getDomain().equals(domain)) {
-        return "You can only create users at the " + domain + " domain";
-      }
-    } catch (InvalidParticipantAddress e) {
-      return "Invalid username";
+      id = RegistrationUtil.checkNewUsername(domain, username);
+    } catch (InvalidParticipantAddress exception) {
+      return exception.getMessage();
     }
 
     if(RegistrationUtil.doesAccountExist(accountStore, id)) {

Modified: 
incubator/wave/trunk/src/org/waveprotocol/box/server/util/RegistrationUtil.java
URL: 
http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/box/server/util/RegistrationUtil.java?rev=1434014&r1=1434013&r2=1434014&view=diff
==============================================================================
--- 
incubator/wave/trunk/src/org/waveprotocol/box/server/util/RegistrationUtil.java 
(original)
+++ 
incubator/wave/trunk/src/org/waveprotocol/box/server/util/RegistrationUtil.java 
Wed Jan 16 16:19:07 2013
@@ -42,6 +42,41 @@ public class RegistrationUtil {
   private RegistrationUtil() {
   }
 
+  /**
+   * Checks if a new username is correct and generates its ParticipantId. On 
error returns
+   * exception containing an error message. On success, returns the id.
+   *
+   * @param domain the domain
+   * @param username the new username
+   * @return the new participant id
+   * @throws InvalidParticipantAddress if the new username is an invalid 
participant address
+   */
+  public static ParticipantId checkNewUsername(String domain, String username) 
throws InvalidParticipantAddress {
+    ParticipantId id = null;
+
+      // First, some cleanup on the parameters.
+      if (username == null) {
+        throw new InvalidParticipantAddress(username, "Username portion of 
address cannot be empty");
+      }
+      username = username.trim().toLowerCase();
+      if (username.contains(ParticipantId.DOMAIN_PREFIX)) {
+        id = ParticipantId.of(username);
+      } else {
+        id = ParticipantId.of(username + ParticipantId.DOMAIN_PREFIX + domain);
+      }
+      if (id.getAddress().indexOf("@") < 1) {
+        throw new InvalidParticipantAddress(username, "Username portion of 
address cannot be empty");
+      }
+      String[] usernameSplit = id.getAddress().split("@");
+      if (usernameSplit.length != 2 || !usernameSplit[0].matches("[\\w\\.]+")) 
{
+        throw new InvalidParticipantAddress(username, "Only letters (a-z), 
numbers (0-9), and periods (.) are allowed in Username");
+      }
+      if (!id.getDomain().equals(domain)) {
+        throw new InvalidParticipantAddress(username,"You can only create 
users at the " + domain + " domain");
+      }
+    return id;
+  }
+
   public static boolean createAccountIfMissing(AccountStore accountStore, 
ParticipantId id,
       PasswordDigest password, WelcomeRobot welcomeBot) {
     HumanAccountDataImpl account = new HumanAccountDataImpl(id, password);


Reply via email to