From 57e0f74e30ee5dbde755b94440cc2d5bfe174e97 Mon Sep 17 00:00:00 2001 From: Ian Allen Date: Fri, 29 Dec 2023 08:21:22 -0400 Subject: [PATCH 1/2] API should accept a structured object instead of JSONObject Updated link search to accept an object LinkFilter instead of JSONObject --- .../org/fao/geonet/api/links/LinksApi.java | 59 ++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/services/src/main/java/org/fao/geonet/api/links/LinksApi.java b/services/src/main/java/org/fao/geonet/api/links/LinksApi.java index aac2aa1342d..a3141517fe6 100644 --- a/services/src/main/java/org/fao/geonet/api/links/LinksApi.java +++ b/services/src/main/java/org/fao/geonet/api/links/LinksApi.java @@ -52,7 +52,6 @@ import org.fao.geonet.repository.specification.LinkSpecs; import org.jdom.JDOMException; import org.json.JSONException; -import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.data.domain.Page; @@ -146,9 +145,9 @@ public void iniMBeansSlidingWindowWithEmptySlot() { @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("isAuthenticated()") public Page getRecordLinks( - @Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421', groupId: 12}\", lastState being 'ok'/'ko'/'unknown'", required = false) + @Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421'}\", lastState being 'ok'/'ko'/'unknown'", required = false) @RequestParam(required = false) - JSONObject filter, + LinkFilter filter, @Parameter(description = "Optional, filter links to records published in that group.", required = false) @RequestParam(required = false) Integer[] groupIdFilter, @@ -187,9 +186,9 @@ public Page getRecordLinks( produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("isAuthenticated()") public Page getRecordLinksPost( - @Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421', groupId: 12}\", lastState being 'ok'/'ko'/'unknown'", required = false) + @Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421'}\", lastState being 'ok'/'ko'/'unknown'", required = false) @RequestParam(required = false) - JSONObject filter, + LinkFilter filter, @Parameter(description = "Optional, filter links to records published in that group.", required = false) @RequestParam(required = false) Integer[] groupIdFilter, @@ -207,7 +206,7 @@ public Page getRecordLinksPost( return getLinks(filter, groupIdFilter, groupOwnerIdFilter, pageRequest, userSession); } private Page getLinks( - JSONObject filter, + LinkFilter filter, Integer[] groupIdFilter, Integer[] groupOwnerIdFilter, Pageable pageRequest, @@ -228,22 +227,22 @@ private Page getLinks( Integer stateToMatch = null; String url = null; List associatedRecords = null; - if (filter.has("lastState")) { + if (filter.getLastState() != null) { stateToMatch = 0; - if (filter.getString("lastState").equalsIgnoreCase("ok")) { + if (filter.getLastState().equalsIgnoreCase("ok")) { stateToMatch = 1; - } else if (filter.getString("lastState").equalsIgnoreCase("ko")) { + } else if (filter.getLastState().equalsIgnoreCase("ko")) { stateToMatch = -1; } } - if (filter.has("url")) { - url = filter.getString("url"); + if (filter.getUrl() != null) { + url = filter.getUrl(); } - if (filter.has("records")) { + if (filter.getRecords() != null) { associatedRecords = Arrays.stream( - filter.getString("records").split(" ") + filter.getRecords().split(" ") ).collect(Collectors.toList()); } @@ -277,9 +276,9 @@ private Page getLinks( @PreAuthorize("isAuthenticated()") @ResponseBody public void getRecordLinksAsCsv( - @Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421', groupId: 12}\", lastState being 'ok'/'ko'/'unknown'", required = false) + @Parameter(description = "Filter, e.g. \"{url: 'png', lastState: 'ko', records: 'e421'}\", lastState being 'ok'/'ko'/'unknown'", required = false) @RequestParam(required = false) - JSONObject filter, + LinkFilter filter, @Parameter(description = "Optional, filter links to records published in that group.", required = false) @RequestParam(required = false) Integer[] groupIdFilter, @@ -445,4 +444,34 @@ private MAnalyseProcess getRegistredMAnalyseProcess() { mAnalyseProcesses.addFirst(mAnalyseProcess); return mAnalyseProcess; } + + private static class LinkFilter { + private String url; + private String lastState; + private String records; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getLastState() { + return lastState; + } + + public void setLastState(String lastState) { + this.lastState = lastState; + } + + public String getRecords() { + return records; + } + + public void setRecords(String records) { + this.records = records; + } + } } From eb042f0f3140bf01a616597d6d0abe79b77ec845 Mon Sep 17 00:00:00 2001 From: Ian Allen Date: Wed, 3 Jan 2024 10:41:27 -0400 Subject: [PATCH 2/2] Fix error with Failed to convert value of type by adding a @InitBinder --- .../org/fao/geonet/api/links/LinksApi.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/services/src/main/java/org/fao/geonet/api/links/LinksApi.java b/services/src/main/java/org/fao/geonet/api/links/LinksApi.java index a3141517fe6..46f17469977 100644 --- a/services/src/main/java/org/fao/geonet/api/links/LinksApi.java +++ b/services/src/main/java/org/fao/geonet/api/links/LinksApi.java @@ -25,6 +25,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.gson.Gson; + import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -63,6 +65,7 @@ import org.springframework.jmx.export.naming.SelfNaming; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Service; +import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @@ -71,6 +74,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; + +import java.beans.PropertyEditorSupport; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayDeque; @@ -445,6 +450,22 @@ private MAnalyseProcess getRegistredMAnalyseProcess() { return mAnalyseProcess; } + @InitBinder + public void initBinder(WebDataBinder dataBinder) { + dataBinder.registerCustomEditor(LinkFilter.class, new PropertyEditorSupport() { + Object value; + @Override + public Object getValue() { + return value; + } + + @Override + public void setAsText(String text) throws IllegalArgumentException { + value = new Gson().fromJson(text, LinkFilter.class); + } + }); + } + private static class LinkFilter { private String url; private String lastState;