Forbid logins from disabled users.
[pithos] / src / gr / ebs / gss / server / NonceIssuer.java
1 /*
2  * Copyright 2008, 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.Nonce;
24 import gr.ebs.gss.server.domain.User;
25
26 import java.io.IOException;
27 import java.io.PrintWriter;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * The servlet that handles nonce creation.
37  *
38  * @author past
39  */
40 public class NonceIssuer extends BaseServlet {
41         /**
42          * The serial version UID of the class.
43          */
44         private static final long serialVersionUID = 1L;
45
46         /**
47          * The request parameter name for the user.
48          */
49         private static final String USER_PARAM = "user";
50
51         /**
52          * The logger.
53          */
54         private static Log logger = LogFactory.getLog(NonceIssuer.class);
55
56         @Override
57         public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
58                 String username = request.getParameter(USER_PARAM);
59                 User user = null;
60                 Nonce nonce = null;
61                 if (username == null) {
62                         String error = "No username supplied";
63                         logger.info(error);
64                         response.setContentType("text/html");
65                         response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
66                         return;
67                 }
68                 try {
69                         user = getService().findUser(username);
70                         if (user == null) {
71                                 String error = "User was not found";
72                                 logger.error(error);
73                                 response.setContentType("text/html");
74                                 response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
75                                 return;
76                         }
77                         nonce = getService().createNonce(user.getId());
78                 } catch (RpcException e) {
79                         String error = "An error occurred while communicating with the service";
80                         logger.error(error, e);
81                         response.setContentType("text/html");
82                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
83                         return;
84                 } catch (ObjectNotFoundException e) {
85                         // The user might not be found in createNonce() since there
86                         // is no transaction spanning the consecutive service calls.
87                         String error = "The user was not found";
88                         logger.error(error, e);
89                         response.setContentType("text/html");
90                         response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
91                         return;
92                 }
93                 if (logger.isDebugEnabled())
94                         logger.debug("user: "+user.getUsername()+" nonce: "+nonce.getEncodedNonce());
95                 response.setContentType("text/plain");
96             PrintWriter out = response.getWriter();
97             out.println(nonce.getEncodedNonce());
98         }
99 }