From f2f0dbb8409fbbd1485d9a58df9027e77ab45703 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Fri, 13 Sep 2024 11:35:14 +0200 Subject: [PATCH 01/23] added resolved functionf before refactoring --- .../org/dspace/handle/HandleServiceImpl.java | 90 +++++++++++++++++++ .../dspace/handle/service/HandleService.java | 6 ++ 2 files changed, 96 insertions(+) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java index 373b31d89750..54d8f24bbc00 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java @@ -19,15 +19,23 @@ import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import com.drew.metadata.Metadata; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.dspace.api.DSpaceApi; +import org.dspace.app.bulkedit.MetadataImportException; import org.dspace.content.Community; import org.dspace.content.DSpaceObject; import org.dspace.content.Item; +import org.dspace.content.MetadataField; +import org.dspace.content.MetadataValue; +import org.dspace.content.service.ItemService; +import org.dspace.content.service.MetadataFieldService; +import org.dspace.content.service.MetadataValueService; import org.dspace.content.service.SiteService; import org.dspace.content.service.clarin.ClarinItemService; import org.dspace.core.Constants; @@ -71,6 +79,10 @@ public class HandleServiceImpl implements HandleService { protected SiteService siteService; @Autowired protected ClarinItemService clarinItemService; + @Autowired + protected MetadataFieldService metadataFieldService; + @Autowired + protected MetadataValueService metadataValueService; private static final Pattern[] IDENTIFIER_PATTERNS = { Pattern.compile("^hdl:(.*)$"), @@ -102,6 +114,84 @@ public String resolveToURL(Context context, String handle) return url; } + @Override + public List resolveToTitle(Context context, String handle) throws SQLException { + Handle dbhandle = findHandleInternal(context, handle); + if (dbhandle == null) { + return null; + } + MetadataField metadataField = metadataFieldService.findByString(context, "dc.identifier.uri", '.'); + if (Objects.isNull(metadataField)) { + throw new RuntimeException ("Cannot get item by handle because the metadata field ID for " + + "`dc.identifier.uri` wasn't found."); + } + List itemList = clarinItemService.findByHandle(context, metadataField, handle); + // handle should have only ONE item + if (CollectionUtils.isEmpty(itemList)) { + return null; + } + Item item = itemList.get(0); + MetadataField mf = metadataFieldService.findByElement(context, "dc", "title", null); + Iterator mdv = metadataValueService.findByUUIDAndField(context, item.getID(), mf); + List metadataValues = new ArrayList<>(); + if (mdv.hasNext()) { + MetadataValue mdvVal = mdv.next(); + metadataValues.add(mdvVal.getValue()); + } + log.debug("Resolved {} to {}", handle, metadataValues.stream() + .map(Object::toString) + .collect(Collectors.joining(", "))); + return metadataValues; + } + + @Override + public String resolveToRepository(Context context, String handle) throws SQLException { + Handle dbhandle = findHandleInternal(context, handle); + if (dbhandle == null) { + return null; + } + String repository = configurationService.getProperty("dspace.name");; + log.debug("Resolved {} to {}", handle, repository); + return configurationService.getProperty("dspace.name"); + } + + @Override + public String resolveToSubmitdate(Context context, String handle) throws SQLException { + Handle dbhandle = findHandleInternal(context, handle); + if (dbhandle == null) { + return null; + } + MetadataField metadataField = metadataFieldService.findByString(context, "dc.identifier.uri", '.'); + if (Objects.isNull(metadataField)) { + throw new RuntimeException ("Cannot get item by handle because the metadata field ID for " + + "`dc.identifier.uri` wasn't found."); + } + List itemList = clarinItemService.findByHandle(context, metadataField, handle); + // handle should have only ONE item + if (CollectionUtils.isEmpty(itemList)) { + return null; + } + Item item = itemList.get(0); + MetadataField mf = metadataFieldService.findByString(context, "dc.date.accessioned", '.'); + Iterator mdv = metadataValueService.findByUUIDAndField(context, item.getID(), mf); + String metadataValue = null; + if (mdv.hasNext()) { + MetadataValue mdvVal = mdv.next(); + metadataValue = mdvVal.getValue(); + if (mdv.hasNext()) { + throw new SQLException("Ambiguous reference; multiple matches in db of" + + " dc.date.accessioned for handle: " + handle); + } + } + log.debug("Resolved {} to {}", handle, metadataValue); + return metadataValue; + } + + @Override + public String resolveToReportemail(Context context, String handle) throws SQLException { + return ""; + } + @Override public String resolveUrlToHandle(Context context, String url) throws SQLException { diff --git a/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java b/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java index 85950ab6db87..cd26ae090393 100644 --- a/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java +++ b/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java @@ -40,6 +40,12 @@ public interface HandleService { */ public String resolveToURL(Context context, String handle) throws SQLException; + public List resolveToTitle(Context context, String handle) throws SQLException; + public String resolveToRepository(Context context, String handle) throws SQLException; + + public String resolveToSubmitdate(Context context, String handle) throws SQLException; + + public String resolveToReportemail(Context context, String handle) throws SQLException; /** * Try to detect a handle in a URL. From 1afa437b3ba902b97339e2ed112d4ac0805316ef Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 16 Sep 2024 09:27:40 +0200 Subject: [PATCH 02/23] return map of mtd of handle --- .../java/org/dspace/handle/HandlePlugin.java | 24 +++++- .../org/dspace/handle/HandleServiceImpl.java | 84 ------------------- .../dspace/handle/service/HandleService.java | 6 -- .../HdlResolverRestController.java | 56 +++++++++++-- .../HdlResolverRestControllerIT.java | 33 ++++++++ 5 files changed, 105 insertions(+), 98 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index 1275ef81d695..4588b069da3e 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -268,6 +268,24 @@ public void scanNAs(ScanCallback callback) throws HandleException { @Override public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, byte[][] typeList) throws HandleException { + ResolvedHandle rh = getHandleValues(theHandle, indexList, typeList); + if (Objects.isNull(rh)) { + return null; + } + return rh.toRawValue(); + } + + public List getListHandleValues(byte[] theHandle, int[] indexList, + byte[][] typeList) throws HandleException { + ResolvedHandle rh = getHandleValues(theHandle, indexList, typeList); + if (Objects.isNull(rh)) { + return null; + } + return rh.getHandleValue(); + } + + private ResolvedHandle getHandleValues(byte[] theHandle, int[] indexList, + byte[][] typeList) throws HandleException { if (log.isInfoEnabled()) { log.info("Called getRawHandleValues"); } @@ -341,7 +359,7 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, rh.setDead(handle, deadSince); } - return rh.toRawValue(); + return rh; } catch (HandleException he) { throw he; } catch (Exception e) { @@ -734,6 +752,10 @@ public byte[][] toRawValue() throws HandleException { return rawValues; } + public List getHandleValue() { + return values; + } + public void setDead(String handle, String deadSince) { //find URL field for (HandleValue hv : values) { diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java index 54d8f24bbc00..7da5a0d3e53c 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java @@ -19,21 +19,15 @@ import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; -import com.drew.metadata.Metadata; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.dspace.api.DSpaceApi; -import org.dspace.app.bulkedit.MetadataImportException; import org.dspace.content.Community; import org.dspace.content.DSpaceObject; import org.dspace.content.Item; -import org.dspace.content.MetadataField; -import org.dspace.content.MetadataValue; -import org.dspace.content.service.ItemService; import org.dspace.content.service.MetadataFieldService; import org.dspace.content.service.MetadataValueService; import org.dspace.content.service.SiteService; @@ -114,84 +108,6 @@ public String resolveToURL(Context context, String handle) return url; } - @Override - public List resolveToTitle(Context context, String handle) throws SQLException { - Handle dbhandle = findHandleInternal(context, handle); - if (dbhandle == null) { - return null; - } - MetadataField metadataField = metadataFieldService.findByString(context, "dc.identifier.uri", '.'); - if (Objects.isNull(metadataField)) { - throw new RuntimeException ("Cannot get item by handle because the metadata field ID for " + - "`dc.identifier.uri` wasn't found."); - } - List itemList = clarinItemService.findByHandle(context, metadataField, handle); - // handle should have only ONE item - if (CollectionUtils.isEmpty(itemList)) { - return null; - } - Item item = itemList.get(0); - MetadataField mf = metadataFieldService.findByElement(context, "dc", "title", null); - Iterator mdv = metadataValueService.findByUUIDAndField(context, item.getID(), mf); - List metadataValues = new ArrayList<>(); - if (mdv.hasNext()) { - MetadataValue mdvVal = mdv.next(); - metadataValues.add(mdvVal.getValue()); - } - log.debug("Resolved {} to {}", handle, metadataValues.stream() - .map(Object::toString) - .collect(Collectors.joining(", "))); - return metadataValues; - } - - @Override - public String resolveToRepository(Context context, String handle) throws SQLException { - Handle dbhandle = findHandleInternal(context, handle); - if (dbhandle == null) { - return null; - } - String repository = configurationService.getProperty("dspace.name");; - log.debug("Resolved {} to {}", handle, repository); - return configurationService.getProperty("dspace.name"); - } - - @Override - public String resolveToSubmitdate(Context context, String handle) throws SQLException { - Handle dbhandle = findHandleInternal(context, handle); - if (dbhandle == null) { - return null; - } - MetadataField metadataField = metadataFieldService.findByString(context, "dc.identifier.uri", '.'); - if (Objects.isNull(metadataField)) { - throw new RuntimeException ("Cannot get item by handle because the metadata field ID for " + - "`dc.identifier.uri` wasn't found."); - } - List itemList = clarinItemService.findByHandle(context, metadataField, handle); - // handle should have only ONE item - if (CollectionUtils.isEmpty(itemList)) { - return null; - } - Item item = itemList.get(0); - MetadataField mf = metadataFieldService.findByString(context, "dc.date.accessioned", '.'); - Iterator mdv = metadataValueService.findByUUIDAndField(context, item.getID(), mf); - String metadataValue = null; - if (mdv.hasNext()) { - MetadataValue mdvVal = mdv.next(); - metadataValue = mdvVal.getValue(); - if (mdv.hasNext()) { - throw new SQLException("Ambiguous reference; multiple matches in db of" + - " dc.date.accessioned for handle: " + handle); - } - } - log.debug("Resolved {} to {}", handle, metadataValue); - return metadataValue; - } - - @Override - public String resolveToReportemail(Context context, String handle) throws SQLException { - return ""; - } - @Override public String resolveUrlToHandle(Context context, String url) throws SQLException { diff --git a/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java b/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java index cd26ae090393..85950ab6db87 100644 --- a/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java +++ b/dspace-api/src/main/java/org/dspace/handle/service/HandleService.java @@ -40,12 +40,6 @@ public interface HandleService { */ public String resolveToURL(Context context, String handle) throws SQLException; - public List resolveToTitle(Context context, String handle) throws SQLException; - public String resolveToRepository(Context context, String handle) throws SQLException; - - public String resolveToSubmitdate(Context context, String handle) throws SQLException; - - public String resolveToReportemail(Context context, String handle) throws SQLException; /** * Try to detect a handle in a URL. diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 540c3fd17243..59325fe39676 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -8,16 +8,26 @@ package org.dspace.app.rest.hdlresolver; import java.text.MessageFormat; +import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Optional; +import java.util.function.BiFunction; import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import net.handle.hdllib.HandleException; +import net.handle.hdllib.HandleValue; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.dspace.app.rest.utils.ContextUtil; +import org.dspace.core.Context; +import org.dspace.handle.HandlePlugin; import org.dspace.handle.hdlresolver.HdlResolverDTO; import org.dspace.handle.hdlresolver.HdlResolverService; import org.springframework.beans.factory.annotation.Autowired; @@ -29,6 +39,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.nio.charset.StandardCharsets; + /** * This controller is public and is useful for handle resolving, * whether a target handle identifier will be resolved into the @@ -102,7 +114,7 @@ public ResponseEntity resolveHandle(HttpServletRequest request, String h if (!handleResolver.isValid()) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } else { - return new ResponseEntity<>(this.resolveToURL(request, handleResolver), HttpStatus.OK); + return new ResponseEntity<>(this.resolveToMtd(request, handleResolver), HttpStatus.OK); } } @@ -171,14 +183,45 @@ public ResponseEntity listHandles(HttpServletRequest request, @PathVaria * @param handleResolver HdlResolverDTO - Handle resolver * @return One element list using String if found, else null String. */ - private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { - return mapAsJson(this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver)); + private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleResolver) { + Map resultMap = new HashMap<>(); + HandlePlugin hp = new HandlePlugin(); + String handle = handleResolver.getHandle(); + if (Objects.isNull(handle)) { + return null; + } + List handleVals = null; + try { + handleVals = hp.getListHandleValues(handle.getBytes(), new int[]{}, new byte[][]{}); + } catch (HandleException e) { + log.error(e); + } + if (CollectionUtils.isEmpty(handleVals)) { + return null; + } + + for (HandleValue handleVal : handleVals) { + String key = new String(handleVal.getType(), StandardCharsets.UTF_8).toLowerCase(); + String val = new String(handleVal.getData(), StandardCharsets.UTF_8); + String param = request.getParameter(key); + if (!Objects.equals(key, "url") || StringUtils.isBlank(param)) { + continue; + } + resultMap.put(key, val); + } + + return mapAsJson(resultMap); } - protected String mapAsJson(final String resolvedUrl) { + protected String mapAsJson(final Map resolvedMap) { + ObjectMapper objectMapper = new ObjectMapper(); String json = "null"; - if (StringUtils.isNotEmpty(resolvedUrl)) { - json = mapAsJson(List.of(resolvedUrl)); + try { + if (resolvedMap != null && !resolvedMap.isEmpty()) { + json = objectMapper.writeValueAsString(resolvedMap); + } + } catch (JsonProcessingException e) { + log.error("Error during conversion of response!", e); } return json; } @@ -194,5 +237,4 @@ protected String mapAsJson(final List jsonList) { } return json; } - } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 8227caffe616..4dc5f37ce830 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -10,6 +10,7 @@ import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -87,6 +88,38 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedURL() throw } + @Test + public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() throws Exception { + context.turnOffAuthorisationSystem(); + + // ** START GIVEN ** + + parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build(); + + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1") + .withLogo("TestingContentForLogo").build(); + + Item publicItem1 = ItemBuilder.createItem(context, col1).withTitle("Public item 1").withIssueDate("2017-10-17") + .withAuthor("Smith, Donald").withAuthor("Doe, John").withSubject("ExtraEntry") + .withHandle("123456789/testHdlResolver").build(); + + context.restoreAuthSystemState(); + + // ** END GIVEN ** + + getClient() + .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) + .param("title", "true") + .param("repository", "true") + .param("submitdate", "true") + .param("reportemail", "true")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) + .andExpect(jsonPath("$.repository", is("DSpace at My University"))) + .andExpect(jsonPath("$.url", + StringContains.containsString("123456789/testHdlResolver"))); + } + @Test public void givenAnyHandlesWhenDisabledListhandleThenReturnsNotFoundResp() throws Exception { From 9504ceeb77a7473896ebd8cc3d23b4b1a95ff3e1 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 16 Sep 2024 09:59:06 +0200 Subject: [PATCH 03/23] checkstyle violations --- .../HdlResolverRestController.java | 8 +--- .../HdlResolverRestControllerIT.java | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 59325fe39676..556675b77b20 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -7,14 +7,13 @@ */ package org.dspace.app.rest.hdlresolver; +import java.nio.charset.StandardCharsets; import java.text.MessageFormat; -import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.function.BiFunction; import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.core.JsonProcessingException; @@ -26,7 +25,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.dspace.app.rest.utils.ContextUtil; -import org.dspace.core.Context; import org.dspace.handle.HandlePlugin; import org.dspace.handle.hdlresolver.HdlResolverDTO; import org.dspace.handle.hdlresolver.HdlResolverService; @@ -39,8 +37,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.nio.charset.StandardCharsets; - /** * This controller is public and is useful for handle resolving, * whether a target handle identifier will be resolved into the @@ -204,7 +200,7 @@ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleRes String key = new String(handleVal.getType(), StandardCharsets.UTF_8).toLowerCase(); String val = new String(handleVal.getData(), StandardCharsets.UTF_8); String param = request.getParameter(key); - if (!Objects.equals(key, "url") || StringUtils.isBlank(param)) { + if (!Objects.equals(key, "url") && StringUtils.isBlank(param)) { continue; } resultMap.put(key, val); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 4dc5f37ce830..288ba12c2f90 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -9,13 +9,14 @@ import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import java.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -117,7 +118,41 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) .andExpect(jsonPath("$.repository", is("DSpace at My University"))) .andExpect(jsonPath("$.url", - StringContains.containsString("123456789/testHdlResolver"))); + StringContains.containsString("123456789/testHdlResolver"))) + .andExpect(jsonPath("$.reportemail", + StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))); + + } + + @Test + public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedTitleReportemail() throws Exception { + context.turnOffAuthorisationSystem(); + + // ** START GIVEN ** + + parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build(); + + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1") + .withLogo("TestingContentForLogo").build(); + + Item publicItem1 = ItemBuilder.createItem(context, col1).withTitle("Public item 1").withIssueDate("2017-10-17") + .withAuthor("Smith, Donald").withAuthor("Doe, John").withSubject("ExtraEntry") + .withHandle("123456789/testHdlResolver").build(); + + context.restoreAuthSystemState(); + + // ** END GIVEN ** + + getClient() + .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) + .param("title", "true") + .param("reportemail", "true")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) + .andExpect(jsonPath("$.url", + StringContains.containsString("123456789/testHdlResolver"))) + .andExpect(jsonPath("$.reportemail", + StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))); } @Test From d62ba904e34f1863b9d87ca3c0a10a1022b59a85 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 16 Sep 2024 10:08:53 +0200 Subject: [PATCH 04/23] checkstyle violations --- .../app/rest/hdlresolver/HdlResolverRestControllerIT.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 288ba12c2f90..0177cc4fc624 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -16,7 +16,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - import java.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -120,7 +119,8 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .andExpect(jsonPath("$.url", StringContains.containsString("123456789/testHdlResolver"))) .andExpect(jsonPath("$.reportemail", - StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))); + StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))) + .andExpect(jsonPath("$.submitdate").exists()); } @@ -152,7 +152,8 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedTitleReport .andExpect(jsonPath("$.url", StringContains.containsString("123456789/testHdlResolver"))) .andExpect(jsonPath("$.reportemail", - StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))); + StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))) + .andExpect(jsonPath("$.repository").doesNotExist()); } @Test From bfae430479d94787e231955bb802d2a406d0285c Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 16 Sep 2024 10:12:38 +0200 Subject: [PATCH 05/23] removed unnecessary services --- .../src/main/java/org/dspace/handle/HandleServiceImpl.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java index 7da5a0d3e53c..373b31d89750 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleServiceImpl.java @@ -28,8 +28,6 @@ import org.dspace.content.Community; import org.dspace.content.DSpaceObject; import org.dspace.content.Item; -import org.dspace.content.service.MetadataFieldService; -import org.dspace.content.service.MetadataValueService; import org.dspace.content.service.SiteService; import org.dspace.content.service.clarin.ClarinItemService; import org.dspace.core.Constants; @@ -73,10 +71,6 @@ public class HandleServiceImpl implements HandleService { protected SiteService siteService; @Autowired protected ClarinItemService clarinItemService; - @Autowired - protected MetadataFieldService metadataFieldService; - @Autowired - protected MetadataValueService metadataValueService; private static final Pattern[] IDENTIFIER_PATTERNS = { Pattern.compile("^hdl:(.*)$"), From 3d31ae42b41e720a000ea6c03abcfb4e6a43add0 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 16 Sep 2024 10:45:37 +0200 Subject: [PATCH 06/23] fixed the tests --- .../app/rest/hdlresolver/HdlResolverRestController.java | 4 ++-- .../app/rest/hdlresolver/HdlResolverRestControllerIT.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 556675b77b20..5920b9c63ff4 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -184,7 +184,7 @@ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleRes HandlePlugin hp = new HandlePlugin(); String handle = handleResolver.getHandle(); if (Objects.isNull(handle)) { - return null; + return "null"; } List handleVals = null; try { @@ -193,7 +193,7 @@ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleRes log.error(e); } if (CollectionUtils.isEmpty(handleVals)) { - return null; + return "null"; } for (HandleValue handleVal : handleVals) { diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 0177cc4fc624..a5cce0ff396c 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -77,11 +77,13 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedURL() throw getClient() .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle())) .andExpect(status().isOk()) - .andExpect(matchHandleResponse); + .andExpect(jsonPath("$.url", + StringContains.containsString("123456789/testHdlResolver"))); getClient() .perform(get(HdlResolverRestController.HDL_RESOLVER + publicItem1.getHandle())) .andExpect(status().isOk()) - .andExpect(matchHandleResponse); + .andExpect(jsonPath("$.url", + StringContains.containsString("123456789/testHdlResolver"))); getClient() .perform(get("/wrongController/" + publicItem1.getHandle())) .andExpect(status().isNotFound()); From 16358144de934546ca389d51009af34a4024b67e Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 23 Sep 2024 09:40:15 +0200 Subject: [PATCH 07/23] moved logs and added reportemail to cfg --- .../main/java/org/dspace/handle/HandlePlugin.java | 14 ++++++++------ .../src/test/data/dspaceFolder/config/local.cfg | 5 ++++- .../hdlresolver/HdlResolverRestControllerIT.java | 7 +++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index 4588b069da3e..d9ba512f034a 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -268,6 +268,9 @@ public void scanNAs(ScanCallback callback) throws HandleException { @Override public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, byte[][] typeList) throws HandleException { + if (log.isInfoEnabled()) { + log.info("Called getRawHandleValues"); + } ResolvedHandle rh = getHandleValues(theHandle, indexList, typeList); if (Objects.isNull(rh)) { return null; @@ -277,19 +280,18 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, public List getListHandleValues(byte[] theHandle, int[] indexList, byte[][] typeList) throws HandleException { + if (log.isInfoEnabled()) { + log.info("Called getListHandleValues"); + } ResolvedHandle rh = getHandleValues(theHandle, indexList, typeList); if (Objects.isNull(rh)) { return null; } - return rh.getHandleValue(); + return rh.getHandleValues(); } private ResolvedHandle getHandleValues(byte[] theHandle, int[] indexList, byte[][] typeList) throws HandleException { - if (log.isInfoEnabled()) { - log.info("Called getRawHandleValues"); - } - // Configuration, HandleClarin, Handle service loadServices(); @@ -752,7 +754,7 @@ public byte[][] toRawValue() throws HandleException { return rawValues; } - public List getHandleValue() { + public List getHandleValues() { return values; } diff --git a/dspace-api/src/test/data/dspaceFolder/config/local.cfg b/dspace-api/src/test/data/dspaceFolder/config/local.cfg index cc9ccb26bbb2..857b7908df0f 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/local.cfg +++ b/dspace-api/src/test/data/dspaceFolder/config/local.cfg @@ -306,4 +306,7 @@ sync.storage.service.enabled = false signposting.enabled = true # Test configuration has only EN locale (submission-forms.xml) -webui.supported.locales = en \ No newline at end of file +webui.supported.locales = en + +# Test configuration for HdlResolverRestController +handle.reportemail = dspace-help@ufal.mff.cuni.cz \ No newline at end of file diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index a5cce0ff396c..570c12718b5b 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -117,13 +117,12 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .param("reportemail", "true")) .andExpect(status().isOk()) .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) - .andExpect(jsonPath("$.repository", is("DSpace at My University"))) + .andExpect(jsonPath("$.repository", is(configurationService.getProperty("dspace.name")))) .andExpect(jsonPath("$.url", StringContains.containsString("123456789/testHdlResolver"))) .andExpect(jsonPath("$.reportemail", - StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))) + StringContains.containsString(configurationService.getProperty("handle.reportemail")))) .andExpect(jsonPath("$.submitdate").exists()); - } @Test @@ -154,7 +153,7 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedTitleReport .andExpect(jsonPath("$.url", StringContains.containsString("123456789/testHdlResolver"))) .andExpect(jsonPath("$.reportemail", - StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))) + StringContains.containsString(configurationService.getProperty("handle.reportemail")))) .andExpect(jsonPath("$.repository").doesNotExist()); } From 2ab50e551accbef95f673cc79c05d815f4b45ece Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 24 Sep 2024 10:29:56 +0200 Subject: [PATCH 08/23] decoded rawvalues and response json modification --- .../java/org/dspace/handle/HandlePlugin.java | 27 +---------- .../HdlResolverRestController.java | 46 +++++++++++++++---- .../HdlResolverRestControllerIT.java | 44 ++---------------- 3 files changed, 41 insertions(+), 76 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index d9ba512f034a..cb57252ba4b2 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -271,27 +271,6 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, if (log.isInfoEnabled()) { log.info("Called getRawHandleValues"); } - ResolvedHandle rh = getHandleValues(theHandle, indexList, typeList); - if (Objects.isNull(rh)) { - return null; - } - return rh.toRawValue(); - } - - public List getListHandleValues(byte[] theHandle, int[] indexList, - byte[][] typeList) throws HandleException { - if (log.isInfoEnabled()) { - log.info("Called getListHandleValues"); - } - ResolvedHandle rh = getHandleValues(theHandle, indexList, typeList); - if (Objects.isNull(rh)) { - return null; - } - return rh.getHandleValues(); - } - - private ResolvedHandle getHandleValues(byte[] theHandle, int[] indexList, - byte[][] typeList) throws HandleException { // Configuration, HandleClarin, Handle service loadServices(); @@ -361,7 +340,7 @@ private ResolvedHandle getHandleValues(byte[] theHandle, int[] indexList, rh.setDead(handle, deadSince); } - return rh; + return rh.toRawValue(); } catch (HandleException he) { throw he; } catch (Exception e) { @@ -754,10 +733,6 @@ public byte[][] toRawValue() throws HandleException { return rawValues; } - public List getHandleValues() { - return values; - } - public void setDead(String handle, String deadSince) { //find URL field for (HandleValue hv : values) { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 5920b9c63ff4..980b928bee32 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -18,9 +18,9 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import net.handle.hdllib.Encoder; import net.handle.hdllib.HandleException; import net.handle.hdllib.HandleValue; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -172,6 +172,17 @@ public ResponseEntity listHandles(HttpServletRequest request, @PathVaria ); } + /** + * Maps the handle to a correct response. + * + * @param request HttpServletRequest + * @param handleResolver HdlResolverDTO - Handle resolver + * @return One element list using String if found, else null String. + */ + private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { + return mapAsJson(this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver)); + } + /** * Maps the handle to a correct response. * @@ -180,35 +191,52 @@ public ResponseEntity listHandles(HttpServletRequest request, @PathVaria * @return One element list using String if found, else null String. */ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleResolver) { + + String param = request.getParameter("metadata"); + if (StringUtils.isBlank(param)) { + return resolveToURL(request, handleResolver); + } + Map resultMap = new HashMap<>(); HandlePlugin hp = new HandlePlugin(); String handle = handleResolver.getHandle(); if (Objects.isNull(handle)) { return "null"; } - List handleVals = null; + HandleValue[] handleValues = null; try { - handleVals = hp.getListHandleValues(handle.getBytes(), new int[]{}, new byte[][]{}); + byte[][] rawValues = hp.getRawHandleValues(handle.getBytes(), new int[]{}, new byte[][]{}); + handleValues = new HandleValue[rawValues.length]; + for (int i = 0; i < rawValues.length; i++) { + byte[] hvalueB = rawValues[i]; + handleValues[i] = new HandleValue(); + Encoder.decodeHandleValue(hvalueB, 0, handleValues[i]); + } } catch (HandleException e) { log.error(e); } - if (CollectionUtils.isEmpty(handleVals)) { + if (handleValues == null) { return "null"; } - for (HandleValue handleVal : handleVals) { + for (int i = 0; i < handleValues.length; i++) { + HandleValue handleVal = handleValues[i]; String key = new String(handleVal.getType(), StandardCharsets.UTF_8).toLowerCase(); String val = new String(handleVal.getData(), StandardCharsets.UTF_8); - String param = request.getParameter(key); - if (!Objects.equals(key, "url") && StringUtils.isBlank(param)) { - continue; - } resultMap.put(key, val); } return mapAsJson(resultMap); } + protected String mapAsJson(final String resolvedUrl) { + String json = "null"; + if (StringUtils.isNotEmpty(resolvedUrl)) { + json = mapAsJson(List.of(resolvedUrl)); + } + return json; + } + protected String mapAsJson(final Map resolvedMap) { ObjectMapper objectMapper = new ObjectMapper(); String json = "null"; diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 570c12718b5b..d5158006e8f5 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -77,17 +77,14 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedURL() throw getClient() .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle())) .andExpect(status().isOk()) - .andExpect(jsonPath("$.url", - StringContains.containsString("123456789/testHdlResolver"))); + .andExpect(matchHandleResponse); getClient() .perform(get(HdlResolverRestController.HDL_RESOLVER + publicItem1.getHandle())) .andExpect(status().isOk()) - .andExpect(jsonPath("$.url", - StringContains.containsString("123456789/testHdlResolver"))); + .andExpect(matchHandleResponse); getClient() .perform(get("/wrongController/" + publicItem1.getHandle())) .andExpect(status().isNotFound()); - } @Test @@ -111,10 +108,7 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th getClient() .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) - .param("title", "true") - .param("repository", "true") - .param("submitdate", "true") - .param("reportemail", "true")) + .param("metadata", "true")) .andExpect(status().isOk()) .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) .andExpect(jsonPath("$.repository", is(configurationService.getProperty("dspace.name")))) @@ -125,38 +119,6 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .andExpect(jsonPath("$.submitdate").exists()); } - @Test - public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedTitleReportemail() throws Exception { - context.turnOffAuthorisationSystem(); - - // ** START GIVEN ** - - parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build(); - - Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1") - .withLogo("TestingContentForLogo").build(); - - Item publicItem1 = ItemBuilder.createItem(context, col1).withTitle("Public item 1").withIssueDate("2017-10-17") - .withAuthor("Smith, Donald").withAuthor("Doe, John").withSubject("ExtraEntry") - .withHandle("123456789/testHdlResolver").build(); - - context.restoreAuthSystemState(); - - // ** END GIVEN ** - - getClient() - .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) - .param("title", "true") - .param("reportemail", "true")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) - .andExpect(jsonPath("$.url", - StringContains.containsString("123456789/testHdlResolver"))) - .andExpect(jsonPath("$.reportemail", - StringContains.containsString(configurationService.getProperty("handle.reportemail")))) - .andExpect(jsonPath("$.repository").doesNotExist()); - } - @Test public void givenAnyHandlesWhenDisabledListhandleThenReturnsNotFoundResp() throws Exception { From 03929bae7f8a09b9ac5a603d1d8dc3612a7bdb5f Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 24 Sep 2024 10:32:51 +0200 Subject: [PATCH 09/23] added empty lines --- dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java | 1 + .../dspace/app/rest/hdlresolver/HdlResolverRestController.java | 1 + 2 files changed, 2 insertions(+) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index cb57252ba4b2..1275ef81d695 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -271,6 +271,7 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, if (log.isInfoEnabled()) { log.info("Called getRawHandleValues"); } + // Configuration, HandleClarin, Handle service loadServices(); diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 980b928bee32..b9b901af9403 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -261,4 +261,5 @@ protected String mapAsJson(final List jsonList) { } return json; } + } From 437e8c226dd36652a063ae97a8d93bf607271f78 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 24 Sep 2024 10:34:40 +0200 Subject: [PATCH 10/23] removed empty line --- .../dspace/app/rest/hdlresolver/HdlResolverRestController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index b9b901af9403..980b928bee32 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -261,5 +261,4 @@ protected String mapAsJson(final List jsonList) { } return json; } - } From 8d25def1658dd24c75b0b8ecfa4b74f2086fb5c4 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 7 Oct 2024 14:08:22 +0200 Subject: [PATCH 11/23] used static extractMetadata funct in HandlePlugin --- .../java/org/dspace/handle/HandlePlugin.java | 65 ++++++++++++++++--- .../HdlResolverRestController.java | 50 ++++---------- .../HdlResolverRestControllerIT.java | 10 +-- 3 files changed, 76 insertions(+), 49 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index 1275ef81d695..cbef31218505 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -254,6 +254,53 @@ public void scanNAs(ScanCallback callback) throws HandleException { // Resolving methods //////////////////////////////////////// + /** + * Extracts a DSpaceObject based on the given handle. + * + * @param context the context + * @param handle the handle to resolve + * @return the resolved DSpaceObject or null if not resolved + * @throws HandleException if an error occurs during resolution + */ + private static DSpaceObject extractDSpaceObject(Context context, String handle) throws HandleException { + boolean resolveMetadata = configurationService.getBooleanProperty("lr.pid.resolvemetadata", true); + try { + if (resolveMetadata) { + return handleClarinService.resolveToObject(context, handle); + } + return null; + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("Exception in extractDSpaceObject", e); + } + throw new HandleException(HandleException.INTERNAL_ERROR); + } + } + + /** + * Retrieves handle values as a map. + * + * @param handle the handle to resolve + * @return a map containing the handle values + * @throws HandleException if an error occurs during handle resolution + */ + public static Map getMapHandleValues(String handle) throws HandleException { + if (log.isInfoEnabled()) { + log.info("Called getMapHandleValues"); + } + loadServices(); + Context context = new Context(); + try { + return extractMetadata(extractDSpaceObject(context, handle)); + } finally { + try { + context.complete(); + } catch (SQLException sqle) { + // ignore + } + } + } + /** * Return the raw values for this handle. This implementation returns a * single URL value. @@ -285,15 +332,7 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, String handle = Util.decodeString(theHandle); context = new Context(); - - DSpaceObject dso = null; String url = handleClarinService.resolveToURL(context, handle); - - boolean resolveMetadata = configurationService.getBooleanProperty("lr.pid.resolvemetadata", true); - if (resolveMetadata) { - dso = handleClarinService.resolveToObject(context, handle); - } - if (Objects.isNull(url)) { // try with old prefix @@ -332,6 +371,7 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, rh = new ResolvedHandle(url, splits[1], splits[2], splits[3], splits[4], splits[5], splits[6], splits[7]); } else { + DSpaceObject dso = extractDSpaceObject(context, handle); rh = new ResolvedHandle(url, dso); } log.info(String.format("Handle [%s] resolved to [%s]", handle, url)); @@ -484,6 +524,15 @@ private static void loadServices() { } } + private static void loadHandleClarinAndConfigService() { + if (Objects.isNull(handleClarinService)) { + handleClarinService = ContentServiceFactory.getInstance().getHandleClarinService(); + } + if (Objects.isNull(configurationService)) { + configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); + } + } + /** * Load the repository email from the configuration. The mail is in the property `help.mail`. * diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 980b928bee32..c5427722e11a 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -173,60 +173,38 @@ public ResponseEntity listHandles(HttpServletRequest request, @PathVaria } /** - * Maps the handle to a correct response. + * Maps the handle to url. * * @param request HttpServletRequest * @param handleResolver HdlResolverDTO - Handle resolver - * @return One element list using String if found, else null String. + * @return String if found, else null String. */ private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { - return mapAsJson(this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver)); + return this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver); } /** - * Maps the handle to a correct response. + * Resolves the metadata based on the given request and handle resolver. * - * @param request HttpServletRequest - * @param handleResolver HdlResolverDTO - Handle resolver - * @return One element list using String if found, else null String. + * @param request the HTTP request containing the metadata parameter + * @param handleResolver the handle resolver containing the handle + * @return a JSON representation of the URL or the handle values map, or "null" in case of an error */ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleResolver) { - + String url = resolveToURL(request, handleResolver); String param = request.getParameter("metadata"); if (StringUtils.isBlank(param)) { - return resolveToURL(request, handleResolver); + return mapAsJson(url); } - - Map resultMap = new HashMap<>(); - HandlePlugin hp = new HandlePlugin(); String handle = handleResolver.getHandle(); - if (Objects.isNull(handle)) { - return "null"; - } - HandleValue[] handleValues = null; try { - byte[][] rawValues = hp.getRawHandleValues(handle.getBytes(), new int[]{}, new byte[][]{}); - handleValues = new HandleValue[rawValues.length]; - for (int i = 0; i < rawValues.length; i++) { - byte[] hvalueB = rawValues[i]; - handleValues[i] = new HandleValue(); - Encoder.decodeHandleValue(hvalueB, 0, handleValues[i]); - } + Map map = HandlePlugin.getMapHandleValues(handle); + map.put("URL", url); + return mapAsJson(map); } catch (HandleException e) { - log.error(e); - } - if (handleValues == null) { - return "null"; + log.error("Failed to resolve handle values for handle: " + handle, e); } - - for (int i = 0; i < handleValues.length; i++) { - HandleValue handleVal = handleValues[i]; - String key = new String(handleVal.getType(), StandardCharsets.UTF_8).toLowerCase(); - String val = new String(handleVal.getData(), StandardCharsets.UTF_8); - resultMap.put(key, val); - } - - return mapAsJson(resultMap); + return "null"; } protected String mapAsJson(final String resolvedUrl) { diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index d5158006e8f5..2a54c0803c9c 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -110,13 +110,13 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) .param("metadata", "true")) .andExpect(status().isOk()) - .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) - .andExpect(jsonPath("$.repository", is(configurationService.getProperty("dspace.name")))) - .andExpect(jsonPath("$.url", + .andExpect(jsonPath("$.TITLE", StringContains.containsString("Public item 1"))) + .andExpect(jsonPath("$.REPOSITORY", is(configurationService.getProperty("dspace.name")))) + .andExpect(jsonPath("$.URL", StringContains.containsString("123456789/testHdlResolver"))) - .andExpect(jsonPath("$.reportemail", + .andExpect(jsonPath("$.REPORTEMAIL", StringContains.containsString(configurationService.getProperty("handle.reportemail")))) - .andExpect(jsonPath("$.submitdate").exists()); + .andExpect(jsonPath("$.SUBMITDATE").exists()); } @Test From 499b0ede8e65be25bc1292eda17fad0ccaf71830 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 7 Oct 2024 14:13:18 +0200 Subject: [PATCH 12/23] removed unneeded method --- .../src/main/java/org/dspace/handle/HandlePlugin.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index cbef31218505..67cd2b12ffca 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -524,15 +524,6 @@ private static void loadServices() { } } - private static void loadHandleClarinAndConfigService() { - if (Objects.isNull(handleClarinService)) { - handleClarinService = ContentServiceFactory.getInstance().getHandleClarinService(); - } - if (Objects.isNull(configurationService)) { - configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); - } - } - /** * Load the repository email from the configuration. The mail is in the property `help.mail`. * From 288173cfb10d0162b80bbd86697138270a73c5af Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Mon, 7 Oct 2024 23:25:45 +0200 Subject: [PATCH 13/23] checkstyle violations --- .../app/rest/hdlresolver/HdlResolverRestController.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index c5427722e11a..948ff751ac2e 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -7,20 +7,15 @@ */ package org.dspace.app.rest.hdlresolver; -import java.nio.charset.StandardCharsets; import java.text.MessageFormat; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import net.handle.hdllib.Encoder; import net.handle.hdllib.HandleException; -import net.handle.hdllib.HandleValue; import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; From fd9b0e1a7489787c6f688cf4448822d112c6f1d3 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 8 Oct 2024 07:59:40 +0200 Subject: [PATCH 14/23] additional info as metadata --- .../HdlResolverRestController.java | 9 ++++--- .../HdlResolverRestControllerIT.java | 25 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 948ff751ac2e..29014f1686d9 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -8,6 +8,7 @@ package org.dspace.app.rest.hdlresolver; import java.text.MessageFormat; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -186,16 +187,18 @@ private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleRes * @return a JSON representation of the URL or the handle values map, or "null" in case of an error */ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleResolver) { + Map result = new HashMap<>(); String url = resolveToURL(request, handleResolver); String param = request.getParameter("metadata"); if (StringUtils.isBlank(param)) { return mapAsJson(url); } + result.put("url", url); String handle = handleResolver.getHandle(); try { - Map map = HandlePlugin.getMapHandleValues(handle); - map.put("URL", url); - return mapAsJson(map); + Map metadata = HandlePlugin.getMapHandleValues(handle); + result.put("metadata", mapAsJson(metadata)); + return mapAsJson(result); } catch (HandleException e) { log.error("Failed to resolve handle values for handle: " + handle, e); } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 2a54c0803c9c..ddea64287b94 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -7,9 +7,10 @@ */ package org.dspace.app.rest.hdlresolver; +import static org.junit.Assert.assertTrue; + import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; @@ -19,7 +20,9 @@ import java.sql.SQLException; import java.util.Arrays; import java.util.List; +import java.util.Objects; +import org.json.JSONObject; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; @@ -31,6 +34,7 @@ import org.hamcrest.core.StringContains; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; /** @@ -106,17 +110,22 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th // ** END GIVEN ** - getClient() + MvcResult result = getClient() .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) .param("metadata", "true")) .andExpect(status().isOk()) - .andExpect(jsonPath("$.TITLE", StringContains.containsString("Public item 1"))) - .andExpect(jsonPath("$.REPOSITORY", is(configurationService.getProperty("dspace.name")))) - .andExpect(jsonPath("$.URL", + .andExpect(jsonPath("$.url", StringContains.containsString("123456789/testHdlResolver"))) - .andExpect(jsonPath("$.REPORTEMAIL", - StringContains.containsString(configurationService.getProperty("handle.reportemail")))) - .andExpect(jsonPath("$.SUBMITDATE").exists()); + .andExpect(jsonPath("$.metadata").exists()) + .andReturn(); + + String metadata = new JSONObject(result.getResponse().getContentAsString()).get("metadata").toString(); + JSONObject jsonObject = new JSONObject(metadata); + + assert Objects.equals(jsonObject.get("TITLE"), "Public item 1"); + assert Objects.equals(jsonObject.get("REPORTEMAIL"), configurationService.getProperty("handle.reportemail")); + assert Objects.equals(jsonObject.get("REPOSITORY"), configurationService.getProperty("dspace.name")); + assertTrue(jsonObject.has("SUBMITDATE")); } @Test From 01b72a82b65679692f55ef55f37429589a5b0f75 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 8 Oct 2024 08:01:12 +0200 Subject: [PATCH 15/23] checkstyle violations --- .../app/rest/hdlresolver/HdlResolverRestControllerIT.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index ddea64287b94..4ad0852c9ff3 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -7,11 +7,12 @@ */ package org.dspace.app.rest.hdlresolver; -import static org.junit.Assert.assertTrue; - import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.not; + +import static org.junit.Assert.assertTrue; + import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -22,7 +23,6 @@ import java.util.List; import java.util.Objects; -import org.json.JSONObject; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; @@ -32,6 +32,7 @@ import org.dspace.handle.hdlresolver.HdlResolverServiceImpl; import org.dspace.services.ConfigurationService; import org.hamcrest.core.StringContains; +import org.json.JSONObject; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.MvcResult; From 9043583eff1b250244d56f8aa7df5fb7756813c4 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 8 Oct 2024 08:02:01 +0200 Subject: [PATCH 16/23] checkstyle violations --- .../app/rest/hdlresolver/HdlResolverRestControllerIT.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 4ad0852c9ff3..9a928b194ae9 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -10,9 +10,7 @@ import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.not; - import static org.junit.Assert.assertTrue; - import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; From a7475b2ce7dcd22343497ec00110ef6845191d73 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 8 Oct 2024 10:31:26 +0200 Subject: [PATCH 17/23] return dict: --- .../HdlResolverRestController.java | 4 +++- .../HdlResolverRestControllerIT.java | 22 ++++++------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 29014f1686d9..f82cecaeae24 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -197,7 +197,9 @@ private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleRes String handle = handleResolver.getHandle(); try { Map metadata = HandlePlugin.getMapHandleValues(handle); - result.put("metadata", mapAsJson(metadata)); + for (Map.Entry entry : metadata.entrySet()) { + result.put(entry.getKey().toLowerCase(), entry.getValue()); + } return mapAsJson(result); } catch (HandleException e) { log.error("Failed to resolve handle values for handle: " + handle, e); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 9a928b194ae9..954004f89f11 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -9,8 +9,8 @@ import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -19,7 +19,6 @@ import java.sql.SQLException; import java.util.Arrays; import java.util.List; -import java.util.Objects; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.CollectionBuilder; @@ -30,7 +29,6 @@ import org.dspace.handle.hdlresolver.HdlResolverServiceImpl; import org.dspace.services.ConfigurationService; import org.hamcrest.core.StringContains; -import org.json.JSONObject; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.MvcResult; @@ -108,23 +106,17 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th context.restoreAuthSystemState(); // ** END GIVEN ** - - MvcResult result = getClient() + getClient() .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) .param("metadata", "true")) .andExpect(status().isOk()) .andExpect(jsonPath("$.url", StringContains.containsString("123456789/testHdlResolver"))) - .andExpect(jsonPath("$.metadata").exists()) - .andReturn(); - - String metadata = new JSONObject(result.getResponse().getContentAsString()).get("metadata").toString(); - JSONObject jsonObject = new JSONObject(metadata); - - assert Objects.equals(jsonObject.get("TITLE"), "Public item 1"); - assert Objects.equals(jsonObject.get("REPORTEMAIL"), configurationService.getProperty("handle.reportemail")); - assert Objects.equals(jsonObject.get("REPOSITORY"), configurationService.getProperty("dspace.name")); - assertTrue(jsonObject.has("SUBMITDATE")); + .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) + .andExpect(jsonPath("$.repository", is(configurationService.getProperty("dspace.name")))) + .andExpect(jsonPath("$.reportemail", + StringContains.containsString(configurationService.getProperty("handle.reportemail")))) + .andExpect(jsonPath("$.submitdate").exists()); } @Test From 65ed0cdba267f88e220bc45859b762fda09d96f3 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 8 Oct 2024 10:32:05 +0200 Subject: [PATCH 18/23] checkstyle violations --- .../dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 954004f89f11..cc5366403d52 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -31,7 +31,6 @@ import org.hamcrest.core.StringContains; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; /** From 1ecb4430670be82a962081985ba72052ab8dee0b Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Tue, 8 Oct 2024 12:52:27 +0200 Subject: [PATCH 19/23] removed property for test from local --- dspace-api/src/test/data/dspaceFolder/config/local.cfg | 3 --- .../app/rest/hdlresolver/HdlResolverRestControllerIT.java | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/dspace-api/src/test/data/dspaceFolder/config/local.cfg b/dspace-api/src/test/data/dspaceFolder/config/local.cfg index 857b7908df0f..6b0ef5bbb180 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/local.cfg +++ b/dspace-api/src/test/data/dspaceFolder/config/local.cfg @@ -307,6 +307,3 @@ signposting.enabled = true # Test configuration has only EN locale (submission-forms.xml) webui.supported.locales = en - -# Test configuration for HdlResolverRestController -handle.reportemail = dspace-help@ufal.mff.cuni.cz \ No newline at end of file diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index cc5366403d52..5475850c10f8 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -114,7 +114,7 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) .andExpect(jsonPath("$.repository", is(configurationService.getProperty("dspace.name")))) .andExpect(jsonPath("$.reportemail", - StringContains.containsString(configurationService.getProperty("handle.reportemail")))) + StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))) .andExpect(jsonPath("$.submitdate").exists()); } From 14202d6fc11ed5877a12cb4556e1e53e7b3be6be Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 10 Oct 2024 12:02:44 +0200 Subject: [PATCH 20/23] removed empty line --- .../dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 5475850c10f8..83c6b5cbad1e 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -92,7 +92,6 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th context.turnOffAuthorisationSystem(); // ** START GIVEN ** - parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build(); Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1") From 37d91d835f57a1d644f1fcb5b7f365143828be72 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 10 Oct 2024 13:55:49 +0200 Subject: [PATCH 21/23] removed config var from basic funct, removed converting to lower case --- .../java/org/dspace/handle/HandlePlugin.java | 27 ++++++++++-------- .../test/data/dspaceFolder/config/local.cfg | 2 +- .../HdlResolverRestController.java | 28 +++++-------------- .../HdlResolverRestControllerIT.java | 10 +++---- 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java index 67cd2b12ffca..a22736c179bb 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java @@ -255,23 +255,19 @@ public void scanNAs(ScanCallback callback) throws HandleException { //////////////////////////////////////// /** - * Extracts a DSpaceObject based on the given handle. + * Resolve the given handle to DSpace object. * * @param context the context * @param handle the handle to resolve - * @return the resolved DSpaceObject or null if not resolved + * @return the resolved DSpaceObject * @throws HandleException if an error occurs during resolution */ - private static DSpaceObject extractDSpaceObject(Context context, String handle) throws HandleException { - boolean resolveMetadata = configurationService.getBooleanProperty("lr.pid.resolvemetadata", true); + private static DSpaceObject resolveHandleToObject(Context context, String handle) throws HandleException { try { - if (resolveMetadata) { - return handleClarinService.resolveToObject(context, handle); - } - return null; + return handleClarinService.resolveToObject(context, handle); } catch (Exception e) { if (log.isDebugEnabled()) { - log.debug("Exception in extractDSpaceObject", e); + log.debug("Exception in resolveHandleToObject", e); } throw new HandleException(HandleException.INTERNAL_ERROR); } @@ -291,7 +287,12 @@ public static Map getMapHandleValues(String handle) throws Handl loadServices(); Context context = new Context(); try { - return extractMetadata(extractDSpaceObject(context, handle)); + DSpaceObject dso = null; + boolean resolveMetadata = configurationService.getBooleanProperty("lr.pid.resolvemetadata", true); + if (resolveMetadata) { + dso = resolveHandleToObject(context, handle); + } + return extractMetadata(dso); } finally { try { context.complete(); @@ -371,7 +372,11 @@ public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList, rh = new ResolvedHandle(url, splits[1], splits[2], splits[3], splits[4], splits[5], splits[6], splits[7]); } else { - DSpaceObject dso = extractDSpaceObject(context, handle); + DSpaceObject dso = null; + boolean resolveMetadata = configurationService.getBooleanProperty("lr.pid.resolvemetadata", true); + if (resolveMetadata) { + dso = resolveHandleToObject(context, handle); + } rh = new ResolvedHandle(url, dso); } log.info(String.format("Handle [%s] resolved to [%s]", handle, url)); diff --git a/dspace-api/src/test/data/dspaceFolder/config/local.cfg b/dspace-api/src/test/data/dspaceFolder/config/local.cfg index 6b0ef5bbb180..cc9ccb26bbb2 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/local.cfg +++ b/dspace-api/src/test/data/dspaceFolder/config/local.cfg @@ -306,4 +306,4 @@ sync.storage.service.enabled = false signposting.enabled = true # Test configuration has only EN locale (submission-forms.xml) -webui.supported.locales = en +webui.supported.locales = en \ No newline at end of file diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index f82cecaeae24..eae2c44f365b 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -106,7 +106,7 @@ public ResponseEntity resolveHandle(HttpServletRequest request, String h if (!handleResolver.isValid()) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } else { - return new ResponseEntity<>(this.resolveToMtd(request, handleResolver), HttpStatus.OK); + return new ResponseEntity<>(this.resolveToURL(request, handleResolver), HttpStatus.OK); } } @@ -169,38 +169,24 @@ public ResponseEntity listHandles(HttpServletRequest request, @PathVaria } /** - * Maps the handle to url. - * - * @param request HttpServletRequest - * @param handleResolver HdlResolverDTO - Handle resolver - * @return String if found, else null String. - */ - private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { - return this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver); - } - - /** - * Resolves the metadata based on the given request and handle resolver. + * Maps the handle to a correct response. + * If the metadata parameter is provided, return additional handle values. * * @param request the HTTP request containing the metadata parameter * @param handleResolver the handle resolver containing the handle * @return a JSON representation of the URL or the handle values map, or "null" in case of an error */ - private String resolveToMtd(HttpServletRequest request, HdlResolverDTO handleResolver) { - Map result = new HashMap<>(); - String url = resolveToURL(request, handleResolver); + private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { + String url = this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver); String param = request.getParameter("metadata"); if (StringUtils.isBlank(param)) { return mapAsJson(url); } - result.put("url", url); String handle = handleResolver.getHandle(); try { Map metadata = HandlePlugin.getMapHandleValues(handle); - for (Map.Entry entry : metadata.entrySet()) { - result.put(entry.getKey().toLowerCase(), entry.getValue()); - } - return mapAsJson(result); + metadata.put("URL", url); + return mapAsJson(metadata); } catch (HandleException e) { log.error("Failed to resolve handle values for handle: " + handle, e); } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java index 83c6b5cbad1e..f19f4944478e 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/hdlresolver/HdlResolverRestControllerIT.java @@ -108,13 +108,13 @@ public void givenMappedIdentifierWhenCallHdlresolverThenReturnsMappedParams() th .perform(get(HdlResolverRestController.RESOLVE + publicItem1.getHandle()) .param("metadata", "true")) .andExpect(status().isOk()) - .andExpect(jsonPath("$.url", + .andExpect(jsonPath("$.URL", StringContains.containsString("123456789/testHdlResolver"))) - .andExpect(jsonPath("$.title", StringContains.containsString("Public item 1"))) - .andExpect(jsonPath("$.repository", is(configurationService.getProperty("dspace.name")))) - .andExpect(jsonPath("$.reportemail", + .andExpect(jsonPath("$.TITLE", StringContains.containsString("Public item 1"))) + .andExpect(jsonPath("$.REPOSITORY", is(configurationService.getProperty("dspace.name")))) + .andExpect(jsonPath("$.REPORTEMAIL", StringContains.containsString("dspace-help@ufal.mff.cuni.cz"))) - .andExpect(jsonPath("$.submitdate").exists()); + .andExpect(jsonPath("$.SUBMITDATE").exists()); } @Test From e0b81382f9d69ee8d4cddc861376c50241169a4e Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 10 Oct 2024 14:04:05 +0200 Subject: [PATCH 22/23] doc comments for resolveToURL funct --- .../app/rest/hdlresolver/HdlResolverRestController.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index eae2c44f365b..2dacd9e57dd7 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -172,9 +172,10 @@ public ResponseEntity listHandles(HttpServletRequest request, @PathVaria * Maps the handle to a correct response. * If the metadata parameter is provided, return additional handle values. * - * @param request the HTTP request containing the metadata parameter - * @param handleResolver the handle resolver containing the handle - * @return a JSON representation of the URL or the handle values map, or "null" in case of an error + * @param request HttpServletRequest + * @param handleResolver HdlResolverDTO - Handle resolver + * @return One element list using String if found, else map of metadata if param is entered, + * else "null" in case of an error */ private String resolveToURL(HttpServletRequest request, HdlResolverDTO handleResolver) { String url = this.hdlResolverService.resolveToURL(ContextUtil.obtainContext(request), handleResolver); From 56b7c2c6a9dad51be970caf6f42aa89103dee02a Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Thu, 10 Oct 2024 14:05:19 +0200 Subject: [PATCH 23/23] removed unused import --- .../dspace/app/rest/hdlresolver/HdlResolverRestController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java index 2dacd9e57dd7..ac2a4a1f7df6 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/hdlresolver/HdlResolverRestController.java @@ -8,7 +8,6 @@ package org.dspace.app.rest.hdlresolver; import java.text.MessageFormat; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional;