Mercurial > dive4elements > river
changeset 2984:06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
If a logged in user is not known add him to the database via the REST protocol.
flys-client/trunk@4994 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Bjoern Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 13 Jul 2012 13:38:32 +0000 (2012-07-13) |
parents | 725470fc57d2 |
children | 1f64ee424f92 |
files | flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java flys-client/src/main/java/de/intevation/flys/client/server/auth/UserClient.java |
diffstat | 3 files changed, 128 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-client/ChangeLog Fri Jul 13 11:31:25 2012 +0000 +++ b/flys-client/ChangeLog Fri Jul 13 13:38:32 2012 +0000 @@ -1,3 +1,11 @@ +2012-07-13 Bj�rn Ricks <bjoern.ricks@intevation.de> + + * src/main/java/de/intevation/flys/client/server/LoginServlet.java, + src/main/java/de/intevation/flys/client/server/auth/UserClient.java: + Add UserClient class to handle REST communication for user related + interfaces. If a logged in user is not known add him to the database + via the REST protocol. + 2012-07-13 Christian Lins <christian.lins@intevation.de> * src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
--- a/flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java Fri Jul 13 11:31:25 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java Fri Jul 13 13:38:32 2012 +0000 @@ -14,6 +14,7 @@ import de.intevation.flys.client.server.auth.AuthenticationException; import de.intevation.flys.client.server.auth.AuthenticationFactory; import de.intevation.flys.client.server.auth.User; +import de.intevation.flys.client.server.auth.UserClient; public class LoginServlet extends HttpServlet { @@ -24,9 +25,12 @@ } private void redirectFailure(HttpServletResponse resp, Exception e) throws IOException { - resp.sendRedirect("/login.jsp?error=" + e.getMessage()); + this.redirectFailure(resp, e.getMessage()); } + private void redirectFailure(HttpServletResponse resp, String message) throws IOException { + resp.sendRedirect("/login.jsp?error=" + message); + } private void redirectSuccess(HttpServletResponse resp, String uri) throws IOException { if (uri == null) { uri = "/FLYS.html"; @@ -60,8 +64,17 @@ logger.debug("Athentication not successful"); this.redirectFailure(resp); } + User user = aresp.getUser(); + + String url = getServletContext().getInitParameter("server-url"); + UserClient client = new UserClient(url); + if (!client.userExists(user)) { + if (!client.createUser(user)) { + this.redirectFailure(resp, "Could not create new user"); + } + } + HttpSession session = req.getSession(); - User user = aresp.getUser(); session.setAttribute("user", user); String uri = (String)session.getAttribute("requesturi");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/UserClient.java Fri Jul 13 13:38:32 2012 +0000 @@ -0,0 +1,105 @@ +package de.intevation.flys.client.server.auth; + +import javax.xml.xpath.XPathConstants; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifacts.httpclient.exceptions.ConnectionException; +import de.intevation.artifacts.httpclient.http.HttpClient; +import de.intevation.artifacts.httpclient.http.HttpClientImpl; + +/** + * UserClient is a class to allow easier communication + * with the REST based artifact user protocol + */ +public class UserClient { + + private static final Logger logger = Logger.getLogger(UserClient.class); + + private String url; + + public UserClient(String url) { + this.url = url; + } + + public boolean userExists(User user) throws ConnectionException { + NodeList users = this.listUsers(); + + if (users == null || users.getLength() == 0) { + return false; + } + for(int i=0; i < users.getLength(); i++) { + Node usernode = users.item(i); + String name = XMLUtils.xpathString( + usernode, "@art:name", ArtifactNamespaceContext.INSTANCE); + if (name.equals(user.getName())) { + return true; + } + } + return false; + } + + public boolean createUser(User user) throws ConnectionException { + logger.debug("Creating new user " + user.getName()); + HttpClient client = new HttpClientImpl(this.url); + + Document document = XMLUtils.newDocument(); + + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + document, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX + ); + + Element action = creator.create("action"); + + Element type = creator.create("type"); + type.setAttribute("name", "create"); + Element artuser = creator.create("user"); + artuser.setAttribute("name", user.getName()); + + //TODO create roles + action.appendChild(type); + action.appendChild(artuser); + document.appendChild(action); + + logger.debug("Create user request xml: " + XMLUtils.toString(document)); + + Document resp = client.createUser(document); + + logger.debug("Create user response xml: " + XMLUtils.toString(resp)); + + String XPATH_RESPONSE = "/art:result"; + Node nresult = (Node) XMLUtils.xpath( + resp, + XPATH_RESPONSE, + XPathConstants.NODE, + ArtifactNamespaceContext.INSTANCE); + String result = nresult.getTextContent(); + return (result != null && result.equalsIgnoreCase("success")); + } + + public NodeList listUsers() throws ConnectionException { + HttpClient client = new HttpClientImpl(this.url); + + Document users = (Document) client.listUsers(); + + String XPATH_USERS = "/art:users/art:user"; + + return (NodeList) XMLUtils.xpath( + users, + XPATH_USERS, + XPathConstants.NODESET, + ArtifactNamespaceContext.INSTANCE); + } +} +// vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: +