bjoern@2984: package de.intevation.flys.client.server.auth; bjoern@2984: bjoern@2984: import javax.xml.xpath.XPathConstants; bjoern@2984: bjoern@2984: import org.apache.log4j.Logger; bjoern@2984: bjoern@2984: import org.w3c.dom.Document; bjoern@2984: import org.w3c.dom.Element; bjoern@2984: import org.w3c.dom.Node; bjoern@2984: import org.w3c.dom.NodeList; bjoern@2984: bjoern@2984: import de.intevation.artifacts.common.ArtifactNamespaceContext; bjoern@2984: import de.intevation.artifacts.common.utils.XMLUtils; bjoern@2984: bjoern@2984: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; bjoern@2984: import de.intevation.artifacts.httpclient.http.HttpClient; bjoern@2984: import de.intevation.artifacts.httpclient.http.HttpClientImpl; bjoern@2984: bjoern@2984: /** bjoern@2984: * UserClient is a class to allow easier communication bjoern@2984: * with the REST based artifact user protocol bjoern@2984: */ bjoern@2984: public class UserClient { bjoern@2984: bjoern@2984: private static final Logger logger = Logger.getLogger(UserClient.class); bjoern@2984: bjoern@2984: private String url; bjoern@2984: bjoern@2984: public UserClient(String url) { bjoern@2984: this.url = url; bjoern@2984: } bjoern@2984: bjoern@2984: public boolean userExists(User user) throws ConnectionException { bjoern@2984: NodeList users = this.listUsers(); bjoern@2984: bjoern@2984: if (users == null || users.getLength() == 0) { bjoern@2984: return false; bjoern@2984: } bjoern@2984: for(int i=0; i < users.getLength(); i++) { bjoern@3505: Element usernode = (Element)users.item(i); bjoern@3505: String name = usernode.getAttributeNS( bjoern@3505: ArtifactNamespaceContext.NAMESPACE_URI, "name"); bjoern@3505: bjoern@2984: if (name.equals(user.getName())) { bjoern@2984: return true; bjoern@2984: } bjoern@2984: } bjoern@2984: return false; bjoern@2984: } bjoern@2984: bjoern@2984: public boolean createUser(User user) throws ConnectionException { bjoern@2984: logger.debug("Creating new user " + user.getName()); bjoern@2984: HttpClient client = new HttpClientImpl(this.url); bjoern@2984: bjoern@2984: Document document = XMLUtils.newDocument(); bjoern@2984: bjoern@2984: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( bjoern@2984: document, bjoern@2984: ArtifactNamespaceContext.NAMESPACE_URI, bjoern@2984: ArtifactNamespaceContext.NAMESPACE_PREFIX bjoern@2984: ); bjoern@2984: bjoern@2984: Element action = creator.create("action"); bjoern@2984: bjoern@2984: Element type = creator.create("type"); bjoern@2984: type.setAttribute("name", "create"); bjoern@2984: Element artuser = creator.create("user"); bjoern@2984: artuser.setAttribute("name", user.getName()); bjoern@3506: Element account = creator.create("account"); bjoern@3506: account.setAttribute("name", user.getAccount()); bjoern@2984: bjoern@2984: //TODO create roles bjoern@3506: artuser.appendChild(account); bjoern@2984: action.appendChild(type); bjoern@2984: action.appendChild(artuser); bjoern@2984: document.appendChild(action); bjoern@2984: bjoern@2984: logger.debug("Create user request xml: " + XMLUtils.toString(document)); bjoern@2984: bjoern@2984: Document resp = client.createUser(document); bjoern@2984: bjoern@2984: logger.debug("Create user response xml: " + XMLUtils.toString(resp)); bjoern@2984: bjoern@2984: String XPATH_RESPONSE = "/art:result"; bjoern@2984: Node nresult = (Node) XMLUtils.xpath( bjoern@2984: resp, bjoern@2984: XPATH_RESPONSE, bjoern@2984: XPathConstants.NODE, bjoern@2984: ArtifactNamespaceContext.INSTANCE); bjoern@2984: String result = nresult.getTextContent(); bjoern@2984: return (result != null && result.equalsIgnoreCase("success")); bjoern@2984: } bjoern@2984: bjoern@2984: public NodeList listUsers() throws ConnectionException { bjoern@2984: HttpClient client = new HttpClientImpl(this.url); bjoern@2984: bjoern@2984: Document users = (Document) client.listUsers(); bjoern@2984: bjoern@2984: String XPATH_USERS = "/art:users/art:user"; bjoern@2984: bjoern@2984: return (NodeList) XMLUtils.xpath( bjoern@2984: users, bjoern@2984: XPATH_USERS, bjoern@2984: XPathConstants.NODESET, bjoern@2984: ArtifactNamespaceContext.INSTANCE); bjoern@2984: } bjoern@2984: } bjoern@2984: // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: bjoern@2984: