root / src / gr / ebs / gss / server / NonceIssuer.java @ 4684df80
History | View | Annotate | Download (4.1 kB)
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 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.Nonce; |
25 |
import gr.ebs.gss.server.domain.User; |
26 |
import gr.ebs.gss.server.ejb.ExternalAPI; |
27 |
|
28 |
import java.io.IOException; |
29 |
import java.io.PrintWriter; |
30 |
|
31 |
import javax.naming.Context; |
32 |
import javax.naming.InitialContext; |
33 |
import javax.naming.NamingException; |
34 |
import javax.rmi.PortableRemoteObject; |
35 |
import javax.servlet.http.HttpServlet; |
36 |
import javax.servlet.http.HttpServletRequest; |
37 |
import javax.servlet.http.HttpServletResponse; |
38 |
|
39 |
import org.apache.commons.logging.Log; |
40 |
import org.apache.commons.logging.LogFactory; |
41 |
|
42 |
/**
|
43 |
* The servlet that handles nonce creation.
|
44 |
*
|
45 |
* @author past
|
46 |
*/
|
47 |
public class NonceIssuer extends HttpServlet { |
48 |
/**
|
49 |
* The serial version UID of the class.
|
50 |
*/
|
51 |
private static final long serialVersionUID = 1L; |
52 |
|
53 |
/**
|
54 |
* The request parameter name for the user.
|
55 |
*/
|
56 |
private static final String USER_PARAM = "user"; |
57 |
|
58 |
/**
|
59 |
* The logger.
|
60 |
*/
|
61 |
private static Log logger = LogFactory.getLog(NonceIssuer.class); |
62 |
|
63 |
/**
|
64 |
* A helper method that retrieves a reference to the ExternalAPI bean and
|
65 |
* stores it for future use.
|
66 |
*
|
67 |
* @return an ExternalAPI instance
|
68 |
* @throws RpcException in case an error occurs
|
69 |
*/
|
70 |
private ExternalAPI getService() throws RpcException { |
71 |
try {
|
72 |
final Context ctx = new InitialContext(); |
73 |
final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath")); |
74 |
return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class); |
75 |
} catch (final NamingException e) { |
76 |
logger.error("Unable to retrieve the ExternalAPI EJB", e);
|
77 |
throw new RpcException("An error occurred while contacting the naming service"); |
78 |
} |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { |
83 |
String username = request.getParameter(USER_PARAM);
|
84 |
User user = null;
|
85 |
Nonce nonce = null;
|
86 |
if (username == null) { |
87 |
String error = "No username supplied"; |
88 |
logger.info(error); |
89 |
response.setContentType("text/html");
|
90 |
response.sendError(HttpServletResponse.SC_FORBIDDEN, error); |
91 |
return;
|
92 |
} |
93 |
try {
|
94 |
user = getService().findUser(username); |
95 |
if (user == null) { |
96 |
String error = "User was not found"; |
97 |
logger.error(error); |
98 |
response.setContentType("text/html");
|
99 |
response.sendError(HttpServletResponse.SC_FORBIDDEN, error); |
100 |
return;
|
101 |
} |
102 |
nonce = getService().createNonce(user.getId()); |
103 |
} catch (RpcException e) {
|
104 |
String error = "An error occurred while communicating with the service"; |
105 |
logger.error(error, e); |
106 |
response.setContentType("text/html");
|
107 |
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error); |
108 |
return;
|
109 |
} catch (ObjectNotFoundException e) {
|
110 |
// The user might not be found in createNonce() since there
|
111 |
// is no transaction spanning the consecutive service calls.
|
112 |
String error = "The user was not found"; |
113 |
logger.error(error, e); |
114 |
response.setContentType("text/html");
|
115 |
response.sendError(HttpServletResponse.SC_FORBIDDEN, error); |
116 |
return;
|
117 |
} |
118 |
if (logger.isDebugEnabled())
|
119 |
logger.debug("user: "+user.getUsername()+" nonce: "+nonce.getEncodedNonce()); |
120 |
response.setContentType("text/plain");
|
121 |
PrintWriter out = response.getWriter();
|
122 |
out.println(nonce.getEncodedNonce()); |
123 |
} |
124 |
} |