teichmann@5835: package org.dive4elements.river.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: teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; bjoern@2984: teichmann@5835: import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.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@3507: if (user == null) { bjoern@2984: return false; bjoern@2984: } sascha@3695: sascha@3695: Element data = this.findUser(user); sascha@3695: bjoern@3524: String XPATH_USERACCOUNT = "/art:user/art:account/@art:name"; bjoern@3505: bjoern@3507: String account = XMLUtils.xpathString( bjoern@3507: data, XPATH_USERACCOUNT, ArtifactNamespaceContext.INSTANCE); bjoern@3507: bjoern@3507: if (account == null) { bjoern@3507: return false; bjoern@2984: } bjoern@3507: bjoern@3507: return account.equals(user.getAccount()); bjoern@2984: } bjoern@2984: bjoern@2984: public boolean createUser(User user) throws ConnectionException { christian@3696: if(user == null) { christian@3696: logger.warn("createUser: given user is null"); christian@3696: return false; christian@3696: } sascha@3697: 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@3507: bjoern@3507: public Element findUser(User user) throws ConnectionException { christian@3696: if(user == null) { christian@3696: throw new IllegalArgumentException("user is null"); christian@3696: } sascha@3697: bjoern@3507: HttpClient client = new HttpClientImpl(this.url); bjoern@3507: bjoern@3507: Document document = XMLUtils.newDocument(); bjoern@3507: bjoern@3507: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( bjoern@3507: document, bjoern@3507: ArtifactNamespaceContext.NAMESPACE_URI, bjoern@3507: ArtifactNamespaceContext.NAMESPACE_PREFIX bjoern@3507: ); bjoern@3507: bjoern@3507: Element action = creator.create("action"); bjoern@3507: bjoern@3507: Element type = creator.create("type"); bjoern@3507: type.setAttribute("name", "find"); bjoern@3507: Element account = creator.create("account"); bjoern@3507: account.setAttribute("name", user.getAccount()); bjoern@3507: bjoern@3507: action.appendChild(type); bjoern@3507: action.appendChild(account); bjoern@3507: document.appendChild(action); bjoern@3507: sascha@3695: boolean debug = logger.isDebugEnabled(); sascha@3695: sascha@3695: if (debug) { sascha@3695: logger.debug("Find user request xml: " + sascha@3695: XMLUtils.toString(document)); sascha@3695: } bjoern@3507: bjoern@3507: Document resp = client.findUser(document); bjoern@3507: sascha@3695: if (debug) { sascha@3695: logger.debug("Find user request response xml: " + sascha@3695: XMLUtils.toString(resp)); sascha@3695: } bjoern@3507: bjoern@3507: return resp.getDocumentElement(); bjoern@3507: } bjoern@2984: } bjoern@2984: // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: bjoern@2984: