annotate flys-client/src/main/java/de/intevation/flys/client/server/auth/UserClient.java @ 3506:f2a4e6e92ffd

Send account data to artifact rest interface to create a user flys-client/trunk@5258 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 11:42:31 +0000
parents 637d114232b2
children 59c77a9551c8
rev   line source
2984
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.client.server.auth;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
2
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
3 import javax.xml.xpath.XPathConstants;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
4
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
5 import org.apache.log4j.Logger;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
6
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
7 import org.w3c.dom.Document;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
8 import org.w3c.dom.Element;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9 import org.w3c.dom.Node;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10 import org.w3c.dom.NodeList;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12 import de.intevation.artifacts.common.ArtifactNamespaceContext;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
13 import de.intevation.artifacts.common.utils.XMLUtils;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
14
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
15 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
16 import de.intevation.artifacts.httpclient.http.HttpClient;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
18
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
19 /**
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 * UserClient is a class to allow easier communication
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
21 * with the REST based artifact user protocol
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 */
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 public class UserClient {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25 private static final Logger logger = Logger.getLogger(UserClient.class);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27 private String url;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
28
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
29 public UserClient(String url) {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30 this.url = url;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
31 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
33 public boolean userExists(User user) throws ConnectionException {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
34 NodeList users = this.listUsers();
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
35
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
36 if (users == null || users.getLength() == 0) {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
37 return false;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
38 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
39 for(int i=0; i < users.getLength(); i++) {
3505
637d114232b2 Don't use XPath Query to get attribute of a XML Node
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2984
diff changeset
40 Element usernode = (Element)users.item(i);
637d114232b2 Don't use XPath Query to get attribute of a XML Node
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2984
diff changeset
41 String name = usernode.getAttributeNS(
637d114232b2 Don't use XPath Query to get attribute of a XML Node
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2984
diff changeset
42 ArtifactNamespaceContext.NAMESPACE_URI, "name");
637d114232b2 Don't use XPath Query to get attribute of a XML Node
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2984
diff changeset
43
2984
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
44 if (name.equals(user.getName())) {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45 return true;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
46 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 return false;
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
49 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
50
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 public boolean createUser(User user) throws ConnectionException {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52 logger.debug("Creating new user " + user.getName());
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 HttpClient client = new HttpClientImpl(this.url);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
54
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
55 Document document = XMLUtils.newDocument();
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
56
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
57 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
58 document,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
59 ArtifactNamespaceContext.NAMESPACE_URI,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
60 ArtifactNamespaceContext.NAMESPACE_PREFIX
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
61 );
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
62
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
63 Element action = creator.create("action");
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
64
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
65 Element type = creator.create("type");
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
66 type.setAttribute("name", "create");
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
67 Element artuser = creator.create("user");
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
68 artuser.setAttribute("name", user.getName());
3506
f2a4e6e92ffd Send account data to artifact rest interface to create a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3505
diff changeset
69 Element account = creator.create("account");
f2a4e6e92ffd Send account data to artifact rest interface to create a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3505
diff changeset
70 account.setAttribute("name", user.getAccount());
2984
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
71
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
72 //TODO create roles
3506
f2a4e6e92ffd Send account data to artifact rest interface to create a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3505
diff changeset
73 artuser.appendChild(account);
2984
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
74 action.appendChild(type);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
75 action.appendChild(artuser);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
76 document.appendChild(action);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
77
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
78 logger.debug("Create user request xml: " + XMLUtils.toString(document));
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
79
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
80 Document resp = client.createUser(document);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
82 logger.debug("Create user response xml: " + XMLUtils.toString(resp));
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
83
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
84 String XPATH_RESPONSE = "/art:result";
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
85 Node nresult = (Node) XMLUtils.xpath(
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
86 resp,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
87 XPATH_RESPONSE,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
88 XPathConstants.NODE,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
89 ArtifactNamespaceContext.INSTANCE);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
90 String result = nresult.getTextContent();
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
91 return (result != null && result.equalsIgnoreCase("success"));
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
92 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
93
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
94 public NodeList listUsers() throws ConnectionException {
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
95 HttpClient client = new HttpClientImpl(this.url);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
96
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
97 Document users = (Document) client.listUsers();
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
98
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
99 String XPATH_USERS = "/art:users/art:user";
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
100
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
101 return (NodeList) XMLUtils.xpath(
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
102 users,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
103 XPATH_USERS,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
104 XPathConstants.NODESET,
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
105 ArtifactNamespaceContext.INSTANCE);
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
106 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
107 }
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
108 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:
06d999e95615 Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
109

http://dive4elements.wald.intevation.org