2 * Copyright 2009 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 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;
27 import java.io.IOException;
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;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
41 * The servlet that handles user policy acceptance.
45 public class Policy extends HttpServlet {
47 * The request parameter name for the acceptance flag.
49 private static final String ACCEPT_PARAM = "accept";
52 * The request parameter name for the original query string to the jsp.
54 private static final String QUERY_PARAM = "queryString";
57 * The request parameter name for the username.
59 private static final String USER_PARAM = "user";
62 * The serial version UID of the class.
64 private static final long serialVersionUID = 1L;
69 private static Log logger = LogFactory.getLog(Policy.class);
72 * A helper method that retrieves a reference to the ExternalAPI bean and
73 * stores it for future use.
75 * @return an ExternalAPI instance
76 * @throws RpcException in case an error occurs
78 private ExternalAPI getService() throws RpcException {
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");
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);
101 user = getService().findUser(username);
103 response.sendError(HttpServletResponse.SC_NOT_FOUND);
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);
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);