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.TransactionHelper;
29 import java.io.IOException;
30 import java.util.concurrent.Callable;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
39 * The servlet that handles user registration.
43 public class Registration extends BaseServlet {
45 * The request parameter name for the acceptance flag.
47 private static final String ACCEPT_PARAM = "accept";
50 * The request parameter name for the firstname.
52 private static final String FIRSTNAME_PARAM = "firstname";
55 * The request parameter name for the lastname.
57 private static final String LASTNAME_PARAM = "lastname";
60 * The request parameter name for the username.
62 private static final String USERNAME_PARAM = "username";
65 * The request parameter name for the e-mail.
67 private static final String EMAIL_PARAM = "email";
70 * The request parameter name for the password.
72 private static final String PASSWORD_PARAM = "password";
75 * The request parameter name for the password confirmation.
77 private static final String PASSWORD2_PARAM = "password2";
80 * The serial version UID of the class.
82 private static final long serialVersionUID = 1L;
87 private static Log logger = LogFactory.getLog(Registration.class);
90 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
91 if (getConfiguration().getBoolean("onlyRegisterWithCode"))
92 response.sendRedirect("invites.jsp");
94 response.sendRedirect("register.jsp");
98 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
99 final String firstname = request.getParameter(FIRSTNAME_PARAM);
100 final String lastname = request.getParameter(LASTNAME_PARAM);
101 final String email = request.getParameter(EMAIL_PARAM);
102 final String username = request.getParameter(USERNAME_PARAM);
103 String password = request.getParameter(PASSWORD_PARAM);
104 String password2 = request.getParameter(PASSWORD2_PARAM);
105 String accept = request.getParameter(ACCEPT_PARAM);
106 response.setContentType("text/html");
108 // Validate input parameters.
109 if (username == null || username.isEmpty()) {
110 String error = encode("No username was specified");
111 String errorUrl = "register.jsp?username=&error=" + error;
112 errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
113 errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
114 errorUrl += "&email=" + (email == null? "": encode(email));
115 response.sendRedirect(errorUrl);
117 } else if (username.indexOf(' ') != -1) {
118 String error = encode("Spaces in username are not allowed");
119 String errorUrl = "register.jsp?username=&error=" + error;
120 errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
121 errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
122 errorUrl += "&email=" + (email == null? "": encode(email));
123 response.sendRedirect(errorUrl);
125 } else if (firstname == null || firstname.isEmpty()) {
126 String error = encode("No firstname was specified");
127 String errorUrl = "register.jsp?firstname=&error=" + error;
128 errorUrl += "&username=" + encode(username);
129 errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
130 errorUrl += "&email=" + (email == null? "": encode(email));
131 response.sendRedirect(errorUrl);
133 } else if (lastname == null || lastname.isEmpty()) {
134 String error = encode("No lastname was specified");
135 String errorUrl = "register.jsp?lastname=&error=" + error;
136 errorUrl += "&username=" + encode(username);
137 errorUrl += "&firstname=" + encode(firstname);
138 errorUrl += "&email=" + (email == null? "": encode(email));
139 response.sendRedirect(errorUrl);
141 } else if (email == null || email.isEmpty()) {
142 String error = encode("No e-mail was specified");
143 String errorUrl = "register.jsp?email=&error=" + error;
144 errorUrl += "&username=" + encode(username);
145 errorUrl += "&firstname=" + encode(firstname);
146 errorUrl += "&lastname=" + encode(lastname);
147 response.sendRedirect(errorUrl);
149 } else if (password == null || password.isEmpty()) {
150 String error = encode("No password was specified");
151 String errorUrl = "register.jsp?error=" + error;
152 errorUrl += "&username=" + encode(username);
153 errorUrl += "&firstname=" + encode(firstname);
154 errorUrl += "&lastname=" + encode(lastname);
155 errorUrl += "&email=" + encode(email);
156 response.sendRedirect(errorUrl);
158 } else if (!password.equals(password2)) {
159 String error = encode("Passwords do not match");
160 String errorUrl = "register.jsp?error=" + error;
161 errorUrl += "&username=" + encode(username);
162 errorUrl += "&firstname=" + encode(firstname);
163 errorUrl += "&lastname=" + encode(lastname);
164 errorUrl += "&email=" + encode(email);
165 response.sendRedirect(errorUrl);
167 } else if (!"on".equalsIgnoreCase(accept)) {
168 String error = encode("You must accept the terms and conditions");
169 String errorUrl = "register.jsp?error=" + error;
170 errorUrl += "&username=" + encode(username);
171 errorUrl += "&firstname=" + encode(firstname);
172 errorUrl += "&lastname=" + encode(lastname);
173 errorUrl += "&email=" + encode(email);
174 response.sendRedirect(errorUrl);
180 user = getService().findUser(username);
182 String error = encode("The username already exists");
183 String errorUrl = "register.jsp?username=&error=" + error;
184 errorUrl += "&firstname=" + encode(firstname);
185 errorUrl += "&lastname=" + encode(lastname);
186 errorUrl += "&email=" + encode(email);
187 response.sendRedirect(errorUrl);
191 getService().createLdapUser(username, firstname, lastname, email, password);
192 } catch (Exception e) {
194 handleException(response, e.getMessage());
197 final UserDTO userDto = new TransactionHelper<UserDTO>().tryExecute(new Callable<UserDTO>() {
199 public UserDTO call() throws Exception {
200 return getService().createUser(username, firstname + " " + lastname, email, "", "").getDTO();
204 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
206 public Void call() throws Exception {
207 getService().updateUserPolicyAcceptance(userDto.getId(), true);
212 response.sendRedirect("registered.jsp");
213 } catch (RpcException e) {
215 handleException(response, "An error occurred while communicating with the service");
216 } catch (DuplicateNameException e) {
217 // Can't happen, but this is more user-friendly than an assert.
219 handleException(response, "The username already exists");
220 } catch (ObjectNotFoundException e) {
221 // Can't happen, but this is more user-friendly than an assert.
223 handleException(response, "No username or name was specified");
224 } catch (Exception e) {
226 handleException(response, e.getMessage());
230 private void handleException(HttpServletResponse response, String error) throws IOException {
231 String errorUrl = "register.jsp?username=&firstname=&lastname=&email=&error=" + encode(error);
232 response.sendRedirect(errorUrl);