Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / CouponHandler.java @ 092998fe

History | View | Annotate | Download (4.8 kB)

1 978061e3 Panagiotis Astithas
/*
2 978061e3 Panagiotis Astithas
 * Copyright 2010 Electronic Business Systems Ltd.
3 978061e3 Panagiotis Astithas
 *
4 978061e3 Panagiotis Astithas
 * This file is part of GSS.
5 978061e3 Panagiotis Astithas
 *
6 978061e3 Panagiotis Astithas
 * GSS is free software: you can redistribute it and/or modify
7 978061e3 Panagiotis Astithas
 * it under the terms of the GNU General Public License as published by
8 978061e3 Panagiotis Astithas
 * the Free Software Foundation, either version 3 of the License, or
9 978061e3 Panagiotis Astithas
 * (at your option) any later version.
10 978061e3 Panagiotis Astithas
 *
11 978061e3 Panagiotis Astithas
 * GSS is distributed in the hope that it will be useful,
12 978061e3 Panagiotis Astithas
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 978061e3 Panagiotis Astithas
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 978061e3 Panagiotis Astithas
 * GNU General Public License for more details.
15 978061e3 Panagiotis Astithas
 *
16 978061e3 Panagiotis Astithas
 * You should have received a copy of the GNU General Public License
17 978061e3 Panagiotis Astithas
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 978061e3 Panagiotis Astithas
 */
19 978061e3 Panagiotis Astithas
package gr.ebs.gss.server;
20 978061e3 Panagiotis Astithas
21 978061e3 Panagiotis Astithas
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 978061e3 Panagiotis Astithas
import gr.ebs.gss.client.exceptions.RpcException;
23 978061e3 Panagiotis Astithas
import gr.ebs.gss.server.domain.Invitation;
24 978061e3 Panagiotis Astithas
import gr.ebs.gss.server.domain.User;
25 978061e3 Panagiotis Astithas
26 978061e3 Panagiotis Astithas
import java.io.IOException;
27 978061e3 Panagiotis Astithas
28 978061e3 Panagiotis Astithas
import javax.servlet.http.HttpServletRequest;
29 978061e3 Panagiotis Astithas
import javax.servlet.http.HttpServletResponse;
30 978061e3 Panagiotis Astithas
31 978061e3 Panagiotis Astithas
import org.apache.commons.logging.Log;
32 978061e3 Panagiotis Astithas
import org.apache.commons.logging.LogFactory;
33 978061e3 Panagiotis Astithas
34 978061e3 Panagiotis Astithas
/**
35 978061e3 Panagiotis Astithas
 * The servlet that handles coupons for quota upgrades.
36 978061e3 Panagiotis Astithas
 *
37 978061e3 Panagiotis Astithas
 * @author past
38 978061e3 Panagiotis Astithas
 */
39 978061e3 Panagiotis Astithas
public class CouponHandler extends BaseServlet {
40 978061e3 Panagiotis Astithas
        /**
41 978061e3 Panagiotis Astithas
         * The request parameter name for the coupon code.
42 978061e3 Panagiotis Astithas
         */
43 978061e3 Panagiotis Astithas
        private static final String CODE_PARAM = "code";
44 978061e3 Panagiotis Astithas
45 978061e3 Panagiotis Astithas
        /**
46 978061e3 Panagiotis Astithas
         * The serial version UID of the class.
47 978061e3 Panagiotis Astithas
         */
48 978061e3 Panagiotis Astithas
        private static final long serialVersionUID = 1L;
49 978061e3 Panagiotis Astithas
50 978061e3 Panagiotis Astithas
        /**
51 978061e3 Panagiotis Astithas
         * The logger.
52 978061e3 Panagiotis Astithas
         */
53 978061e3 Panagiotis Astithas
        private static Log logger = LogFactory.getLog(CouponHandler.class);
54 978061e3 Panagiotis Astithas
55 978061e3 Panagiotis Astithas
        @Override
56 978061e3 Panagiotis Astithas
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
57 978061e3 Panagiotis Astithas
                response.sendRedirect("coupon.jsp");
58 978061e3 Panagiotis Astithas
        }
59 978061e3 Panagiotis Astithas
60 978061e3 Panagiotis Astithas
        @Override
61 978061e3 Panagiotis Astithas
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
62 978061e3 Panagiotis Astithas
                String code = request.getParameter(CODE_PARAM);
63 978061e3 Panagiotis Astithas
                response.setContentType("text/html");
64 978061e3 Panagiotis Astithas
65 978061e3 Panagiotis Astithas
                // Validate input parameters.
66 978061e3 Panagiotis Astithas
                if (code == null || code.isEmpty()) {
67 978061e3 Panagiotis Astithas
                        response.sendRedirect("coupon.jsp?error=" + encode("No code was specified"));
68 978061e3 Panagiotis Astithas
                        return;
69 978061e3 Panagiotis Astithas
                }
70 978061e3 Panagiotis Astithas
71 978061e3 Panagiotis Astithas
                try {
72 978061e3 Panagiotis Astithas
                        Invitation invite = getService().findInvite(code);
73 978061e3 Panagiotis Astithas
                        if (invite == null) {
74 978061e3 Panagiotis Astithas
                                response.sendRedirect("coupon.jsp?code=&error=" + encode("The specified code was not found"));
75 978061e3 Panagiotis Astithas
                                return;
76 978061e3 Panagiotis Astithas
                        }
77 978061e3 Panagiotis Astithas
                        String firstname = invite.getFirstname() == null? "": invite.getFirstname();
78 978061e3 Panagiotis Astithas
                        String lastname = invite.getLastname() == null? "": invite.getLastname();
79 978061e3 Panagiotis Astithas
                        String email = invite.getEmail() == null? "": invite.getEmail();
80 978061e3 Panagiotis Astithas
81 978061e3 Panagiotis Astithas
                        Object usernameAttr = request.getAttribute("REMOTE_USER");
82 978061e3 Panagiotis Astithas
                        if (getConfiguration().getString("testUsername") != null)
83 978061e3 Panagiotis Astithas
                                usernameAttr = getConfiguration().getString("testUsername");
84 978061e3 Panagiotis Astithas
                        if (usernameAttr == null) {
85 978061e3 Panagiotis Astithas
                                Object nameAttr = request.getAttribute("HTTP_SHIB_INETORGPERSON_DISPLAYNAME");
86 978061e3 Panagiotis Astithas
                                Object givennameAttr = request.getAttribute("HTTP_SHIB_INETORGPERSON_GIVENNAME"); // Multi-valued
87 978061e3 Panagiotis Astithas
                                Object cnAttr = request.getAttribute("HTTP_SHIB_PERSON_COMMONNAME"); // Multi-valued
88 978061e3 Panagiotis Astithas
                                Object snAttr = request.getAttribute("HTTP_SHIB_PERSON_SURNAME"); // Multi-valued
89 978061e3 Panagiotis Astithas
                                Object mailAttr = request.getAttribute("HTTP_SHIB_INETORGPERSON_MAIL"); // Multi-valued
90 978061e3 Panagiotis Astithas
                                Object userclassAttr = request.getAttribute("HTTP_SHIB_EP_UNSCOPEDAFFILIATION"); // Multi-valued
91 978061e3 Panagiotis Astithas
                                String authErrorUrl = "authenticationError.jsp";
92 978061e3 Panagiotis Astithas
                                authErrorUrl += "?name=" + (nameAttr==null? "-": nameAttr.toString());
93 978061e3 Panagiotis Astithas
                                authErrorUrl += "&givenname=" + (givennameAttr==null? "-": givennameAttr.toString());
94 978061e3 Panagiotis Astithas
                                authErrorUrl += "&sn=" + (snAttr==null? "-": snAttr.toString());
95 978061e3 Panagiotis Astithas
                                authErrorUrl += "&cn=" + (cnAttr==null? "-": cnAttr.toString());
96 978061e3 Panagiotis Astithas
                                authErrorUrl += "&mail=" + (mailAttr==null? "-": mailAttr.toString());
97 978061e3 Panagiotis Astithas
                                authErrorUrl += "&userclass=" + (userclassAttr==null? "-": userclassAttr.toString());
98 978061e3 Panagiotis Astithas
                                response.sendRedirect(authErrorUrl);
99 978061e3 Panagiotis Astithas
                                return;
100 978061e3 Panagiotis Astithas
                        }
101 978061e3 Panagiotis Astithas
                        String username = decodeAttribute(usernameAttr);
102 978061e3 Panagiotis Astithas
                        User user;
103 978061e3 Panagiotis Astithas
                        try {
104 978061e3 Panagiotis Astithas
                                user = getService().findUser(username);
105 978061e3 Panagiotis Astithas
                                if (user == null) {
106 978061e3 Panagiotis Astithas
                                        handleException(response, encode("Your user account was not found!"));
107 978061e3 Panagiotis Astithas
                                        return;
108 978061e3 Panagiotis Astithas
                                }
109 978061e3 Panagiotis Astithas
                        } catch (RpcException e) {
110 978061e3 Panagiotis Astithas
                                String error = "An error occurred while communicating with the service";
111 978061e3 Panagiotis Astithas
                                logger.error(error, e);
112 978061e3 Panagiotis Astithas
                                handleException(response, encode(error));
113 978061e3 Panagiotis Astithas
                                return;
114 978061e3 Panagiotis Astithas
                        }
115 978061e3 Panagiotis Astithas
                        response.sendRedirect("couponSubmission.jsp?firstname=" +
116 978061e3 Panagiotis Astithas
                                        encode(firstname) +        "&lastname=" + encode(lastname) +
117 978061e3 Panagiotis Astithas
                                        "&email=" + encode(email) + "&code=" + encode(code) +
118 978061e3 Panagiotis Astithas
                                        "&username=" + encode(username));
119 978061e3 Panagiotis Astithas
                } catch (RpcException e) {
120 978061e3 Panagiotis Astithas
                        logger.error(e);
121 978061e3 Panagiotis Astithas
                        handleException(response, encode("An error occurred while communicating with the service"));
122 978061e3 Panagiotis Astithas
                }
123 978061e3 Panagiotis Astithas
        }
124 978061e3 Panagiotis Astithas
125 978061e3 Panagiotis Astithas
        private void handleException(HttpServletResponse response, String error) throws IOException {
126 978061e3 Panagiotis Astithas
                String errorUrl = "coupon.jsp?error=" + error;
127 978061e3 Panagiotis Astithas
                response.sendRedirect(errorUrl);
128 978061e3 Panagiotis Astithas
        }
129 978061e3 Panagiotis Astithas
}