-
Notifications
You must be signed in to change notification settings - Fork 2
/
GNManager.R
649 lines (598 loc) · 22.4 KB
/
GNManager.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#' GeoNetwork REST API Manager
#'
#' @docType class
#' @importFrom R6 R6Class
#' @importFrom openssl base64_encode
#' @import httr
#' @import XML
#' @importFrom openssl base64_encode
#' @export
#' @keywords geonetwork rest api
#' @return Object of \code{\link{R6Class}} with methods for communication with
#' the REST API of a GeoNetwork instance.
#' @format \code{\link{R6Class}} object.
#'
#' @examples
#' \dontrun{
#' GMManager$new("http://localhost:8080/geonetwork", "admin", "geonetwork")
#' }
#'
#' @field loggerType the type of logger
#' @field verbose.info if geosapi logs have to be printed
#' @field verbose.debug if curl logs have to be printed
#' @field url the Base url of GeoNetwork
#' @field version the version of GeoNetwork. Handled as \code{GNVersion} object
#' @field user the user name
#'
#' @section Methods:
#' \describe{
#' \item{\code{new(url, user, pwd, version, logger)}}{
#' This method is used to instantiate a GNManager with the \code{url} of the
#' GeoNetwork and credentials to authenticate (\code{user}/\code{pwd}). By default,
#' the \code{logger} argument will be set to \code{NULL} (no logger). This argument
#' accepts two possible values: \code{INFO}: to print only geonapi logs,
#' \code{DEBUG}: to print geonapi and CURL logs
#' }
#' \item{\code{logger(type, text)}}{
#' Basic logger to report geonapi logs. Used internally
#' }
#' \item{\code{INFO(text)}}{
#' Logger to report information. Used internally
#' }
#' \item{\code{WARN(text)}}{
#' Logger to report warnings. Used internally
#' }
#' \item{\code{ERROR(text)}}{
#' Logger to report errors. Used internally
#' }
#' \item{\code{getUrl()}}{
#' Get the authentication URL
#' }
#' \item{\code{getLang()}}{
#' Get the service lang
#' }
#' \item{\code{login(user, pwd)}}{
#' This methods attempts a connection to GeoNetwork REST API. User internally
#' during initialization of \code{GNManager}.
#' }
#' \item{\code{getClassName()}}{
#' Retrieves the name of the class instance
#' }
#' \item{\code{insertMetadata(xml, file, geometa, group, category, stylesheet, validate, geometa_validate, geometa_inspire)}}{
#' Inserts a metadata by file, XML object or \pkg{geometa} object of class
#' \code{ISOMetadata} or \code{ISOFeatureCatalogue}. If successful, returns the Geonetwork
#' metadata internal identifier (integer). Extra parameters \code{geometa_validate} (TRUE
#' by default) and \code{geometa_inspire} (FALSE by default) can be used with geometa objects
#' for perform ISO and INSPIRE validation respectively.
#' }
#' \item{\code{setPrivConfiguration(id, config)}}{
#' Set the privilege configuration for a metadata. 'id' is the metadata integer id.
#' 'config' is an object of class "GNPrivConfiguration".
#' }
#' \item{\code{get(id, by, output)}}{
#' Generic getter for metadata. Possible values for by are 'id', 'uuid'. Used
#' internally only. The 'output' argument gives the type of output to return,
#' with possible values "id", "metadata", "info".
#' }
#' \item{\code{getMetadataByID(id)}}{
#' Get a metadata by Id. Returns an object of class \code{ISOMetadata} (ISO 19115)
#' or \code{ISOFeatureCatalogue} (ISO 19110) (from \pkg{geometa} package)
#' }
#' \item{\code{getMetadataByUUID(uuid)}}{
#' Get a metadata by UUID. Returns an object of class \code{ISOMetadata} (ISO 19115)
#' or \code{ISOFeatureCatalogue} (ISO 19110) (from \pkg{geometa} package)
#' }
#' \item{\code{getInfoByID(id)}}{
#' Get a metadata Info by Id. Returns an XML document object
#' }
#' \item{\code{getInfoByUUID(uuid)}}{
#' Get a metadata Info by UUID. Returns an XML document object
#' }
#' \item{\code{updateMetadata(id, xml, file, geometa, geometa_validate, geometa_inspire)}}{
#' Updates a metadata by file, XML object or \pkg{geometa} object of class
#' 'ISOMetadata' or 'ISOFeatureCatalogue'. Extra parameters \code{geometa_validate} (TRUE
#' by default) and \code{geometa_inspire} (FALSE by default) can be used with geometa objects
#' for perform ISO and INSPIRE validation respectively.
#' }
#' \item{\code{deleteMetadata(id)}}{
#' Deletes a metadata
#' }
#' \item{\code{deleteMetadataAll()}}{
#' Deletes all metadata for which the authenticated user is owner
#' }
#' }
#'
#' @author Emmanuel Blondel <emmanuel.blondel1@@gmail.com>
GNManager <- R6Class("GNManager",
lock_objects = FALSE,
#TODO provider specific formatter to prevent these fields to be printable
private = list(
user = NULL,
pwd = NULL,
token = NULL,
cookies = NULL,
#getEditingMetadataVersion
#---------------------------------------------------------------------------
getEditingMetadataVersion = function(id){
self$INFO(sprintf("Fetching metadata version for id = %s", id))
version <- NULL
req <- GNUtils$GET(
url = self$getUrl(),
path = "/metadata.edit!",
token = private$token,
query = list(id = id),
verbose = self$verbose.debug
)
xml <- NULL
if(status_code(req) == 200){
self$INFO("Successfully fetched editing metadata!")
xml <- GNUtils$parseResponseXML(req)
versionXML <- getNodeSet(xml, "//geonet:info/version",
c(geonet = "http://www.fao.org/geonetwork"))
if(length(versionXML)>0){
version <- as.integer(xmlValue(versionXML[[1]]))
}else{
stop("No geonet:info XML element found")
}
}else{
self$ERROR(sprintf("Error while fetching metadata version - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(version)
}
),
public = list(
#logger
verbose.info = FALSE,
verbose.debug = FALSE,
loggerType = NULL,
logger = function(type, text){
if(self$verbose.info){
cat(sprintf("[geonapi][%s] %s \n", type, text))
}
},
INFO = function(text){self$logger("INFO", text)},
WARN = function(text){self$logger("WARN", text)},
ERROR = function(text){self$logger("ERROR", text)},
#manager
url = NA,
version = NULL,
basicAuth = FALSE,
initialize = function(url, user = NULL, pwd = NULL, version, logger = NULL){
#logger
if(!missing(logger)){
if(!is.null(logger)){
self$loggerType <- toupper(logger)
if(!(self$loggerType %in% c("INFO","DEBUG"))){
stop(sprintf("Unknown logger type '%s", logger))
}
if(self$loggerType == "INFO"){
self$verbose.info = TRUE
}else if(self$loggerType == "DEBUG"){
self$verbose.info = TRUE
self$verbose.debug = TRUE
}
}
}
#GN version
if(!is(version, "character")) stop("The version should be an object of class 'character'")
self$version <- GNVersion$new(version = version)
#service lang
isGN26 <- self$version$value$major == 2 && self$version$value$minor == 6
self$lang <- ifelse(isGN26,"en","eng")
#basic auth
self$basicAuth <- self$version$value$major == 3
#baseUrl
self$url = sprintf("%s/srv/%s", url, self$lang)
#try to login
if(!is.null(user) && !is.null(pwd)){
self$INFO(sprintf("Connecting to GeoNetwork services as authenticated user '%s'", user))
self$login(user, pwd)
}else{
self$INFO("Connected to GeoNetwork services as anonymous user")
}
invisible(self)
},
#getUrl
#---------------------------------------------------------------------------
getUrl = function(){
return(self$url)
},
#getLang
#---------------------------------------------------------------------------
getLang = function(){
return(self$lang)
},
#login
#---------------------------------------------------------------------------
login = function(user, pwd){
req <- NULL
if(self$basicAuth){
#for newer versions >= 3
req <- GNUtils$POST(
url = self$getUrl(), path = "/info?type=me",
user = user, pwd = pwd, content = NULL, contentType = NULL,
verbose = TRUE
)
}else{
#for older versions < 3
gnRequest <- GNRESTRequest$new(username = user, password = pwd)
req <- GNUtils$POST(
url = self$getUrl(),
path = "/xml.user.login",
content = gnRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
}
private$user <- user
private$pwd <- pwd
req_cookies <- cookies(req)
cookies <- as.list(req_cookies$value)
names(cookies) <- req_cookies$name
if(length(cookies[names(cookies)=="XSRF-TOKEN"])>0){
private$token <- cookies[names(cookies)=="XSRF-TOKEN"][[1]]
}
cookies <- unlist(cookies[names(cookies)!="XSRF-TOKEN"])
private$cookies <- paste0(sapply(names(cookies), function(cookiename){paste0(cookiename,"=",cookies[[cookiename]])}),collapse=";")
if(!is.null(private$token)){
req <- GNUtils$POST(
url = self$getUrl(), path = "/info?type=me",
user = user, pwd = pwd, token = private$token, cookies = private$cookies, content = NULL, contentType = NULL,
verbose = TRUE
)
}
if(status_code(req) == 401){
err <- "Impossible to login to GeoNetwork: Wrong credentials"
self$ERROR(err)
stop(err)
}
if(status_code(req) == 404){
err <- "Impossible to login to GeoNetwork: Incorrect URL or GeoNetwork temporarily unavailable"
self$ERROR(err)
stop(err)
}
if(status_code(req) != 200){
err <- "Impossible to login to GeoNetwork: Unexpected error"
self$ERROR(err)
stop(err)
}
if(status_code(req) == 200){
self$INFO("Successfully authenticated to GeoNetwork!\n")
}
return(TRUE)
},
getCookies = function(){
return(private$cookies)
},
getToken = function(){
return(private$token)
},
getUserPwd = function(){
return(paste0(private$user,":", private$pwd))
},
#getClassName
#---------------------------------------------------------------------------
getClassName = function(){
return(class(self)[1])
},
#getGroups
#---------------------------------------------------------------------------
getGroups = function(){
out <- NULL
self$INFO("Getting user groups...")
req <- GNUtils$GET(
url = self$getUrl(),
path = "/xml.info",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
query = list(type = "groups"),
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully fetched user groups!")
xml <- GNUtils$parseResponseXML(req, "UTF-8")
xml.groups <- getNodeSet(xml, "//group/group")
out <- do.call("rbind", lapply(xml.groups, function(xml.group){
out.group <- data.frame(
id = xmlGetAttr(xml.group, "id"),
name = xmlValue(xmlChildren(xml.group)$name),
stringsAsFactors = FALSE
)
return(out.group)
}))
}else{
self$ERROR("Error while fetching user groups")
}
return(out)
},
#insertMetadata
#---------------------------------------------------------------------------
insertMetadata = function(xml = NULL, file = NULL, geometa = NULL, group,
category = NULL, stylesheet = NULL, validate = FALSE,
geometa_validate = TRUE, geometa_inspire = FALSE){
self$INFO("Inserting metadata ...")
out <- NULL
data <- NULL
isTempFile <- FALSE
if(!is.null(xml)){
tempf = tempfile(tmpdir = tempdir())
file <- paste(tempf,".xml",sep='')
isTempFile <- TRUE
saveXML(xml, file, encoding = "UTF-8")
}
if(!is.null(file)){
data <- geometa::readISO19139(file = file, raw = TRUE)
if(isTempFile) unlink(file)
}
if(!is.null(geometa)){
if(!is(geometa, "ISOMetadata") & !is(geometa, "ISOFeatureCatalogue")){
stop("Object 'geometa' should be of class 'ISOMetadata' or 'ISOFeatureCatalogue")
}
data <- geometa$encode(validate = geometa_validate, inspire = geometa_inspire)
}
if(is.null(data)){
stop("At least one of 'file', 'xml', or 'geometa' argument is required!")
}
if(is.null(category)) category <- "_none_"
if(is.null(stylesheet)) stylesheet <- "_none_"
gnRequest <- GNRESTRequest$new(
data = data,
group = group,
category = category,
stylesheet = stylesheet,
validate = ifelse(validate, "on", "off")
)
req <- GNUtils$POST(
url = self$getUrl(),
path = "/xml.metadata.insert",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
content = gnRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully inserted metadata!")
response <- GNUtils$parseResponseXML(req)
out <- as.integer(xpathApply(response, "//id", xmlValue)[[1]])
}else{
self$ERROR(sprintf("Error while inserting metadata - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
},
#setPrivConfiguration
#---------------------------------------------------------------------------
setPrivConfiguration = function(id, config){
self$INFO(sprintf("Setting privileges for metadata id = %s", id))
out <- FALSE
if(!is(config, "GNPrivConfiguration")){
stop("The 'config' value should be an object of class 'GNPrivConfiguration")
}
queryPrivParams = list()
for(grant in config$privileges){
for(priv in grant$privileges){
el <- paste0("_", grant$group, "_", priv)
queryPrivParams[[el]] <- "on"
}
}
queryParams <- list()
if(self$version$value$major == 3){
queryParams <- c(list("_content_type" = "xml"), queryPrivParams, list(id = id))
}else{
queryParams <- c(list(id = id), queryPrivParams)
}
req <- GNUtils$GET(
url = self$getUrl(),
path = ifelse(self$version$value$major < 3, "/metadata.admin", "md.privileges.update"),
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
query = queryParams,
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO(sprintf("Successfully set privileges for metadata id = %s!", id))
out <- TRUE
}else{
self$ERROR(sprintf("Error while setting privileges - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
},
#get
#---------------------------------------------------------------------------
get = function(id, by, output){
allowedByValues <- c("id","uuid")
if(!(by %in% allowedByValues)){
stop(sprintf("Unsupported 'by' parameter value '%s'. Possible values are [%s]",
by, paste0(allowedByValues, collapse=",")))
}
allowedOutputTypes <- c("id", "metadata", "info")
if(!(output %in% allowedOutputTypes)){
stop(sprintf("Unsupported 'output' type '%s'. Possible values are [%s]",
output, paste0(allowedOutputTypes, collapse=",")))
}
self$INFO(sprintf("Fetching metadata for %s = %s", by, id))
out <- NULL
if(by=="id"){
gnRequest <- GNRESTRequest$new(id = id)
}else if(by=="uuid"){
gnRequest <- GNRESTRequest$new(uuid = id)
}
req <- GNUtils$POST(
url = self$getUrl(),
path = "/xml.metadata.get",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
content = gnRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully fetched metadata!")
xml <- GNUtils$parseResponseXML(req, "UTF-8")
if(output == "id"){
idXML <- getNodeSet(xml, "//geonet:info/id", c(geonet = "http://www.fao.org/geonetwork"))
if(length(idXML)>0){
out <- as.integer(xmlValue(idXML[[1]]))
}else{
stop("No geonet:info XML element found")
}
}else if(output == "metadata"){
#bridge to geometa package once geometa XML decoding supported
isoClass <- xmlName(xmlRoot(xml))
out <- NULL
if(isoClass=="MD_Metadata"){
out <- geometa::ISOMetadata$new(xml = xml)
}else if(isoClass=="FC_FeatureCatalogue"){
out <- geometa::ISOFeatureCatalogue$new(xml = xml)
}
}else if(output == "info"){
#TODO support for GNInfo object
stop("GNInfo is not yet implemented in geonapi!")
}
}else{
self$ERROR(sprintf("Error while fetching metadata - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
},
#getMetadataByID
#---------------------------------------------------------------------------
getMetadataByID = function(id){
return(self$get(id, by = "id", output = "metadata"))
},
#getMetadataByUUID
#---------------------------------------------------------------------------
getMetadataByUUID = function(uuid){
return(self$get(uuid, by = "uuid", output = "metadata"))
},
#getInfoByID
#---------------------------------------------------------------------------
getInfoByID = function(id){
return(self$get(id, by = "id", output = "info"))
},
#getInfoByUUID
#---------------------------------------------------------------------------
getInfoByUUID = function(uuid){
return(self$get(uuid, by = "uuid", output = "info"))
},
#updateMetadata
#---------------------------------------------------------------------------
updateMetadata = function(id, xml = NULL, file = NULL, geometa = NULL,
geometa_validate = TRUE, geometa_inspire = FALSE){
self$INFO(sprintf("Updating metadata id = %s ...", id))
out <- NULL
data <- NULL
isTempFile <- FALSE
if(!is.null(xml)){
tempf = tempfile(tmpdir = tempdir())
file <- paste(tempf,".xml",sep='')
isTempFile <- TRUE
saveXML(xml, file, encoding = "UTF-8")
}
if(!is.null(file)){
data <- geometa::readISO19139(file = file, raw = TRUE)
if(isTempFile) unlink(file)
}
if(!is.null(geometa)){
if(!is(geometa, "ISOMetadata") & !is(geometa, "ISOFeatureCatalogue")){
stop("Object 'geometa' should be of class 'ISOMetadata' or 'ISOFeatureCatalogue")
}
data <- geometa$encode(validate = geometa_validate, inspire = geometa_inspire)
}
if(is.null(data)){
stop("At least one of 'file', 'xml', or 'geometa' argument is required!")
}
if(self$version$value$major >= 3){
gnRequest <- GNRESTRequest$new(
id = id,
data = data
)
}else{
gnRequest <- GNRESTRequest$new(
id = id,
version = private$getEditingMetadataVersion(id),
data = data
)
}
req <- GNUtils$POST(
url = self$getUrl(),
path = "/metadata.update.finish",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
content = gnRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully updated metadata!")
response <- GNUtils$parseResponseXML(req)
out <- as.integer(xpathApply(response, "//id", xmlValue)[[1]])
}else{
self$ERROR(sprintf("Error while updating metadata - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
},
#deleteMetadata
#---------------------------------------------------------------------------
deleteMetadata = function(id){
self$INFO(sprintf("Deleting metadata id = %s ...", id))
out <- NULL
gnRequest <- GNRESTRequest$new(id = id)
req <- GNUtils$POST(
url = self$getUrl(),
path = "/xml.metadata.delete",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
content = gnRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully deleted metadata!")
response <- GNUtils$parseResponseXML(req)
out <- as.integer(xpathApply(response, "//id", xmlValue)[[1]])
}else{
self$ERROR(sprintf("Error while deleting metadata - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
},
#deleteMetadataAll
#---------------------------------------------------------------------------
deleteMetadataAll = function(){
self$INFO("Deleting all owned metadata...")
if(self$version$lowerThan("2.10.x")){
stop("Method unsupported for GN < 2.10.X")
}
#select all
gnSelectRequest <- GNRESTRequest$new()
gnSelectRequest$setChild("selected", "add-all")
selectReq <- GNUtils$POST(
url = self$getUrl(),
path = "/xml.metadata.select",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
content = gnSelectRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
#delete all
gnDeleteRequest <- GNRESTRequest$new()
deleteReq <- GNUtils$POST(
url = self$getUrl(),
path = "/xml.metadata.batch.delete",
token = private$token, cookies = private$cookies,
user = private$user, pwd = private$pwd,
content = gnDeleteRequest$encode(),
contentType = "text/xml",
verbose = self$verbose.debug
)
response <- GNUtils$parseResponseXML(deleteReq)
return(response)
}
)
)