Mercurial > dive4elements > river
annotate 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 |
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++) { |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 Node usernode = users.item(i); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 String name = XMLUtils.xpathString( |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 usernode, "@art:name", ArtifactNamespaceContext.INSTANCE); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 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
|
44 return true; |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 } |
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 return false; |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 } |
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 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
|
51 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
|
52 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
|
53 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
54 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
|
55 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
56 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
|
57 document, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
58 ArtifactNamespaceContext.NAMESPACE_URI, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 ArtifactNamespaceContext.NAMESPACE_PREFIX |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
60 ); |
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 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
|
63 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 artuser.setAttribute("name", user.getName()); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
68 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
69 //TODO create roles |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
70 action.appendChild(type); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
71 action.appendChild(artuser); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
72 document.appendChild(action); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
73 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
74 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
|
75 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
76 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
|
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 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
|
79 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
80 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
|
81 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
|
82 resp, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
83 XPATH_RESPONSE, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
84 XPathConstants.NODE, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
85 ArtifactNamespaceContext.INSTANCE); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
86 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
|
87 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
|
88 } |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
89 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
90 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
|
91 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
|
92 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
93 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
|
94 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
95 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
|
96 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
97 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
|
98 users, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
99 XPATH_USERS, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
100 XPathConstants.NODESET, |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
101 ArtifactNamespaceContext.INSTANCE); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
102 } |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
103 } |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
104 // 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
|
105 |