Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / Registration.java @ b8e09949

History | View | Annotate | Download (8.4 kB)

1 2f551abc Panagiotis Astithas
/*
2 2f551abc Panagiotis Astithas
 * Copyright 2010 Electronic Business Systems Ltd.
3 2f551abc Panagiotis Astithas
 *
4 2f551abc Panagiotis Astithas
 * This file is part of GSS.
5 2f551abc Panagiotis Astithas
 *
6 2f551abc Panagiotis Astithas
 * GSS is free software: you can redistribute it and/or modify
7 2f551abc Panagiotis Astithas
 * it under the terms of the GNU General Public License as published by
8 2f551abc Panagiotis Astithas
 * the Free Software Foundation, either version 3 of the License, or
9 2f551abc Panagiotis Astithas
 * (at your option) any later version.
10 2f551abc Panagiotis Astithas
 *
11 2f551abc Panagiotis Astithas
 * GSS is distributed in the hope that it will be useful,
12 2f551abc Panagiotis Astithas
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 2f551abc Panagiotis Astithas
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 2f551abc Panagiotis Astithas
 * GNU General Public License for more details.
15 2f551abc Panagiotis Astithas
 *
16 2f551abc Panagiotis Astithas
 * You should have received a copy of the GNU General Public License
17 2f551abc Panagiotis Astithas
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 2f551abc Panagiotis Astithas
 */
19 2f551abc Panagiotis Astithas
package gr.ebs.gss.server;
20 2f551abc Panagiotis Astithas
21 2f551abc Panagiotis Astithas
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 2f551abc Panagiotis Astithas
import gr.ebs.gss.client.exceptions.DuplicateNameException;
23 2f551abc Panagiotis Astithas
import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
24 2f551abc Panagiotis Astithas
import gr.ebs.gss.client.exceptions.RpcException;
25 2f551abc Panagiotis Astithas
import gr.ebs.gss.server.domain.User;
26 2f551abc Panagiotis Astithas
import gr.ebs.gss.server.domain.dto.UserDTO;
27 2f551abc Panagiotis Astithas
import gr.ebs.gss.server.ejb.TransactionHelper;
28 2f551abc Panagiotis Astithas
29 2f551abc Panagiotis Astithas
import java.io.IOException;
30 2f551abc Panagiotis Astithas
import java.util.concurrent.Callable;
31 2f551abc Panagiotis Astithas
32 2f551abc Panagiotis Astithas
import javax.servlet.http.HttpServletRequest;
33 2f551abc Panagiotis Astithas
import javax.servlet.http.HttpServletResponse;
34 2f551abc Panagiotis Astithas
35 2f551abc Panagiotis Astithas
import org.apache.commons.logging.Log;
36 2f551abc Panagiotis Astithas
import org.apache.commons.logging.LogFactory;
37 2f551abc Panagiotis Astithas
38 2f551abc Panagiotis Astithas
/**
39 2f551abc Panagiotis Astithas
 * The servlet that handles user registration.
40 2f551abc Panagiotis Astithas
 *
41 2f551abc Panagiotis Astithas
 * @author past
42 2f551abc Panagiotis Astithas
 */
43 978061e3 Panagiotis Astithas
public class Registration extends BaseServlet {
44 2f551abc Panagiotis Astithas
        /**
45 2f551abc Panagiotis Astithas
         * The request parameter name for the acceptance flag.
46 2f551abc Panagiotis Astithas
         */
47 2f551abc Panagiotis Astithas
        private static final String ACCEPT_PARAM = "accept";
48 2f551abc Panagiotis Astithas
49 2f551abc Panagiotis Astithas
        /**
50 46268014 Panagiotis Astithas
         * The request parameter name for the firstname.
51 2f551abc Panagiotis Astithas
         */
52 46268014 Panagiotis Astithas
        private static final String FIRSTNAME_PARAM = "firstname";
53 46268014 Panagiotis Astithas
54 46268014 Panagiotis Astithas
        /**
55 46268014 Panagiotis Astithas
         * The request parameter name for the lastname.
56 46268014 Panagiotis Astithas
         */
57 46268014 Panagiotis Astithas
        private static final String LASTNAME_PARAM = "lastname";
58 2f551abc Panagiotis Astithas
59 2f551abc Panagiotis Astithas
        /**
60 2f551abc Panagiotis Astithas
         * The request parameter name for the username.
61 2f551abc Panagiotis Astithas
         */
62 2f551abc Panagiotis Astithas
        private static final String USERNAME_PARAM = "username";
63 2f551abc Panagiotis Astithas
64 2f551abc Panagiotis Astithas
        /**
65 2f551abc Panagiotis Astithas
         * The request parameter name for the e-mail.
66 2f551abc Panagiotis Astithas
         */
67 2f551abc Panagiotis Astithas
        private static final String EMAIL_PARAM = "email";
68 2f551abc Panagiotis Astithas
69 2f551abc Panagiotis Astithas
        /**
70 2f551abc Panagiotis Astithas
         * The request parameter name for the password.
71 2f551abc Panagiotis Astithas
         */
72 2f551abc Panagiotis Astithas
        private static final String PASSWORD_PARAM = "password";
73 2f551abc Panagiotis Astithas
74 2f551abc Panagiotis Astithas
        /**
75 2f551abc Panagiotis Astithas
         * The request parameter name for the password confirmation.
76 2f551abc Panagiotis Astithas
         */
77 2f551abc Panagiotis Astithas
        private static final String PASSWORD2_PARAM = "password2";
78 2f551abc Panagiotis Astithas
79 2f551abc Panagiotis Astithas
        /**
80 2f551abc Panagiotis Astithas
         * The serial version UID of the class.
81 2f551abc Panagiotis Astithas
         */
82 2f551abc Panagiotis Astithas
        private static final long serialVersionUID = 1L;
83 2f551abc Panagiotis Astithas
84 2f551abc Panagiotis Astithas
        /**
85 2f551abc Panagiotis Astithas
         * The logger.
86 2f551abc Panagiotis Astithas
         */
87 2f551abc Panagiotis Astithas
        private static Log logger = LogFactory.getLog(Registration.class);
88 2f551abc Panagiotis Astithas
89 2f551abc Panagiotis Astithas
        @Override
90 2f551abc Panagiotis Astithas
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
91 2f551abc Panagiotis Astithas
                if (getConfiguration().getBoolean("onlyRegisterWithCode"))
92 2f551abc Panagiotis Astithas
                        response.sendRedirect("invites.jsp");
93 2f551abc Panagiotis Astithas
                else
94 2f551abc Panagiotis Astithas
                        response.sendRedirect("register.jsp");
95 2f551abc Panagiotis Astithas
        }
96 2f551abc Panagiotis Astithas
97 2f551abc Panagiotis Astithas
        @Override
98 2f551abc Panagiotis Astithas
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
99 46268014 Panagiotis Astithas
                final String firstname = request.getParameter(FIRSTNAME_PARAM);
100 46268014 Panagiotis Astithas
                final String lastname = request.getParameter(LASTNAME_PARAM);
101 2f551abc Panagiotis Astithas
                final String email = request.getParameter(EMAIL_PARAM);
102 2f551abc Panagiotis Astithas
                final String username = request.getParameter(USERNAME_PARAM);
103 2f551abc Panagiotis Astithas
                String password = request.getParameter(PASSWORD_PARAM);
104 2f551abc Panagiotis Astithas
                String password2 = request.getParameter(PASSWORD2_PARAM);
105 2f551abc Panagiotis Astithas
                String accept = request.getParameter(ACCEPT_PARAM);
106 2f551abc Panagiotis Astithas
                response.setContentType("text/html");
107 2f551abc Panagiotis Astithas
108 2f551abc Panagiotis Astithas
                // Validate input parameters.
109 2f551abc Panagiotis Astithas
                if (username == null || username.isEmpty()) {
110 edf24a21 Panagiotis Astithas
                        String error = encode("No username was specified");
111 2f551abc Panagiotis Astithas
                        String errorUrl = "register.jsp?username=&error=" + error;
112 edf24a21 Panagiotis Astithas
                        errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
113 edf24a21 Panagiotis Astithas
                        errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
114 edf24a21 Panagiotis Astithas
                        errorUrl += "&email=" + (email == null? "": encode(email));
115 46268014 Panagiotis Astithas
                        response.sendRedirect(errorUrl);
116 46268014 Panagiotis Astithas
                        return;
117 09c35393 Panagiotis Astithas
                } else if (username.indexOf(' ') != -1) {
118 09c35393 Panagiotis Astithas
                        String error = encode("Spaces in username are not allowed");
119 09c35393 Panagiotis Astithas
                        String errorUrl = "register.jsp?username=&error=" + error;
120 09c35393 Panagiotis Astithas
                        errorUrl += "&firstname=" + (firstname == null? "": encode(firstname));
121 09c35393 Panagiotis Astithas
                        errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
122 09c35393 Panagiotis Astithas
                        errorUrl += "&email=" + (email == null? "": encode(email));
123 09c35393 Panagiotis Astithas
                        response.sendRedirect(errorUrl);
124 09c35393 Panagiotis Astithas
                        return;
125 46268014 Panagiotis Astithas
                } else if (firstname == null || firstname.isEmpty()) {
126 edf24a21 Panagiotis Astithas
                        String error = encode("No firstname was specified");
127 46268014 Panagiotis Astithas
                        String errorUrl = "register.jsp?firstname=&error=" + error;
128 edf24a21 Panagiotis Astithas
                        errorUrl += "&username=" + encode(username);
129 edf24a21 Panagiotis Astithas
                        errorUrl += "&lastname=" + (lastname == null? "": encode(lastname));
130 edf24a21 Panagiotis Astithas
                        errorUrl += "&email=" + (email == null? "": encode(email));
131 2f551abc Panagiotis Astithas
                        response.sendRedirect(errorUrl);
132 2f551abc Panagiotis Astithas
                        return;
133 46268014 Panagiotis Astithas
                } else if (lastname == null || lastname.isEmpty()) {
134 edf24a21 Panagiotis Astithas
                        String error = encode("No lastname was specified");
135 46268014 Panagiotis Astithas
                        String errorUrl = "register.jsp?lastname=&error=" + error;
136 edf24a21 Panagiotis Astithas
                        errorUrl += "&username=" + encode(username);
137 edf24a21 Panagiotis Astithas
                        errorUrl += "&firstname=" + encode(firstname);
138 edf24a21 Panagiotis Astithas
                        errorUrl += "&email=" + (email == null? "": encode(email));
139 2f551abc Panagiotis Astithas
                        response.sendRedirect(errorUrl);
140 2f551abc Panagiotis Astithas
                        return;
141 2f551abc Panagiotis Astithas
                } else if (email == null || email.isEmpty()) {
142 edf24a21 Panagiotis Astithas
                        String error = encode("No e-mail was specified");
143 2f551abc Panagiotis Astithas
                        String errorUrl = "register.jsp?email=&error=" + error;
144 edf24a21 Panagiotis Astithas
                        errorUrl += "&username=" + encode(username);
145 edf24a21 Panagiotis Astithas
                        errorUrl += "&firstname=" + encode(firstname);
146 edf24a21 Panagiotis Astithas
                        errorUrl += "&lastname=" + encode(lastname);
147 2f551abc Panagiotis Astithas
                        response.sendRedirect(errorUrl);
148 2f551abc Panagiotis Astithas
                        return;
149 2f551abc Panagiotis Astithas
                } else if (password == null || password.isEmpty()) {
150 edf24a21 Panagiotis Astithas
                        String error = encode("No password was specified");
151 2f551abc Panagiotis Astithas
                        String errorUrl = "register.jsp?error=" + error;
152 edf24a21 Panagiotis Astithas
                        errorUrl += "&username=" + encode(username);
153 edf24a21 Panagiotis Astithas
                        errorUrl += "&firstname=" + encode(firstname);
154 edf24a21 Panagiotis Astithas
                        errorUrl += "&lastname=" + encode(lastname);
155 edf24a21 Panagiotis Astithas
                        errorUrl += "&email=" + encode(email);
156 2f551abc Panagiotis Astithas
                        response.sendRedirect(errorUrl);
157 2f551abc Panagiotis Astithas
                        return;
158 2f551abc Panagiotis Astithas
                } else if (!password.equals(password2)) {
159 edf24a21 Panagiotis Astithas
                        String error = encode("Passwords do not match");
160 2f551abc Panagiotis Astithas
                        String errorUrl = "register.jsp?error=" + error;
161 edf24a21 Panagiotis Astithas
                        errorUrl += "&username=" + encode(username);
162 edf24a21 Panagiotis Astithas
                        errorUrl += "&firstname=" + encode(firstname);
163 edf24a21 Panagiotis Astithas
                        errorUrl += "&lastname=" + encode(lastname);
164 edf24a21 Panagiotis Astithas
                        errorUrl += "&email=" + encode(email);
165 2f551abc Panagiotis Astithas
                        response.sendRedirect(errorUrl);
166 2f551abc Panagiotis Astithas
                        return;
167 2f551abc Panagiotis Astithas
                } else if (!"on".equalsIgnoreCase(accept)) {
168 edf24a21 Panagiotis Astithas
                        String error = encode("You must accept the terms and conditions");
169 2f551abc Panagiotis Astithas
                        String errorUrl = "register.jsp?error=" + error;
170 edf24a21 Panagiotis Astithas
                        errorUrl += "&username=" + encode(username);
171 edf24a21 Panagiotis Astithas
                        errorUrl += "&firstname=" + encode(firstname);
172 edf24a21 Panagiotis Astithas
                        errorUrl += "&lastname=" + encode(lastname);
173 edf24a21 Panagiotis Astithas
                        errorUrl += "&email=" + encode(email);
174 2f551abc Panagiotis Astithas
                        response.sendRedirect(errorUrl);
175 2f551abc Panagiotis Astithas
                        return;
176 2f551abc Panagiotis Astithas
                }
177 2f551abc Panagiotis Astithas
178 2f551abc Panagiotis Astithas
                User user = null;
179 2f551abc Panagiotis Astithas
                try {
180 2f551abc Panagiotis Astithas
                        user = getService().findUser(username);
181 2f551abc Panagiotis Astithas
                        if (user != null) {
182 edf24a21 Panagiotis Astithas
                                String error = encode("The username already exists");
183 2f551abc Panagiotis Astithas
                                String errorUrl = "register.jsp?username=&error=" + error;
184 edf24a21 Panagiotis Astithas
                                errorUrl += "&firstname=" + encode(firstname);
185 edf24a21 Panagiotis Astithas
                                errorUrl += "&lastname=" + encode(lastname);
186 edf24a21 Panagiotis Astithas
                                errorUrl += "&email=" + encode(email);
187 2f551abc Panagiotis Astithas
                                response.sendRedirect(errorUrl);
188 2f551abc Panagiotis Astithas
                                return;
189 2f551abc Panagiotis Astithas
                        }
190 3f6fd106 Panagiotis Astithas
                        try {
191 46268014 Panagiotis Astithas
                                getService().createLdapUser(username, firstname, lastname, email, password);
192 46268014 Panagiotis Astithas
                        } catch (Exception e) {
193 46268014 Panagiotis Astithas
                                logger.error(e);
194 edf24a21 Panagiotis Astithas
                                handleException(response, e.getMessage());
195 3f6fd106 Panagiotis Astithas
                                return;
196 3f6fd106 Panagiotis Astithas
                        }
197 2f551abc Panagiotis Astithas
                        final UserDTO userDto = new TransactionHelper<UserDTO>().tryExecute(new Callable<UserDTO>() {
198 2f551abc Panagiotis Astithas
                                @Override
199 2f551abc Panagiotis Astithas
                                public UserDTO call() throws Exception {
200 46268014 Panagiotis Astithas
                                        return getService().createUser(username, firstname + " " + lastname, email, "", "").getDTO();
201 2f551abc Panagiotis Astithas
                                }
202 2f551abc Panagiotis Astithas
203 2f551abc Panagiotis Astithas
                        });
204 2f551abc Panagiotis Astithas
                        new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
205 2f551abc Panagiotis Astithas
                                @Override
206 2f551abc Panagiotis Astithas
                                public Void call() throws Exception {
207 2f551abc Panagiotis Astithas
                                        getService().updateUserPolicyAcceptance(userDto.getId(), true);
208 2f551abc Panagiotis Astithas
                                        return null;
209 2f551abc Panagiotis Astithas
                                }
210 2f551abc Panagiotis Astithas
211 2f551abc Panagiotis Astithas
                        });
212 2f551abc Panagiotis Astithas
                        response.sendRedirect("registered.jsp");
213 2f551abc Panagiotis Astithas
                } catch (RpcException e) {
214 edf24a21 Panagiotis Astithas
                        logger.error(e);
215 edf24a21 Panagiotis Astithas
                        handleException(response, "An error occurred while communicating with the service");
216 2f551abc Panagiotis Astithas
                } catch (DuplicateNameException e) {
217 2f551abc Panagiotis Astithas
                        // Can't happen, but this is more user-friendly than an assert.
218 edf24a21 Panagiotis Astithas
                        logger.error(e);
219 edf24a21 Panagiotis Astithas
                        handleException(response, "The username already exists");
220 2f551abc Panagiotis Astithas
                } catch (ObjectNotFoundException e) {
221 2f551abc Panagiotis Astithas
                        // Can't happen, but this is more user-friendly than an assert.
222 edf24a21 Panagiotis Astithas
                        logger.error(e);
223 edf24a21 Panagiotis Astithas
                        handleException(response, "No username or name was specified");
224 2f551abc Panagiotis Astithas
                } catch (Exception e) {
225 46268014 Panagiotis Astithas
                        logger.error(e);
226 edf24a21 Panagiotis Astithas
                        handleException(response, e.getMessage());
227 2f551abc Panagiotis Astithas
                }
228 2f551abc Panagiotis Astithas
        }
229 edf24a21 Panagiotis Astithas
230 edf24a21 Panagiotis Astithas
        private void handleException(HttpServletResponse response, String error) throws IOException {
231 edf24a21 Panagiotis Astithas
                String errorUrl = "register.jsp?username=&firstname=&lastname=&email=&error=" + encode(error);
232 edf24a21 Panagiotis Astithas
                response.sendRedirect(errorUrl);
233 edf24a21 Panagiotis Astithas
        }
234 2f551abc Panagiotis Astithas
}