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 gr.ebs.gss.server.domain.UserClass;
22 import gr.ebs.gss.server.ejb.TransactionHelper;
24 import java.io.IOException;
25 import java.util.concurrent.Callable;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
31 * The servlet that handles user registration.
35 public class CouponVerifier extends BaseServlet {
37 * The request parameter name for the verification flag.
39 private static final String VERIFY_PARAM = "verify";
42 * The request parameter name for the coupon code.
44 private static final String CODE_PARAM = "code";
47 * The request parameter name for the firstname.
49 private static final String FIRSTNAME_PARAM = "firstname";
52 * The request parameter name for the lastname.
54 private static final String LASTNAME_PARAM = "lastname";
57 * The request parameter name for the username.
59 private static final String USERNAME_PARAM = "username";
62 * The request parameter name for the e-mail.
64 private static final String EMAIL_PARAM = "email";
67 * The serial version UID of the class.
69 private static final long serialVersionUID = 1L;
72 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
73 String firstname = request.getParameter(FIRSTNAME_PARAM);
74 String lastname = request.getParameter(LASTNAME_PARAM);
75 String email = request.getParameter(EMAIL_PARAM);
76 final String username = request.getParameter(USERNAME_PARAM);
77 final String code = request.getParameter(CODE_PARAM);
78 String verify = request.getParameter(VERIFY_PARAM);
79 response.setContentType("text/html");
81 // Validate input parameters.
82 if (username == null || username.isEmpty()) {
83 handleException(response, "No username was specified");
85 } else if (firstname == null || firstname.isEmpty()) {
86 handleException(response, "No firstname was specified");
88 } else if (lastname == null || lastname.isEmpty()) {
89 handleException(response, "No lastname was specified");
91 } else if (email == null || email.isEmpty()) {
92 handleException(response, "No email was specified");
94 } else if (code == null || code.isEmpty()) {
95 handleException(response, "No code was specified");
97 } else if (!"on".equalsIgnoreCase(verify)) {
98 String error = encode("You must verify that the coupon belongs to you");
99 String errorUrl = "couponSubmission.jsp?error=" + error;
100 errorUrl += "&username=" + username;
101 errorUrl += "&firstname=" + firstname;
102 errorUrl += "&lastname=" + lastname;
103 errorUrl += "&code=" + code;
104 errorUrl += "&email=" + email;
105 response.sendRedirect(errorUrl);
110 UserClass newClass = new TransactionHelper<UserClass>().tryExecute(new Callable<UserClass>() {
112 public UserClass call() throws Exception {
113 return getService().upgradeUserClass(username, code);
117 response.sendRedirect("couponSubmitted.jsp?newQuota=" + newClass.getQuotaAsString());
118 } catch (Exception e) {
119 handleException(response, e.getMessage());
123 private void handleException(HttpServletResponse response, String error) throws IOException {
124 String errorUrl = "coupon.jsp?error=" + error;
125 response.sendRedirect(errorUrl);