Use firstname & lastname instead of full name for more flexibility and escape commas...
[pithos] / src / gr / ebs / gss / server / Registration.java
1 /*
2  * Copyright 2010 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 package gr.ebs.gss.server;
20
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;
29
30 import java.io.IOException;
31 import java.net.URLEncoder;
32 import java.util.concurrent.Callable;
33
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;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46  * The servlet that handles user registration.
47  *
48  * @author past
49  */
50 public class Registration extends HttpServlet {
51         /**
52          * The request parameter name for the acceptance flag.
53          */
54         private static final String ACCEPT_PARAM = "accept";
55
56         /**
57          * The request parameter name for the firstname.
58          */
59         private static final String FIRSTNAME_PARAM = "firstname";
60
61         /**
62          * The request parameter name for the lastname.
63          */
64         private static final String LASTNAME_PARAM = "lastname";
65
66         /**
67          * The request parameter name for the username.
68          */
69         private static final String USERNAME_PARAM = "username";
70
71         /**
72          * The request parameter name for the e-mail.
73          */
74         private static final String EMAIL_PARAM = "email";
75
76         /**
77          * The request parameter name for the password.
78          */
79         private static final String PASSWORD_PARAM = "password";
80
81         /**
82          * The request parameter name for the password confirmation.
83          */
84         private static final String PASSWORD2_PARAM = "password2";
85
86         /**
87          * The serial version UID of the class.
88          */
89         private static final long serialVersionUID = 1L;
90
91         /**
92          * The logger.
93          */
94         private static Log logger = LogFactory.getLog(Registration.class);
95
96         /**
97          * A helper method that retrieves a reference to the ExternalAPI bean and
98          * stores it for future use.
99          *
100          * @return an ExternalAPI instance
101          * @throws RpcException in case an error occurs
102          */
103         private ExternalAPI getService() throws RpcException {
104                 try {
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");
111                 }
112         }
113
114         @Override
115         public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
116                 if (getConfiguration().getBoolean("onlyRegisterWithCode"))
117                         response.sendRedirect("invites.jsp");
118                 else
119                         response.sendRedirect("register.jsp");
120         }
121
122         @Override
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");
132
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);
141                         return;
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);
149                         return;
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);
157                         return;
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);
165                         return;
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);
174                         return;
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);
183                         return;
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);
192                         return;
193                 }
194
195                 User user = null;
196                 try {
197                         user = getService().findUser(username);
198                         if (user != null) {
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);
205                                 return;
206                         }
207                         try {
208                                 getService().createLdapUser(username, firstname, lastname, email, password);
209                         } catch (Exception e) {
210                                 logger.error(e);
211                                 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
212                                 return;
213                         }
214                         final UserDTO userDto = new TransactionHelper<UserDTO>().tryExecute(new Callable<UserDTO>() {
215                                 @Override
216                                 public UserDTO call() throws Exception {
217                                         return getService().createUser(username, firstname + " " + lastname, email, "", "").getDTO();
218                                 }
219
220                         });
221                         new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
222                                 @Override
223                                 public Void call() throws Exception {
224                                         getService().updateUserPolicyAcceptance(userDto.getId(), true);
225                                         return null;
226                                 }
227
228                         });
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) {
245                         logger.error(e);
246                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
247                 }
248         }
249 }