comparison 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
comparison
equal deleted inserted replaced
2983:725470fc57d2 2984:06d999e95615
1 package de.intevation.flys.client.server.auth;
2
3 import javax.xml.xpath.XPathConstants;
4
5 import org.apache.log4j.Logger;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11
12 import de.intevation.artifacts.common.ArtifactNamespaceContext;
13 import de.intevation.artifacts.common.utils.XMLUtils;
14
15 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
16 import de.intevation.artifacts.httpclient.http.HttpClient;
17 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
18
19 /**
20 * UserClient is a class to allow easier communication
21 * with the REST based artifact user protocol
22 */
23 public class UserClient {
24
25 private static final Logger logger = Logger.getLogger(UserClient.class);
26
27 private String url;
28
29 public UserClient(String url) {
30 this.url = url;
31 }
32
33 public boolean userExists(User user) throws ConnectionException {
34 NodeList users = this.listUsers();
35
36 if (users == null || users.getLength() == 0) {
37 return false;
38 }
39 for(int i=0; i < users.getLength(); i++) {
40 Node usernode = users.item(i);
41 String name = XMLUtils.xpathString(
42 usernode, "@art:name", ArtifactNamespaceContext.INSTANCE);
43 if (name.equals(user.getName())) {
44 return true;
45 }
46 }
47 return false;
48 }
49
50 public boolean createUser(User user) throws ConnectionException {
51 logger.debug("Creating new user " + user.getName());
52 HttpClient client = new HttpClientImpl(this.url);
53
54 Document document = XMLUtils.newDocument();
55
56 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
57 document,
58 ArtifactNamespaceContext.NAMESPACE_URI,
59 ArtifactNamespaceContext.NAMESPACE_PREFIX
60 );
61
62 Element action = creator.create("action");
63
64 Element type = creator.create("type");
65 type.setAttribute("name", "create");
66 Element artuser = creator.create("user");
67 artuser.setAttribute("name", user.getName());
68
69 //TODO create roles
70 action.appendChild(type);
71 action.appendChild(artuser);
72 document.appendChild(action);
73
74 logger.debug("Create user request xml: " + XMLUtils.toString(document));
75
76 Document resp = client.createUser(document);
77
78 logger.debug("Create user response xml: " + XMLUtils.toString(resp));
79
80 String XPATH_RESPONSE = "/art:result";
81 Node nresult = (Node) XMLUtils.xpath(
82 resp,
83 XPATH_RESPONSE,
84 XPathConstants.NODE,
85 ArtifactNamespaceContext.INSTANCE);
86 String result = nresult.getTextContent();
87 return (result != null && result.equalsIgnoreCase("success"));
88 }
89
90 public NodeList listUsers() throws ConnectionException {
91 HttpClient client = new HttpClientImpl(this.url);
92
93 Document users = (Document) client.listUsers();
94
95 String XPATH_USERS = "/art:users/art:user";
96
97 return (NodeList) XMLUtils.xpath(
98 users,
99 XPATH_USERS,
100 XPathConstants.NODESET,
101 ArtifactNamespaceContext.INSTANCE);
102 }
103 }
104 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:
105

http://dive4elements.wald.intevation.org