2 * Copyright 2008, 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 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;
26 import java.io.IOException;
27 import java.io.PrintWriter;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
36 * The servlet that handles nonce creation.
40 public class NonceIssuer extends BaseServlet {
42 * The serial version UID of the class.
44 private static final long serialVersionUID = 1L;
47 * The request parameter name for the user.
49 private static final String USER_PARAM = "user";
54 private static Log logger = LogFactory.getLog(NonceIssuer.class);
57 public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
58 String username = request.getParameter(USER_PARAM);
61 if (username == null) {
62 String error = "No username supplied";
64 response.setContentType("text/html");
65 response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
69 user = getService().findUser(username);
71 String error = "User was not found";
73 response.setContentType("text/html");
74 response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
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);
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);
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());