- Initial implementation of quota coupon workflow. Userclass modifications are not...
[pithos] / src / gr / ebs / gss / server / CouponHandler.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.RpcException;
23 import gr.ebs.gss.server.domain.Invitation;
24 import gr.ebs.gss.server.domain.User;
25
26 import java.io.IOException;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * The servlet that handles coupons for quota upgrades.
36  *
37  * @author past
38  */
39 public class CouponHandler extends BaseServlet {
40         /**
41          * The request parameter name for the coupon code.
42          */
43         private static final String CODE_PARAM = "code";
44
45         /**
46          * The serial version UID of the class.
47          */
48         private static final long serialVersionUID = 1L;
49
50         /**
51          * The logger.
52          */
53         private static Log logger = LogFactory.getLog(CouponHandler.class);
54
55         @Override
56         public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
57                 response.sendRedirect("coupon.jsp");
58         }
59
60         @Override
61         public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
62                 String code = request.getParameter(CODE_PARAM);
63                 response.setContentType("text/html");
64
65                 // Validate input parameters.
66                 if (code == null || code.isEmpty()) {
67                         response.sendRedirect("coupon.jsp?error=" + encode("No code was specified"));
68                         return;
69                 }
70
71                 try {
72                         Invitation invite = getService().findInvite(code);
73                         if (invite == null) {
74                                 response.sendRedirect("coupon.jsp?code=&error=" + encode("The specified code was not found"));
75                                 return;
76                         }
77                         String firstname = invite.getFirstname() == null? "": invite.getFirstname();
78                         String lastname = invite.getLastname() == null? "": invite.getLastname();
79                         String email = invite.getEmail() == null? "": invite.getEmail();
80
81                         Object usernameAttr = request.getAttribute("REMOTE_USER");
82                         if (getConfiguration().getString("testUsername") != null)
83                                 usernameAttr = getConfiguration().getString("testUsername");
84                         if (usernameAttr == null) {
85                                 Object nameAttr = request.getAttribute("HTTP_SHIB_INETORGPERSON_DISPLAYNAME");
86                                 Object givennameAttr = request.getAttribute("HTTP_SHIB_INETORGPERSON_GIVENNAME"); // Multi-valued
87                                 Object cnAttr = request.getAttribute("HTTP_SHIB_PERSON_COMMONNAME"); // Multi-valued
88                                 Object snAttr = request.getAttribute("HTTP_SHIB_PERSON_SURNAME"); // Multi-valued
89                                 Object mailAttr = request.getAttribute("HTTP_SHIB_INETORGPERSON_MAIL"); // Multi-valued
90                                 Object userclassAttr = request.getAttribute("HTTP_SHIB_EP_UNSCOPEDAFFILIATION"); // Multi-valued
91                                 String authErrorUrl = "authenticationError.jsp";
92                                 authErrorUrl += "?name=" + (nameAttr==null? "-": nameAttr.toString());
93                                 authErrorUrl += "&givenname=" + (givennameAttr==null? "-": givennameAttr.toString());
94                                 authErrorUrl += "&sn=" + (snAttr==null? "-": snAttr.toString());
95                                 authErrorUrl += "&cn=" + (cnAttr==null? "-": cnAttr.toString());
96                                 authErrorUrl += "&mail=" + (mailAttr==null? "-": mailAttr.toString());
97                                 authErrorUrl += "&userclass=" + (userclassAttr==null? "-": userclassAttr.toString());
98                                 response.sendRedirect(authErrorUrl);
99                                 return;
100                         }
101                         String username = decodeAttribute(usernameAttr);
102                         User user;
103                         try {
104                                 user = getService().findUser(username);
105                                 if (user == null) {
106                                         handleException(response, encode("Your user account was not found!"));
107                                         return;
108                                 }
109                         } catch (RpcException e) {
110                                 String error = "An error occurred while communicating with the service";
111                                 logger.error(error, e);
112                                 handleException(response, encode(error));
113                                 return;
114                         }
115                         response.sendRedirect("couponSubmission.jsp?firstname=" +
116                                         encode(firstname) +     "&lastname=" + encode(lastname) +
117                                         "&email=" + encode(email) + "&code=" + encode(code) +
118                                         "&username=" + encode(username));
119                 } catch (RpcException e) {
120                         logger.error(e);
121                         handleException(response, encode("An error occurred while communicating with the service"));
122                 }
123         }
124
125         private void handleException(HttpServletResponse response, String error) throws IOException {
126                 String errorUrl = "coupon.jsp?error=" + error;
127                 response.sendRedirect(errorUrl);
128         }
129 }