2 * Copyright 2010 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.server;
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.client.exceptions.DuplicateNameException;
23 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
24 import gr.ebs.gss.client.exceptions.RpcException;
25 import gr.ebs.gss.server.domain.User;
26 import gr.ebs.gss.server.domain.dto.UserDTO;
27 import gr.ebs.gss.server.ejb.ExternalAPI;
28 import gr.ebs.gss.server.ejb.TransactionHelper;
30 import java.io.IOException;
31 import java.net.URLEncoder;
32 import java.util.concurrent.Callable;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import javax.rmi.PortableRemoteObject;
38 import javax.servlet.http.HttpServlet;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
46 * The servlet that handles user registration.
50 public class Registration extends HttpServlet {
52 * The request parameter name for the acceptance flag.
54 private static final String ACCEPT_PARAM = "accept";
57 * The request parameter name for the firstname.
59 private static final String FIRSTNAME_PARAM = "firstname";
62 * The request parameter name for the lastname.
64 private static final String LASTNAME_PARAM = "lastname";
67 * The request parameter name for the username.
69 private static final String USERNAME_PARAM = "username";
72 * The request parameter name for the e-mail.
74 private static final String EMAIL_PARAM = "email";
77 * The request parameter name for the password.
79 private static final String PASSWORD_PARAM = "password";
82 * The request parameter name for the password confirmation.
84 private static final String PASSWORD2_PARAM = "password2";
87 * The serial version UID of the class.
89 private static final long serialVersionUID = 1L;
94 private static Log logger = LogFactory.getLog(Registration.class);
97 * A helper method that retrieves a reference to the ExternalAPI bean and
98 * stores it for future use.
100 * @return an ExternalAPI instance
101 * @throws RpcException in case an error occurs
103 private ExternalAPI getService() throws RpcException {
105 final Context ctx = new InitialContext();
106 final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
107 return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
108 } catch (final NamingException e) {
109 logger.error("Unable to retrieve the ExternalAPI EJB", e);
110 throw new RpcException("An error occurred while contacting the naming service");
115 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
116 if (getConfiguration().getBoolean("onlyRegisterWithCode"))
117 response.sendRedirect("invites.jsp");
119 response.sendRedirect("register.jsp");
123 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
124 final String firstname = request.getParameter(FIRSTNAME_PARAM);
125 final String lastname = request.getParameter(LASTNAME_PARAM);
126 final String email = request.getParameter(EMAIL_PARAM);
127 final String username = request.getParameter(USERNAME_PARAM);
128 String password = request.getParameter(PASSWORD_PARAM);
129 String password2 = request.getParameter(PASSWORD2_PARAM);
130 String accept = request.getParameter(ACCEPT_PARAM);
131 response.setContentType("text/html");
133 // Validate input parameters.
134 if (username == null || username.isEmpty()) {
135 String error = URLEncoder.encode("No username was specified", "UTF-8");
136 String errorUrl = "register.jsp?username=&error=" + error;
137 errorUrl += "&firstname=" + (firstname == null? "": firstname);
138 errorUrl += "&lastname=" + (lastname == null? "": lastname);
139 errorUrl += "&email=" + (email == null? "": email);
140 response.sendRedirect(errorUrl);
142 } else if (firstname == null || firstname.isEmpty()) {
143 String error = URLEncoder.encode("No firstname was specified", "UTF-8");
144 String errorUrl = "register.jsp?firstname=&error=" + error;
145 errorUrl += "&username=" + username;
146 errorUrl += "&lastname=" + (lastname == null? "": lastname);
147 errorUrl += "&email=" + (email == null? "": email);
148 response.sendRedirect(errorUrl);
150 } else if (lastname == null || lastname.isEmpty()) {
151 String error = URLEncoder.encode("No lastname was specified", "UTF-8");
152 String errorUrl = "register.jsp?lastname=&error=" + error;
153 errorUrl += "&username=" + username;
154 errorUrl += "&firstname=" + firstname;
155 errorUrl += "&email=" + (email == null? "": email);
156 response.sendRedirect(errorUrl);
158 } else if (email == null || email.isEmpty()) {
159 String error = URLEncoder.encode("No e-mail was specified", "UTF-8");
160 String errorUrl = "register.jsp?email=&error=" + error;
161 errorUrl += "&username=" + username;
162 errorUrl += "&firstname=" + firstname;
163 errorUrl += "&lastname=" + lastname;
164 response.sendRedirect(errorUrl);
166 } else if (password == null || password.isEmpty()) {
167 String error = URLEncoder.encode("No password was specified", "UTF-8");
168 String errorUrl = "register.jsp?error=" + error;
169 errorUrl += "&username=" + username;
170 errorUrl += "&firstname=" + firstname;
171 errorUrl += "&lastname=" + lastname;
172 errorUrl += "&email=" + email;
173 response.sendRedirect(errorUrl);
175 } else if (!password.equals(password2)) {
176 String error = URLEncoder.encode("Passwords do not match", "UTF-8");
177 String errorUrl = "register.jsp?error=" + error;
178 errorUrl += "&username=" + username;
179 errorUrl += "&firstname=" + firstname;
180 errorUrl += "&lastname=" + lastname;
181 errorUrl += "&email=" + email;
182 response.sendRedirect(errorUrl);
184 } else if (!"on".equalsIgnoreCase(accept)) {
185 String error = URLEncoder.encode("You must accept the terms and conditions", "UTF-8");
186 String errorUrl = "register.jsp?error=" + error;
187 errorUrl += "&username=" + username;
188 errorUrl += "&firstname=" + firstname;
189 errorUrl += "&lastname=" + lastname;
190 errorUrl += "&email=" + email;
191 response.sendRedirect(errorUrl);
197 user = getService().findUser(username);
199 String error = URLEncoder.encode("The username already exists", "UTF-8");
200 String errorUrl = "register.jsp?username=&error=" + error;
201 errorUrl += "&firstname=" + firstname;
202 errorUrl += "&lastname=" + lastname;
203 errorUrl += "&email=" + email;
204 response.sendRedirect(errorUrl);
208 getService().createLdapUser(username, firstname, lastname, email, password);
209 } catch (Exception e) {
211 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
214 final UserDTO userDto = new TransactionHelper<UserDTO>().tryExecute(new Callable<UserDTO>() {
216 public UserDTO call() throws Exception {
217 return getService().createUser(username, firstname + " " + lastname, email, "", "").getDTO();
221 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
223 public Void call() throws Exception {
224 getService().updateUserPolicyAcceptance(userDto.getId(), true);
229 response.sendRedirect("registered.jsp");
230 } catch (RpcException e) {
231 String error = "An error occurred while communicating with the service";
232 logger.error(error, e);
233 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
234 } catch (DuplicateNameException e) {
235 // Can't happen, but this is more user-friendly than an assert.
236 String error = URLEncoder.encode("The username already exists", "UTF-8");
237 String errorUrl = "register.jsp?username=&firstname=&lastname=&email=&error=" + error;
238 response.sendRedirect(errorUrl);
239 } catch (ObjectNotFoundException e) {
240 // Can't happen, but this is more user-friendly than an assert.
241 String error = URLEncoder.encode("No username or name was specified", "UTF-8");
242 String errorUrl = "register.jsp?username=&firstname=&lastname=&email=&error=" + error;
243 response.sendRedirect(errorUrl);
244 } catch (Exception e) {
246 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());