Mercurial > hg > LGServices
comparison src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java @ 27:3b3e2963c8f7 dev_dataverse4
new: rewrite LGServices for LGDataverse with the new dataverse, dataverse4.0
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 08 Sep 2015 16:33:31 +0200 |
parents | ce2e3f2814c0 |
children | cbc2c83022c7 |
comparison
equal
deleted
inserted
replaced
26:ce2e3f2814c0 | 27:3b3e2963c8f7 |
---|---|
1 package de.mpiwg.gazetteer.dataverse; | 1 package de.mpiwg.gazetteer.dataverse; |
2 | 2 |
3 | 3 |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.security.KeyManagementException; | |
6 import java.security.NoSuchAlgorithmException; | |
5 import java.util.ArrayList; | 7 import java.util.ArrayList; |
6 import java.util.List; | 8 import java.util.List; |
7 | 9 |
8 import org.apache.commons.lang.StringUtils; | 10 import org.apache.commons.lang.StringUtils; |
9 import org.json.JSONArray; | 11 import org.json.JSONArray; |
10 import org.json.JSONException; | 12 import org.json.JSONException; |
11 import org.json.JSONObject; | 13 import org.json.JSONObject; |
12 | 14 |
13 import de.mpiwg.gazetteer.bo.LGBranch; | 15 import de.mpiwg.gazetteer.bo.LGBranch; |
14 import de.mpiwg.gazetteer.bo.LGFile; | 16 import de.mpiwg.gazetteer.bo.LGFile; |
17 import de.mpiwg.gazetteer.dataverse.bo.Datasets; | |
18 import de.mpiwg.gazetteer.dataverse.bo.Dataverse; | |
15 import de.mpiwg.gazetteer.dataverse.bo.Study; | 19 import de.mpiwg.gazetteer.dataverse.bo.Study; |
16 import de.mpiwg.gazetteer.dataverse.bo.VDCUser; | 20 import de.mpiwg.gazetteer.dataverse.bo.VDCUser; |
17 import de.mpiwg.gazetteer.utils.DataProvider; | 21 import de.mpiwg.gazetteer.utils.DataProvider; |
18 import de.mpiwg.gazetteer.utils.HTTPUtils; | 22 import de.mpiwg.gazetteer.utils.HTTPUtils; |
19 import de.mpiwg.gazetteer.utils.HTTPUtils.HttpStringResponse; | 23 import de.mpiwg.gazetteer.utils.HTTPUtils.HttpStringResponse; |
20 import de.mpiwg.gazetteer.utils.PropertiesUtils; | 24 import de.mpiwg.gazetteer.utils.PropertiesUtils; |
21 | 25 |
22 public class DataverseUtils { | 26 public class DataverseUtils { |
23 | 27 |
28 | |
29 | |
24 private static List<VDCUser> userList; | 30 private static List<VDCUser> userList; |
25 | 31 |
26 static{ | 32 static{ |
27 userList = new ArrayList<VDCUser>(); | 33 userList = new ArrayList<VDCUser>(); |
28 userList.add(new VDCUser(new Long(10), "jurzua", "1234")); | 34 userList.add(new VDCUser(new Long(10), "jurzua", "1234")); |
29 userList.add(new VDCUser(new Long(11), "john", "1234")); | 35 userList.add(new VDCUser(new Long(11), "john", "1234")); |
30 userList.add(new VDCUser(new Long(12), "zoe", "1234")); | 36 userList.add(new VDCUser(new Long(12), "zoe", "1234")); |
31 userList.add(new VDCUser(new Long(13), "paul", "1234")); | 37 userList.add(new VDCUser(new Long(13), "paul", "1234")); |
32 } | 38 |
33 | 39 } |
34 | 40 |
35 /** | 41 |
36 * | 42 |
37 * curl --form upload=@pom.xml --form user=networkAdmin --form password=networkAdmin --form studyId=hdl:TEST/10002 http://localhost/dvn/saveResource | 43 public static JSONObject publishFile(Long fileId, String datasetId, String userName, String password) throws Exception{ |
38 * see: http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically | |
39 * @param branchId | |
40 * @param studyGlobalId | |
41 * @throws Exception | |
42 */ | |
43 public static JSONObject publishFile(Long fileId, String studyGlobalId, String userName, String password) throws Exception{ | |
44 | 44 |
45 LGFile lgFile = DataProvider.getInstance().getFile(fileId); | 45 LGFile lgFile = DataProvider.getInstance().getFile(fileId); |
46 LGBranch branch = DataProvider.getInstance().getBranch(lgFile.getBranchId()); | 46 LGBranch branch = DataProvider.getInstance().getBranch(lgFile.getBranchId()); |
47 String link = PropertiesUtils.getPropValue("dvn_server") + "/saveResource"; | 47 String link = PropertiesUtils.getPropValue("dvn_server") + "/saveResource"; |
48 | 48 |
49 | 49 // get datasetGlobalId by datasetId |
50 String apiToken = PropertiesUtils.getPropValue("dvn_apitoken"); | |
51 String dvnServerPath = PropertiesUtils.getPropValue("dvn_server"); | |
52 | |
53 String query2 = dvnServerPath + "/api/datasets/"+ datasetId + "?key=" + apiToken; | |
54 HttpStringResponse response1 = null; | |
55 String datasetGlobalId = ""; | |
56 try { | |
57 response1 = HTTPUtils.getStringResponse(query2); | |
58 JSONObject jsonObject = new JSONObject(response1.content); | |
59 if(StringUtils.equals(jsonObject.getString("status"), "OK")){ | |
60 JSONObject data = jsonObject.getJSONObject("data"); | |
61 String dsProtocol = data.getString("protocol"); | |
62 String dsAuthority = data.getString("authority"); | |
63 String dsIdentifier = data.getString("identifier"); | |
64 // concatenate info to datasetGlobalId | |
65 datasetGlobalId = dsProtocol + ":"+ dsAuthority + "/" + dsIdentifier; | |
66 } | |
67 | |
68 } catch (Exception e) { | |
69 e.printStackTrace(); | |
70 } | |
71 | |
72 | |
50 MultipartUtility multipart = new MultipartUtility(link, "UTF-8"); | 73 MultipartUtility multipart = new MultipartUtility(link, "UTF-8"); |
51 | 74 multipart.addFormField("datasetGlobalId", datasetGlobalId); |
52 multipart.addFormField("studyId", studyGlobalId); | |
53 multipart.addFormField("password", password); | 75 multipart.addFormField("password", password); |
54 multipart.addFormField("user", userName); | 76 multipart.addFormField("user", userName); |
55 //multipart.addFormField("fileLabel", branch.getLabel() + ".html"); | 77 |
56 | 78 //String table = HTTPUtils.getTableOfFile(lgFile.getId()); |
57 | 79 String table = HTTPUtils.getTabDelimitedOfFile(lgFile.getId()); |
58 String table = HTTPUtils.getTableOfFile(lgFile.getId()); | 80 |
59 System.out.println("Table string lenght" + table.length()); | 81 System.out.println("Table string lenght" + table.length()); |
60 | 82 |
61 multipart.addFilePart0(branch.getLabel() + ".html", table); | 83 //multipart.addFilePart0(branch.getLabel() + ".html", table); |
84 | |
85 // put table and name into a jsonObject then pass it | |
86 JSONObject fileObj = new JSONObject(); | |
87 | |
88 // TODO publish to LGDataverse using tab-delimiter data | |
89 //fileObj.put(branch.getLabel() + ".html", table); | |
90 fileObj.put(branch.getLabel() + ".csv", table); | |
91 | |
92 String fileStr = fileObj.toString(); | |
93 multipart.addFormField("file", fileStr); | |
94 | |
95 // end | |
96 | |
62 | 97 |
63 String response = multipart.finish(); | 98 String response = multipart.finish(); |
64 | 99 |
65 System.out.println(response); | 100 System.out.println(response); |
66 | 101 |
67 return new JSONObject(response); | 102 return new JSONObject(response); |
68 } | 103 } |
69 | 104 |
70 public static List<Study> getStudies(String userName, String password) throws IOException{ | 105 public static JSONObject publishFile_old(Long fileId, String studyGlobalId, String userName, String password) throws Exception{ |
106 | |
107 LGFile lgFile = DataProvider.getInstance().getFile(fileId); | |
108 LGBranch branch = DataProvider.getInstance().getBranch(lgFile.getBranchId()); | |
109 String link = PropertiesUtils.getPropValue("dvn_server") + "/saveResource"; | |
110 | |
111 | |
112 MultipartUtility multipart = new MultipartUtility(link, "UTF-8"); | |
113 | |
114 multipart.addFormField("studyId", studyGlobalId); | |
115 multipart.addFormField("password", password); | |
116 multipart.addFormField("user", userName); | |
117 //multipart.addFormField("fileLabel", branch.getLabel() + ".html"); | |
118 | |
119 | |
120 String table = HTTPUtils.getTableOfFile(lgFile.getId()); | |
121 System.out.println("Table string lenght" + table.length()); | |
122 | |
123 multipart.addFilePart0(branch.getLabel() + ".html", table); | |
124 | |
125 String response = multipart.finish(); | |
126 | |
127 System.out.println(response); | |
128 | |
129 return new JSONObject(response); | |
130 } | |
131 | |
132 public static List<Datasets> getDatasets(String userName, String password) throws IOException{ | |
133 | |
134 | |
135 String apiToken = PropertiesUtils.getPropValue("dvn_apitoken"); | |
136 String dvnServerPath = PropertiesUtils.getPropValue("dvn_server"); | |
137 | |
138 // TODO list all dataverse: | |
139 // Implement an api in LGDataverse that can get the list of all dataverses in our system/instance | |
140 // String queryAllDataverse = dvnServerPath + "/getDataverseAliasList"; | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 // list all the DvObjects under dataverse id. GET http://$SERVER/api/dataverses/$id/contents | |
148 String dvId = PropertiesUtils.getPropValue("dv_id"); // get Local gazetteers dataverse id from config file | |
149 // TODO the dvId above probably need to revise. Each user may have h/her own dataverse. | |
150 // In this case, need to find out which dvIds belong to the user. | |
151 | |
152 // in the position of 'dvId', it can be used as the alias also as recommended by dataverse | |
153 String query1 = dvnServerPath + "/api/dataverses/"+ dvId + "/contents?key=" + apiToken; | |
154 | |
155 List<Datasets> datasetList = new ArrayList<Datasets>(); | |
156 | |
157 HttpStringResponse response = null; | |
158 try { | |
159 response = HTTPUtils.getStringResponse(query1); | |
160 JSONObject json = new JSONObject(response.content); | |
161 | |
162 System.out.println("json: " + json); | |
163 | |
164 if(StringUtils.equals(json.getString("status"), "OK")){ | |
165 // go through all datasets in this dataverse | |
166 JSONArray jsonData = json.getJSONArray("data"); | |
167 for (int i = 0; i < jsonData.length(); i ++) { | |
168 JSONObject aDataset = jsonData.getJSONObject(i); | |
169 String datasetId = aDataset.getString("id"); | |
170 | |
171 System.out.println("checking datasetId= " + datasetId); | |
172 | |
173 // check if the user has roles to access to the dataset. | |
174 // needed to be contributor or above, or in defined groups | |
175 if (canPublish(userName, password, dvId, datasetId)) { | |
176 System.out.println("User @" + userName + " can upload files to datasetId= " + datasetId); | |
177 | |
178 JSONObject datasetJson = getDatasetInfo(datasetId); | |
179 | |
180 Datasets datasets = new Datasets(datasetJson); | |
181 datasetList.add(datasets); | |
182 | |
183 } | |
184 | |
185 } | |
186 } | |
187 | |
188 } catch (Exception e) { | |
189 if(response != null){ | |
190 System.err.println(response.content); | |
191 } | |
192 e.printStackTrace(); | |
193 } | |
194 return datasetList; | |
195 } | |
196 | |
197 | |
198 private static JSONObject getDatasetInfo(String datasetId) { | |
199 JSONObject datasetJson = new JSONObject(); // datasetJson is the JSONObejct we collecting info about a dataset | |
200 // show the dataset whose id is given: GET http://$SERVER/api/datasets/$id?key=$apiKey | |
201 | |
202 try { | |
203 | |
204 String dvnServerPath = PropertiesUtils.getPropValue("dvn_server"); | |
205 String apiToken = PropertiesUtils.getPropValue("dvn_apitoken"); | |
206 | |
207 String query2 = dvnServerPath+ "/api/datasets/"+ datasetId + "?key=" + apiToken; | |
208 HttpStringResponse response1 = null; | |
209 | |
210 | |
211 response1 = HTTPUtils.getStringResponse(query2); | |
212 JSONObject jsonObject = new JSONObject(response1.content); | |
213 if(StringUtils.equals(jsonObject.getString("status"), "OK")){ | |
214 // collect information for the dataset | |
215 datasetJson.put("status", "OK"); | |
216 | |
217 JSONObject data = jsonObject.getJSONObject("data"); | |
218 datasetJson.put("id", datasetId); | |
219 | |
220 datasetJson.put("authority", data.getString("authority")); | |
221 datasetJson.put("persistentURL", data.getString("persistentUrl")); | |
222 datasetJson.put("protocol", data.getString("protocol")); | |
223 datasetJson.put("identifier", data.getString("identifier")); | |
224 | |
225 JSONObject latestVersion = data.getJSONObject("latestVersion"); | |
226 datasetJson.put("createTime", latestVersion.getString("createTime")); | |
227 | |
228 String versionState = latestVersion.getString("versionState"); | |
229 datasetJson.put("versionState",versionState); | |
230 | |
231 if (StringUtils.equals(versionState, "RELEASED")) { | |
232 datasetJson.put("version", latestVersion.getString("versionNumber")+"."+latestVersion.getString("versionMinorNumber")); | |
233 } else { | |
234 datasetJson.put("version",""); | |
235 } | |
236 | |
237 JSONArray citationFields = latestVersion.getJSONObject("metadataBlocks").getJSONObject("citation") | |
238 .getJSONArray("fields"); | |
239 for (int j = 0; j < citationFields.length(); j++) { | |
240 JSONObject a = citationFields.getJSONObject(j); | |
241 if( StringUtils.equals(a.getString("typeName"),"title")) { | |
242 String title = a.getString("value"); | |
243 //System.out.println("title: " + title); | |
244 datasetJson.put("title", title); | |
245 } | |
246 } | |
247 | |
248 } else { | |
249 datasetJson.put("status", "ERROR"); | |
250 | |
251 } | |
252 | |
253 | |
254 } catch (Exception e) { | |
255 e.printStackTrace(); | |
256 } | |
257 | |
258 | |
259 return datasetJson; | |
260 | |
261 | |
262 } | |
263 | |
264 private static boolean canPublish(String userName, String password, String dvId, String datasetId) { | |
265 | |
266 String queryRoleAssignments; | |
267 try { | |
268 queryRoleAssignments = PropertiesUtils.getPropValue("dvn_server") + "/getRoleAssignments?user=" + userName + "&password=" + password | |
269 + "&datasetId=" + datasetId; | |
270 | |
271 HttpStringResponse responseRoleAssignments = null; | |
272 | |
273 responseRoleAssignments = HTTPUtils.getStringResponse(queryRoleAssignments); | |
274 JSONObject raJson = new JSONObject(responseRoleAssignments.content); | |
275 | |
276 if(StringUtils.equals(raJson.getString("state"), "ok") && raJson.has("roleAssignments")){ | |
277 JSONArray roleAssignments = raJson.getJSONArray("roleAssignments"); | |
278 //System.out.println("has role assignments." + roleAssignments); | |
279 | |
280 for( int j = 0, size = roleAssignments.length(); j < size; j++ ) { | |
281 | |
282 JSONObject aRoleAssignment = roleAssignments.getJSONObject(j); | |
283 String aIdentifier = aRoleAssignment.getString("identifier"); | |
284 String aRoleName = aRoleAssignment.getString("roleName"); | |
285 | |
286 //System.out.println("identifier: " + aIdentifier+ ", roleName: " + aRoleName); | |
287 | |
288 boolean checkRoles = false; | |
289 // check user has the right to upload file: | |
290 // 1. check user identifier | |
291 if (StringUtils.equals(aIdentifier, "@"+userName) ) { | |
292 checkRoles = true; | |
293 | |
294 // 2. check user in the group which has the right to upload file | |
295 } else if (StringUtils.startsWith(aIdentifier, "&")) { | |
296 | |
297 String delims = "[&/-]+"; | |
298 String[] tokens = aIdentifier.split(delims); | |
299 String group = tokens[tokens.length-1]; // parse group name from it. It is the last one. | |
300 | |
301 List<String> userGroups = new ArrayList<String>(); | |
302 userGroups.add(group); | |
303 // userGroups is designed to be List. But as here it is used as only one element, this could be modified in the future. | |
304 if (isUserInGroups(userName, userGroups) ) { | |
305 checkRoles = true; | |
306 } | |
307 | |
308 } | |
309 | |
310 if (checkRoles) { | |
311 if (StringUtils.equals(aRoleName, "Contributor") || StringUtils.equals(aRoleName, "Curator") || StringUtils.equals(aRoleName, "Admin")) { | |
312 return true; | |
313 } | |
314 } | |
315 | |
316 } | |
317 | |
318 } | |
319 | |
320 | |
321 } catch (IOException e) { | |
322 e.printStackTrace(); | |
323 } catch (KeyManagementException e) { | |
324 e.printStackTrace(); | |
325 } catch (NoSuchAlgorithmException e) { | |
326 e.printStackTrace(); | |
327 } catch (JSONException e) { | |
328 e.printStackTrace(); | |
329 } | |
330 return false; | |
331 } | |
332 | |
333 // getStudy() has been replaced by getDatasets() when we migrate to dataverse 4.0 | |
334 public static List<Study> getStudy(String userName, String password) throws IOException{ | |
335 | |
336 String query = PropertiesUtils.getPropValue("dvn_server") + "/getStudies?user=" + userName + "&password=" + password; | |
71 List<Study> list = new ArrayList<Study>(); | 337 List<Study> list = new ArrayList<Study>(); |
72 | 338 |
73 String query = PropertiesUtils.getPropValue("dvn_server") + "/getStudies?user=" + userName + "&password=" + password; | 339 HttpStringResponse response1 = null; |
74 | 340 try { |
75 HttpStringResponse response = null; | 341 response1 = HTTPUtils.getStringResponse(query); |
76 try { | 342 JSONObject json = new JSONObject(response1.content); |
77 response = HTTPUtils.getStringResponse(query); | |
78 JSONObject json = new JSONObject(response.content); | |
79 if(json.has("studies")){ | 343 if(json.has("studies")){ |
80 | 344 |
81 JSONArray userArray = json.getJSONArray("studies"); | 345 JSONArray userArray = json.getJSONArray("studies"); |
82 | 346 |
83 for(int i=0; i<userArray.length(); i++){ | 347 for(int i=0; i<userArray.length(); i++){ |
85 Study study = new Study(studyJson); | 349 Study study = new Study(studyJson); |
86 list.add(study); | 350 list.add(study); |
87 } | 351 } |
88 } | 352 } |
89 } catch (Exception e) { | 353 } catch (Exception e) { |
90 if(response != null){ | 354 if(response1 != null){ |
91 System.err.println(response.content); | 355 System.err.println(response1.content); |
92 } | 356 } |
93 e.printStackTrace(); | 357 e.printStackTrace(); |
94 } | 358 } |
95 | 359 |
96 return list; | 360 return list; |
121 } | 385 } |
122 return null; | 386 return null; |
123 } | 387 } |
124 | 388 |
125 public static String getUsername(Long id){ | 389 public static String getUsername(Long id){ |
390 | |
126 try { | 391 try { |
127 for(VDCUser user : getAllUsers()){ | 392 for(VDCUser user : getAllUsers()){ |
128 if(user.getId().equals(id)){ | 393 if(user.getId().equals(id)){ |
129 return user.getUserName(); | 394 return user.getUserName(); |
130 } | 395 } |
167 System.err.println(response.content); | 432 System.err.println(response.content); |
168 } | 433 } |
169 e.printStackTrace(); | 434 e.printStackTrace(); |
170 } | 435 } |
171 | 436 |
437 | |
172 return list; | 438 return list; |
173 } | 439 } |
174 | 440 |
175 public static VDCUser login(String userName, String password) throws Exception{ | 441 public static VDCUser login(String userName, String password) throws Exception{ |
176 | 442 |
200 } | 466 } |
201 | 467 |
202 return null; | 468 return null; |
203 } | 469 } |
204 | 470 |
205 public static boolean isUserInGroups(VDCUser user, List<String> userGroups) { | 471 public static boolean isUserInGroups(String userName, List<String> userGroups) throws IOException { |
206 // find user who named userName belongs in what groups. check if the user is in any of the groups in userGroup list | 472 /* Get group members for each group in userGroup list, then check if user in any of them |
207 try { | 473 * get group members in a group by dataverse Api: GET http://$server/api/dataverses/$id/groups?key=$apiToken |
208 JSONArray groupArray = user.getUserGroups(); | 474 */ |
209 | 475 |
210 for(int i=0; i<groupArray.length(); i++){ | 476 String apiToken = PropertiesUtils.getPropValue("dvn_apitoken"); |
211 JSONObject groupString = groupArray.getJSONObject(i); | 477 |
212 | 478 for (String aGroup: userGroups) { |
213 String friendlyName = (String) groupString.get("friendlyName"); | 479 String query = PropertiesUtils.getPropValue("dvn_server") + "/api/dataverses/2/groups/"+ aGroup +"?key=" + apiToken; |
214 | 480 //System.out.println("URL: " + query); |
215 for(String u : userGroups) { | 481 |
216 if(StringUtils.equals(u, friendlyName)) { | 482 HttpStringResponse response = null; |
217 return true; | 483 try { |
484 response = HTTPUtils.getStringResponse(query); | |
485 JSONObject json = new JSONObject(response.content); | |
486 | |
487 if(StringUtils.equals(json.getString("status"), "OK")){ | |
488 JSONObject jsonData = json.getJSONObject("data"); | |
489 JSONArray jsonContainedRoleAssignees = jsonData.getJSONArray("containedRoleAssignees"); | |
490 for (int i = 0; i < jsonContainedRoleAssignees.length(); i ++) { | |
491 String aName = jsonContainedRoleAssignees.getString(i); | |
492 if (aName.compareTo("@" + userName) == 0) { | |
493 System.out.println("user "+ aName +" in group: " + aGroup); | |
494 return true; | |
495 } | |
218 } | 496 } |
219 } | 497 |
220 | 498 } |
221 } | 499 } catch (Exception e) { |
222 | 500 if(response != null){ |
223 } catch (JSONException e) { | 501 System.err.println(response.content); |
224 | 502 } |
225 e.printStackTrace(); | 503 e.printStackTrace(); |
504 } | |
505 | |
226 } | 506 } |
227 | 507 |
228 return false; | 508 return false; |
229 } | 509 } |
230 | 510 |
231 | 511 |
232 | 512 |
233 } | 513 } |