Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/server/auth/UserClient.java @ 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 |
parents | |
children | 637d114232b2 |
line wrap: on
line source
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: