comparison flys-client/src/main/java/de/intevation/flys/client/server/auth/UserClient.java @ 3507:59c77a9551c8

User new find-user REST service in UserClient Use new find-user REST interface to check if a user already exists in the database or a new user must be created. flys-client/trunk@5265 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 14:14:30 +0000
parents f2a4e6e92ffd
children 2c306a3dacef
comparison
equal deleted inserted replaced
3506:f2a4e6e92ffd 3507:59c77a9551c8
29 public UserClient(String url) { 29 public UserClient(String url) {
30 this.url = url; 30 this.url = url;
31 } 31 }
32 32
33 public boolean userExists(User user) throws ConnectionException { 33 public boolean userExists(User user) throws ConnectionException {
34 NodeList users = this.listUsers(); 34 Element data = this.findUser(user);
35 35
36 if (users == null || users.getLength() == 0) { 36 if (user == null) {
37 return false; 37 return false;
38 } 38 }
39 for(int i=0; i < users.getLength(); i++) { 39 String XPATH_USERACCOUNT = "/art:user/art:account/@name";
40 Element usernode = (Element)users.item(i);
41 String name = usernode.getAttributeNS(
42 ArtifactNamespaceContext.NAMESPACE_URI, "name");
43 40
44 if (name.equals(user.getName())) { 41 String account = XMLUtils.xpathString(
45 return true; 42 data, XPATH_USERACCOUNT, ArtifactNamespaceContext.INSTANCE);
46 } 43
44 if (account == null) {
45 return false;
47 } 46 }
48 return false; 47
48 return account.equals(user.getAccount());
49 } 49 }
50 50
51 public boolean createUser(User user) throws ConnectionException { 51 public boolean createUser(User user) throws ConnectionException {
52 logger.debug("Creating new user " + user.getName()); 52 logger.debug("Creating new user " + user.getName());
53 HttpClient client = new HttpClientImpl(this.url); 53 HttpClient client = new HttpClientImpl(this.url);
102 users, 102 users,
103 XPATH_USERS, 103 XPATH_USERS,
104 XPathConstants.NODESET, 104 XPathConstants.NODESET,
105 ArtifactNamespaceContext.INSTANCE); 105 ArtifactNamespaceContext.INSTANCE);
106 } 106 }
107
108 public Element findUser(User user) throws ConnectionException {
109 HttpClient client = new HttpClientImpl(this.url);
110
111 Document document = XMLUtils.newDocument();
112
113 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
114 document,
115 ArtifactNamespaceContext.NAMESPACE_URI,
116 ArtifactNamespaceContext.NAMESPACE_PREFIX
117 );
118
119 Element action = creator.create("action");
120
121 Element type = creator.create("type");
122 type.setAttribute("name", "find");
123 Element account = creator.create("account");
124 account.setAttribute("name", user.getAccount());
125
126 action.appendChild(type);
127 action.appendChild(account);
128 document.appendChild(action);
129
130 logger.debug("Find user request xml: " + XMLUtils.toString(document));
131
132 Document resp = client.findUser(document);
133
134 logger.debug("Find user request response xml: " + XMLUtils.toString(resp));
135
136 return resp.getDocumentElement();
137 }
107 } 138 }
108 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: 139 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:
109 140

http://dive4elements.wald.intevation.org