From 2e1f9ceff7b8278a037bce4c93ac4614997ba120 Mon Sep 17 00:00:00 2001 From: steven choi Date: Mon, 20 Nov 2023 14:18:53 +1100 Subject: [PATCH 1/5] #1570 check if shape file is uploading correctly --- .../services/au/org/ala/biocollect/merit/WebService.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy b/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy index 4beb1453c..d0089d55c 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy @@ -466,7 +466,7 @@ class WebService { } addHubUrlPath(headers) - headers."Authorization" = grailsApplication.config.getProperty("api_key") + headers."Authorization" = getAuthHeader() if (user) { headers[grailsApplication.config.app.http.header.userId] = user.userId } From 471cfa744077e88c52d188061010d87caeef1e02 Mon Sep 17 00:00:00 2001 From: steven choi Date: Mon, 20 Nov 2023 17:35:46 +1100 Subject: [PATCH 2/5] #1570 check if shape file is uploading correctly --- .../biocollect/merit/MetadataService.groovy | 7 ++++--- .../ala/biocollect/merit/SiteService.groovy | 6 +++--- .../ala/biocollect/merit/WebService.groovy | 21 +++++++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy b/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy index f886732ef..b0ba6e5b2 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy @@ -10,7 +10,8 @@ import static org.apache.http.HttpStatus.* class MetadataService { static DateFormat ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") - def grailsApplication, webService, cacheService, settingService, modelService + def grailsApplication, cacheService, settingService, modelService + WebService webService def activitiesModel() { return cacheService.get('activity-model',{ @@ -247,14 +248,14 @@ class MetadataService { def facetConfig = webService.getJson(grailsApplication.config.ecodata.service.url + "/metadata/getGeographicFacetConfig") facetConfig.grouped.each { k, v -> v.each { name, fid -> - def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid) + def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, true, false, true) results[k] << [(objects[0].fieldname):objects[0]] // Using the fieldname instead of the name for grouped facets is a temp workaround for the GER. } } facetConfig.contextual.each { name, fid -> - def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid) + def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, true, false, true) objects.each { results[name] << [(it.name):it] } diff --git a/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy b/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy index 9b7e34b86..00d63f861 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy @@ -15,7 +15,7 @@ import org.opengis.feature.simple.SimpleFeature class SiteService { - def webService, grailsApplication, commonService, metadataService, userService + def grailsApplication, commonService, metadataService, userService def documentService ActivityService activityService ProjectService projectService @@ -23,7 +23,7 @@ class SiteService { ReportService reportService ProjectActivityService projectActivityService SiteService siteService - + WebService webService def list() { webService.getJson(grailsApplication.config.ecodata.service.url + '/site/').list } @@ -162,7 +162,7 @@ class SiteService { def userId = userService.getUser().userId def url = "${grailsApplication.config.spatial.layersUrl}/shape/upload/shp?user_id=${userId}&api_key=${grailsApplication.config.api_key}" - return webService.postMultipart(url, [:], shapefile) + return webService.postMultipart(url, [:], shapefile, 'files', true) } /** diff --git a/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy b/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy index d0089d55c..0e049282a 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy @@ -187,12 +187,16 @@ class WebService { tokenService.getAuthToken(false).toAuthorizationHeader() } - def getJson(String url, Integer timeout = null, boolean includeApiKey = false, boolean includeUserId = true) { + def getJson(String url, Integer timeout = null, boolean includeApiKey = false, boolean includeUserId = true, boolean useToken = false) { def conn = null try { conn = configureConnection(url, includeUserId, timeout) if (includeApiKey) { - conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key")) + if (useToken) { + conn.setRequestProperty("Authorization", getAuthHeader()) + } else { + conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key")) + } } conn.setRequestProperty(ACCEPT, MediaType.APPLICATION_JSON_VALUE) def json = responseText(conn) @@ -434,9 +438,9 @@ class WebService { * @param file the Multipart file object to forward. * @return [status:, content: */ - def postMultipart(url, Map params, MultipartFile file, fileParam = 'files') { + def postMultipart(url, Map params, MultipartFile file, fileParam = 'files', boolean useToken = false) { - postMultipart(url, params, file.inputStream, file.contentType, file.originalFilename, fileParam) + postMultipart(url, params, file.inputStream, file.contentType, file.originalFilename, fileParam, useToken) } /** @@ -449,7 +453,7 @@ class WebService { * @param fileParamName the name of the HTTP parameter that will be used for the post. * @return [status:, content: */ - def postMultipart(url, Map params, InputStream contentIn, contentType, originalFilename, fileParamName = 'files') { + def postMultipart(url, Map params, InputStream contentIn, contentType, originalFilename, fileParamName = 'files', boolean useToken = false) { def result = [:] def user = userService.getUser() @@ -466,7 +470,12 @@ class WebService { } addHubUrlPath(headers) - headers."Authorization" = getAuthHeader() + if (useToken) { + headers."Authorization" = getAuthHeader() + } else { + headers."Authorization" = grailsApplication.config.getProperty("api_key") + } + if (user) { headers[grailsApplication.config.app.http.header.userId] = user.userId } From 6f7ba11226b483db0dbcfb8bde21af17b3e0b6dd Mon Sep 17 00:00:00 2001 From: steven choi Date: Tue, 21 Nov 2023 13:00:09 +1100 Subject: [PATCH 3/5] #1570 add doPost case --- .../biocollect/merit/MetadataService.groovy | 4 ++-- .../ala/biocollect/merit/SiteService.groovy | 4 ++-- .../ala/biocollect/merit/WebService.groovy | 21 ++++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy b/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy index b0ba6e5b2..46fea063f 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy @@ -248,14 +248,14 @@ class MetadataService { def facetConfig = webService.getJson(grailsApplication.config.ecodata.service.url + "/metadata/getGeographicFacetConfig") facetConfig.grouped.each { k, v -> v.each { name, fid -> - def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, true, false, true) + def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, false, true) results[k] << [(objects[0].fieldname):objects[0]] // Using the fieldname instead of the name for grouped facets is a temp workaround for the GER. } } facetConfig.contextual.each { name, fid -> - def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, true, false, true) + def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, false, true) objects.each { results[name] << [(it.name):it] } diff --git a/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy b/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy index 00d63f861..addebe5bc 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy @@ -179,11 +179,11 @@ class SiteService { def baseUrl = "${grailsApplication.config.spatial.layersUrl}/shape/upload/shp" def userId = userService.getUser().userId - def site = [name:name, description: description, user_id:userId, api_key:grailsApplication.config.api_key] + def site = [name:name, description: description, user_id:userId] def url = "${baseUrl}/${shapeFileId}/${siteId}" - def result = webService.doPost(url, site) + def result = webService.doPost(url, site, true) String error if (!result?.resp?.id) { diff --git a/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy b/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy index 0e049282a..1a7aba804 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/WebService.groovy @@ -192,12 +192,13 @@ class WebService { try { conn = configureConnection(url, includeUserId, timeout) if (includeApiKey) { - if (useToken) { - conn.setRequestProperty("Authorization", getAuthHeader()) - } else { - conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key")) - } + conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key")) + } + + if (useToken) { + conn.setRequestProperty("Authorization", getAuthHeader()) } + conn.setRequestProperty(ACCEPT, MediaType.APPLICATION_JSON_VALUE) def json = responseText(conn) def result = JSON.parse(json) @@ -285,14 +286,20 @@ class WebService { } } - def doPost(String url, Map postBody) { + def doPost(String url, Map postBody, boolean useToken = false) { def conn = null def charEncoding = 'utf-8' try { conn = new URL(url).openConnection() conn.setDoOutput(true) conn.setRequestProperty("Content-Type", "application/json;charset=${charEncoding}") - conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key")) + + if (useToken) { + conn.setRequestProperty("Authorization", getAuthHeader()) + } else { + conn.setRequestProperty("Authorization", grailsApplication.config.getProperty("api_key")) + } + addHubUrlPath(conn) def user = getUserService().getUser() From 2351856ea544974e1a6de8437f30e88bce0e0448 Mon Sep 17 00:00:00 2001 From: steven choi Date: Tue, 21 Nov 2023 13:22:01 +1100 Subject: [PATCH 4/5] #1570 fix --- .../services/au/org/ala/biocollect/merit/SiteService.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy b/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy index addebe5bc..22d910af6 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/SiteService.groovy @@ -179,7 +179,7 @@ class SiteService { def baseUrl = "${grailsApplication.config.spatial.layersUrl}/shape/upload/shp" def userId = userService.getUser().userId - def site = [name:name, description: description, user_id:userId] + def site = [name:name, description: description, user_id:userId, api_key:grailsApplication.config.api_key] def url = "${baseUrl}/${shapeFileId}/${siteId}" @@ -245,7 +245,7 @@ class SiteService { Geometry geom = placemark.getDefaultGeometry() def site = [name:name, description: description, user_id:userId, api_key:grailsApplication.config.api_key, wkt:geom.toText()] - def result = webService.doPost(url, site) + def result = webService.doPost(url, site, true) if (!result.error) { def id = result.resp.id if (!result.resp.error) { From 8127766ad139263dc042c874dce0ee5a9eafa5be Mon Sep 17 00:00:00 2001 From: steven choi Date: Wed, 22 Nov 2023 16:04:13 +1100 Subject: [PATCH 5/5] #1570 fix --- .../au/org/ala/biocollect/merit/MetadataService.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy b/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy index 46fea063f..fd1658795 100644 --- a/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy +++ b/grails-app/services/au/org/ala/biocollect/merit/MetadataService.groovy @@ -248,14 +248,14 @@ class MetadataService { def facetConfig = webService.getJson(grailsApplication.config.ecodata.service.url + "/metadata/getGeographicFacetConfig") facetConfig.grouped.each { k, v -> v.each { name, fid -> - def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, false, true) + def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, true, true) results[k] << [(objects[0].fieldname):objects[0]] // Using the fieldname instead of the name for grouped facets is a temp workaround for the GER. } } facetConfig.contextual.each { name, fid -> - def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, false, true) + def objects = webService.getJson(grailsApplication.config.spatial.baseURL + '/ws/objects/'+fid, null, false, true, true) objects.each { results[name] << [(it.name):it] }