milton webdav support - TODO: get rid of spring dependencies
[pithos] / 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 gr.ebs.gss.client.exceptions.ObjectNotFoundException;
22 import gr.ebs.gss.client.exceptions.RpcException;
23 import gr.ebs.gss.server.domain.User;
24
25 import java.io.IOException;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * The servlet that handles user policy acceptance.
35  *
36  * @author past
37  */
38 public class Policy extends BaseServlet {
39         /**
40          * The request parameter name for the acceptance flag.
41          */
42         private static final String ACCEPT_PARAM = "accept";
43
44         /**
45          * The request parameter name for the original query string to the jsp.
46          */
47         private static final String QUERY_PARAM = "queryString";
48
49         /**
50          * The request parameter name for the username.
51          */
52         private static final String USER_PARAM = "user";
53
54         /**
55          * The serial version UID of the class.
56          */
57         private static final long serialVersionUID = 1L;
58
59         /**
60          * The logger.
61          */
62         private static Log logger = LogFactory.getLog(Policy.class);
63
64         @Override
65         public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
66                 String queryString = request.getParameter(QUERY_PARAM);
67                 String accept = request.getParameter(ACCEPT_PARAM);
68                 String username = request.getParameter(USER_PARAM);
69                 response.setContentType("text/html");
70                 if (username == null) {
71                         response.sendError(HttpServletResponse.SC_NOT_FOUND);
72                         return;
73                 }
74                 User user = null;
75                 try {
76                         user = getService().findUser(username);
77                         if (user == null) {
78                                 response.sendError(HttpServletResponse.SC_NOT_FOUND);
79                                 return;
80                         }
81                         if ("on".equalsIgnoreCase(accept))
82                                 user = getService().updateUserPolicyAcceptance(user.getId(), true);
83                         response.sendRedirect(request.getContextPath()+ "/login?" + queryString);
84                 } catch (RpcException e) {
85                         String error = "An error occurred while communicating with the service";
86                         logger.error(error, e);
87                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
88                         return;
89                 } catch (ObjectNotFoundException e) {
90                         String error = "User " + username + " was not found";
91                         logger.warn(error, e);
92                         response.sendError(HttpServletResponse.SC_NOT_FOUND, error);
93                         return;
94                 }
95         }
96 }