Add a temporary hard-coded notice for the extended token validity period. This should...
[pithos] / gss / src / gr / ebs / gss / server / Policy.java
1 /*
2  * Copyright 2009 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.ObjectNotFoundException;
23 import gr.ebs.gss.client.exceptions.RpcException;
24 import gr.ebs.gss.server.domain.User;
25 import gr.ebs.gss.server.ejb.ExternalAPI;
26
27 import java.io.IOException;
28
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import javax.rmi.PortableRemoteObject;
33 import javax.servlet.http.HttpServlet;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * The servlet that handles user policy acceptance.
42  *
43  * @author past
44  */
45 public class Policy extends HttpServlet {
46         /**
47          * The request parameter name for the acceptance flag.
48          */
49         private static final String ACCEPT_PARAM = "accept";
50
51         /**
52          * The request parameter name for the original query string to the jsp.
53          */
54         private static final String QUERY_PARAM = "queryString";
55
56         /**
57          * The request parameter name for the username.
58          */
59         private static final String USER_PARAM = "user";
60
61         /**
62          * The serial version UID of the class.
63          */
64         private static final long serialVersionUID = 1L;
65
66         /**
67          * The logger.
68          */
69         private static Log logger = LogFactory.getLog(Policy.class);
70
71         /**
72          * A helper method that retrieves a reference to the ExternalAPI bean and
73          * stores it for future use.
74          *
75          * @return an ExternalAPI instance
76          * @throws RpcException in case an error occurs
77          */
78         private ExternalAPI getService() throws RpcException {
79                 try {
80                         final Context ctx = new InitialContext();
81                         final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
82                         return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
83                 } catch (final NamingException e) {
84                         logger.error("Unable to retrieve the ExternalAPI EJB", e);
85                         throw new RpcException("An error occurred while contacting the naming service");
86                 }
87         }
88
89         @Override
90         public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
91                 String queryString = request.getParameter(QUERY_PARAM);
92                 String accept = request.getParameter(ACCEPT_PARAM);
93                 String username = request.getParameter(USER_PARAM);
94                 response.setContentType("text/html");
95                 if (username == null) {
96                         response.sendError(HttpServletResponse.SC_NOT_FOUND);
97                         return;
98                 }
99                 User user = null;
100                 try {
101                         user = getService().findUser(username);
102                         if (user == null) {
103                                 response.sendError(HttpServletResponse.SC_NOT_FOUND);
104                                 return;
105                         }
106                         if ("on".equalsIgnoreCase(accept))
107                                 user = getService().updateUserPolicyAcceptance(user.getId(), true);
108                         response.sendRedirect(request.getContextPath()+ "/login?" + queryString);
109                 } catch (RpcException e) {
110                         String error = "An error occurred while communicating with the service";
111                         logger.error(error, e);
112                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
113                         return;
114                 } catch (ObjectNotFoundException e) {
115                         String error = "User " + username + " was not found";
116                         logger.warn(error, e);
117                         response.sendError(HttpServletResponse.SC_NOT_FOUND, error);
118                         return;
119                 }
120         }
121 }