Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/auth/UserClient.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/server/auth/UserClient.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.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 org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
13 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
14 | |
15 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; | |
16 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
17 import org.dive4elements.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 if (user == null) { | |
35 return false; | |
36 } | |
37 | |
38 Element data = this.findUser(user); | |
39 | |
40 String XPATH_USERACCOUNT = "/art:user/art:account/@art:name"; | |
41 | |
42 String account = XMLUtils.xpathString( | |
43 data, XPATH_USERACCOUNT, ArtifactNamespaceContext.INSTANCE); | |
44 | |
45 if (account == null) { | |
46 return false; | |
47 } | |
48 | |
49 return account.equals(user.getAccount()); | |
50 } | |
51 | |
52 public boolean createUser(User user) throws ConnectionException { | |
53 if(user == null) { | |
54 logger.warn("createUser: given user is null"); | |
55 return false; | |
56 } | |
57 | |
58 logger.debug("Creating new user " + user.getName()); | |
59 HttpClient client = new HttpClientImpl(this.url); | |
60 | |
61 Document document = XMLUtils.newDocument(); | |
62 | |
63 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
64 document, | |
65 ArtifactNamespaceContext.NAMESPACE_URI, | |
66 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
67 ); | |
68 | |
69 Element action = creator.create("action"); | |
70 | |
71 Element type = creator.create("type"); | |
72 type.setAttribute("name", "create"); | |
73 Element artuser = creator.create("user"); | |
74 artuser.setAttribute("name", user.getName()); | |
75 Element account = creator.create("account"); | |
76 account.setAttribute("name", user.getAccount()); | |
77 | |
78 //TODO create roles | |
79 artuser.appendChild(account); | |
80 action.appendChild(type); | |
81 action.appendChild(artuser); | |
82 document.appendChild(action); | |
83 | |
84 logger.debug("Create user request xml: " + XMLUtils.toString(document)); | |
85 | |
86 Document resp = client.createUser(document); | |
87 | |
88 logger.debug("Create user response xml: " + XMLUtils.toString(resp)); | |
89 | |
90 String XPATH_RESPONSE = "/art:result"; | |
91 Node nresult = (Node) XMLUtils.xpath( | |
92 resp, | |
93 XPATH_RESPONSE, | |
94 XPathConstants.NODE, | |
95 ArtifactNamespaceContext.INSTANCE); | |
96 String result = nresult.getTextContent(); | |
97 return (result != null && result.equalsIgnoreCase("success")); | |
98 } | |
99 | |
100 public NodeList listUsers() throws ConnectionException { | |
101 HttpClient client = new HttpClientImpl(this.url); | |
102 | |
103 Document users = (Document) client.listUsers(); | |
104 | |
105 String XPATH_USERS = "/art:users/art:user"; | |
106 | |
107 return (NodeList) XMLUtils.xpath( | |
108 users, | |
109 XPATH_USERS, | |
110 XPathConstants.NODESET, | |
111 ArtifactNamespaceContext.INSTANCE); | |
112 } | |
113 | |
114 public Element findUser(User user) throws ConnectionException { | |
115 if(user == null) { | |
116 throw new IllegalArgumentException("user is null"); | |
117 } | |
118 | |
119 HttpClient client = new HttpClientImpl(this.url); | |
120 | |
121 Document document = XMLUtils.newDocument(); | |
122 | |
123 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
124 document, | |
125 ArtifactNamespaceContext.NAMESPACE_URI, | |
126 ArtifactNamespaceContext.NAMESPACE_PREFIX | |
127 ); | |
128 | |
129 Element action = creator.create("action"); | |
130 | |
131 Element type = creator.create("type"); | |
132 type.setAttribute("name", "find"); | |
133 Element account = creator.create("account"); | |
134 account.setAttribute("name", user.getAccount()); | |
135 | |
136 action.appendChild(type); | |
137 action.appendChild(account); | |
138 document.appendChild(action); | |
139 | |
140 boolean debug = logger.isDebugEnabled(); | |
141 | |
142 if (debug) { | |
143 logger.debug("Find user request xml: " + | |
144 XMLUtils.toString(document)); | |
145 } | |
146 | |
147 Document resp = client.findUser(document); | |
148 | |
149 if (debug) { | |
150 logger.debug("Find user request response xml: " + | |
151 XMLUtils.toString(resp)); | |
152 } | |
153 | |
154 return resp.getDocumentElement(); | |
155 } | |
156 } | |
157 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: | |
158 |