view flys-client/src/main/java/de/intevation/flys/client/server/auth/UserClient.java @ 3505:637d114232b2

Don't use XPath Query to get attribute of a XML Node flys-client/trunk@5257 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 10:52:27 +0000
parents 06d999e95615
children f2a4e6e92ffd
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++) {
            Element usernode = (Element)users.item(i);
            String name = usernode.getAttributeNS(
                    ArtifactNamespaceContext.NAMESPACE_URI, "name");

            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:

http://dive4elements.wald.intevation.org