import gr.ebs.gss.client.exceptions.RpcException;
import gr.ebs.gss.server.domain.User;
import gr.ebs.gss.server.domain.dto.UserDTO;
-import gr.ebs.gss.server.ejb.ExternalAPI;
import gr.ebs.gss.server.ejb.TransactionHelper;
import java.io.IOException;
-import java.net.URLEncoder;
import java.util.concurrent.Callable;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import javax.rmi.PortableRemoteObject;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
*
* @author past
*/
-public class Registration extends HttpServlet {
+public class Registration extends BaseServlet {
/**
* The request parameter name for the acceptance flag.
*/
private static final String ACCEPT_PARAM = "accept";
/**
- * The request parameter name for the name.
+ * The request parameter name for the firstname.
*/
- private static final String NAME_PARAM = "name";
+ private static final String FIRSTNAME_PARAM = "firstname";
+
+ /**
+ * The request parameter name for the lastname.
+ */
+ private static final String LASTNAME_PARAM = "lastname";
/**
* The request parameter name for the username.
*/
private static Log logger = LogFactory.getLog(Registration.class);
- /**
- * A helper method that retrieves a reference to the ExternalAPI bean and
- * stores it for future use.
- *
- * @return an ExternalAPI instance
- * @throws RpcException in case an error occurs
- */
- private ExternalAPI getService() throws RpcException {
- try {
- final Context ctx = new InitialContext();
- final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
- return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
- } catch (final NamingException e) {
- logger.error("Unable to retrieve the ExternalAPI EJB", e);
- throw new RpcException("An error occurred while contacting the naming service");
- }
- }
-
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (getConfiguration().getBoolean("onlyRegisterWithCode"))
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
- final String name = request.getParameter(NAME_PARAM);
+ final String firstname = request.getParameter(FIRSTNAME_PARAM);
+ final String lastname = request.getParameter(LASTNAME_PARAM);
final String email = request.getParameter(EMAIL_PARAM);
final String username = request.getParameter(USERNAME_PARAM);
String password = request.getParameter(PASSWORD_PARAM);
// Validate input parameters.
if (username == null || username.isEmpty()) {
- String error = URLEncoder.encode("No username was specified", "UTF-8");
+ String error = encode("No username was specified");
+ String errorUrl = "register.jsp?username=&error=" + error;
+ errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
+ errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
+ errorUrl += "&email=" + (email == null? "": encode(email));
+ response.sendRedirect(errorUrl);
+ return;
+ } else if (username.indexOf(' ') != -1) {
+ String error = encode("Spaces in username are not allowed");
String errorUrl = "register.jsp?username=&error=" + error;
- errorUrl += "&name=" + (name == null? "": name);
- errorUrl += "&email=" + (email == null? "": email);
+ errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
+ errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
+ errorUrl += "&email=" + (email == null? "": encode(email));
response.sendRedirect(errorUrl);
return;
- } else if (name == null || name.isEmpty()) {
- String error = URLEncoder.encode("No name was specified", "UTF-8");
- String errorUrl = "register.jsp?name=&error=" + error;
- errorUrl += "&username=" + username;
- errorUrl += "&email=" + (email == null? "": email);
+ } else if (firstname == null || firstname.isEmpty()) {
+ String error = encode("No firstname was specified");
+ String errorUrl = "register.jsp?firstname=&error=" + error;
+ errorUrl += "&username=" + encode(username);
+ errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
+ errorUrl += "&email=" + (email == null? "": encode(email));
+ response.sendRedirect(errorUrl);
+ return;
+ } else if (lastname == null || lastname.isEmpty()) {
+ String error = encode("No lastname was specified");
+ String errorUrl = "register.jsp?lastname=&error=" + error;
+ errorUrl += "&username=" + encode(username);
+ errorUrl += "&firstname=" + encode(firstname);
+ errorUrl += "&email=" + (email == null? "": encode(email));
response.sendRedirect(errorUrl);
return;
} else if (email == null || email.isEmpty()) {
- String error = URLEncoder.encode("No e-mail was specified", "UTF-8");
+ String error = encode("No e-mail was specified");
String errorUrl = "register.jsp?email=&error=" + error;
- errorUrl += "&username=" + username;
- errorUrl += "&name=" + name;
+ errorUrl += "&username=" + encode(username);
+ errorUrl += "&firstname=" + encode(firstname);
+ errorUrl += "&lastname=" + encode(lastname);
response.sendRedirect(errorUrl);
return;
} else if (password == null || password.isEmpty()) {
- String error = URLEncoder.encode("No password was specified", "UTF-8");
+ String error = encode("No password was specified");
String errorUrl = "register.jsp?error=" + error;
- errorUrl += "&username=" + username;
- errorUrl += "&name=" + name;
- errorUrl += "&email=" + email;
+ errorUrl += "&username=" + encode(username);
+ errorUrl += "&firstname=" + encode(firstname);
+ errorUrl += "&lastname=" + encode(lastname);
+ errorUrl += "&email=" + encode(email);
response.sendRedirect(errorUrl);
return;
} else if (!password.equals(password2)) {
- String error = URLEncoder.encode("Passwords do not match", "UTF-8");
+ String error = encode("Passwords do not match");
String errorUrl = "register.jsp?error=" + error;
- errorUrl += "&username=" + username;
- errorUrl += "&name=" + name;
- errorUrl += "&email=" + email;
+ errorUrl += "&username=" + encode(username);
+ errorUrl += "&firstname=" + encode(firstname);
+ errorUrl += "&lastname=" + encode(lastname);
+ errorUrl += "&email=" + encode(email);
response.sendRedirect(errorUrl);
return;
} else if (!"on".equalsIgnoreCase(accept)) {
- String error = URLEncoder.encode("You must accept the terms and conditions", "UTF-8");
+ String error = encode("You must accept the terms and conditions");
String errorUrl = "register.jsp?error=" + error;
- errorUrl += "&username=" + username;
- errorUrl += "&name=" + name;
- errorUrl += "&email=" + email;
+ errorUrl += "&username=" + encode(username);
+ errorUrl += "&firstname=" + encode(firstname);
+ errorUrl += "&lastname=" + encode(lastname);
+ errorUrl += "&email=" + encode(email);
response.sendRedirect(errorUrl);
return;
}
try {
user = getService().findUser(username);
if (user != null) {
- String error = URLEncoder.encode("The username already exists", "UTF-8");
+ String error = encode("The username already exists");
String errorUrl = "register.jsp?username=&error=" + error;
- errorUrl += "&name=" + name;
- errorUrl += "&email=" + email;
+ errorUrl += "&firstname=" + encode(firstname);
+ errorUrl += "&lastname=" + encode(lastname);
+ errorUrl += "&email=" + encode(email);
response.sendRedirect(errorUrl);
return;
}
+ try {
+ getService().createLdapUser(username, firstname, lastname, email, password);
+ } catch (Exception e) {
+ logger.error(e);
+ handleException(response, e.getMessage());
+ return;
+ }
final UserDTO userDto = new TransactionHelper<UserDTO>().tryExecute(new Callable<UserDTO>() {
@Override
public UserDTO call() throws Exception {
- return getService().createUser(username, name, email, "", "").getDTO();
+ return getService().createUser(username, firstname + " " + lastname, email, "", "").getDTO();
}
});
});
response.sendRedirect("registered.jsp");
} catch (RpcException e) {
- String error = "An error occurred while communicating with the service";
- logger.error(error, e);
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
+ logger.error(e);
+ handleException(response, "An error occurred while communicating with the service");
} catch (DuplicateNameException e) {
// Can't happen, but this is more user-friendly than an assert.
- String error = URLEncoder.encode("The username already exists", "UTF-8");
- String errorUrl = "register.jsp?username=&name=&email=&error=" + error;
- response.sendRedirect(errorUrl);
+ logger.error(e);
+ handleException(response, "The username already exists");
} catch (ObjectNotFoundException e) {
// Can't happen, but this is more user-friendly than an assert.
- String error = URLEncoder.encode("No username or name was specified", "UTF-8");
- String errorUrl = "register.jsp?username=&name=&email=&error=" + error;
- response.sendRedirect(errorUrl);
+ logger.error(e);
+ handleException(response, "No username or name was specified");
} catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.error(e);
+ handleException(response, e.getMessage());
}
}
+
+ private void handleException(HttpServletResponse response, String error) throws IOException {
+ String errorUrl = "register.jsp?username=&firstname=&lastname=&email=&error=" + encode(error);
+ response.sendRedirect(errorUrl);
+ }
}