From 4d1e8f2e2c0d1aca76c96b18c75b7de27c1ae762 Mon Sep 17 00:00:00 2001 From: William Welling Date: Fri, 19 Jan 2024 18:26:12 -0600 Subject: [PATCH] Restore get license link and endpoint --- .../dspace/app/rest/model/CollectionRest.java | 10 +- .../CollectionLicenseLinkRepository.java | 57 +++++----- .../CollectionLicensesLinkRepository.java | 103 ++++++++++++++++++ 3 files changed, 135 insertions(+), 35 deletions(-) create mode 100644 dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicensesLinkRepository.java diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/model/CollectionRest.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/model/CollectionRest.java index 1760115166b..71ea3936b63 100644 --- a/dspace/modules/server/src/main/java/org/dspace/app/rest/model/CollectionRest.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/model/CollectionRest.java @@ -15,12 +15,14 @@ * @author Andrea Bollini (andrea.bollini at 4science.it) */ @LinksRest(links = { + // TAMU Customization - proxy license step @LinkRest( - // TAMU Customization - proxy license step name = CollectionRest.LICENSES, method = "getLicenses" - // name = CollectionRest.LICENSE, - // method = "getLicense" + ), + @LinkRest( + name = CollectionRest.LICENSE, + method = "getLicense" ), @LinkRest( name = CollectionRest.LOGO, @@ -57,8 +59,8 @@ public class CollectionRest extends DSpaceObjectRest { public static final String CATEGORY = RestAddressableModel.CORE; public static final String HARVEST = "harvester"; + public static final String LICENSE = "license"; // TAMU Customization - proxy license step - // public static final String LICENSE = "license"; public static final String LICENSES = "licenses"; public static final String LOGO = "logo"; public static final String MAPPED_ITEMS = "mappedItems"; diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicenseLinkRepository.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicenseLinkRepository.java index aa7b3b4cc7c..966451a9b28 100644 --- a/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicenseLinkRepository.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicenseLinkRepository.java @@ -37,7 +37,7 @@ * * @author Luigi Andrea Pascarelli (luigiandrea.pascarelli at 4science.it) */ -@Component(CollectionRest.CATEGORY + "." + CollectionRest.NAME + "." + CollectionRest.LICENSES) +@Component(CollectionRest.CATEGORY + "." + CollectionRest.NAME + "." + CollectionRest.LICENSE) public class CollectionLicenseLinkRepository extends AbstractDSpaceRestRepository implements LinkRestRepository { @@ -51,10 +51,10 @@ public class CollectionLicenseLinkRepository extends AbstractDSpaceRestRepositor private ConfigurationService configurationService; @PreAuthorize("hasPermission(#collectionId, 'COLLECTION', 'READ')") - public Page getLicenses(@Nullable HttpServletRequest request, - UUID collectionId, - @Nullable Pageable optionalPageable, - Projection projection) { + public LicenseRest getLicense(@Nullable HttpServletRequest request, + UUID collectionId, + @Nullable Pageable pageable, + Projection projection) { try { Context context = obtainContext(); Collection collection = collectionService.find(context, collectionId); @@ -62,40 +62,35 @@ public Page getLicenses(@Nullable HttpServletRequest request, throw new ResourceNotFoundException("No such collection: " + collectionId); } - List licenses = new ArrayList<>(); + // TAMU Customization - use customized LicenseRest DTO + String license = "default"; - Pageable pageable = utils.getPageable(optionalPageable); + boolean custom = false; - for (String filename : licenseService.getLicenseFilenames()) { - String[] parts = filename.split("\\."); - String license = parts[0]; + String label = configurationService.getProperty(String.join(".", + "license", license, "label")); - boolean isDefault = license.equalsIgnoreCase("default"); + String text = collection.getLicenseCollection(); - boolean custom = false; - - String licensePath = String.join(File.separator, - configurationService.getProperty("dspace.dir"), "config", filename); - - String label = configurationService.getProperty(String.join(".", - "license", license, "label")); - - String text = isDefault - ? collection.getLicenseCollection() - : licenseService.getLicenseText(licensePath); - - if (StringUtils.isNotBlank(text)) { - custom = isDefault; - } else { - text = licenseService.getDefaultSubmissionLicense(); - } - - licenses.add(LicenseRest.of(license, label, text, custom)); + if (StringUtils.isNotBlank(text)) { + custom = true; + } else { + text = licenseService.getDefaultSubmissionLicense(); } - return new PageImpl(licenses, pageable, licenses.size()); + return LicenseRest.of(license, label, text, custom); + // LicenseRest licenseRest = new LicenseRest(); + // String text = collection.getLicenseCollection(); + // if (StringUtils.isNotBlank(text)) { + // licenseRest.setCustom(true); + // licenseRest.setText(text); + // } else { + // licenseRest.setText(licenseService.getDefaultSubmissionLicense()); + // } + // return licenseRest; } catch (SQLException e) { throw new RuntimeException(e); } } + } diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicensesLinkRepository.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicensesLinkRepository.java new file mode 100644 index 00000000000..3ca30fc9845 --- /dev/null +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/repository/CollectionLicensesLinkRepository.java @@ -0,0 +1,103 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.repository; + +import java.io.File; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import javax.annotation.Nullable; +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.StringUtils; +import org.dspace.app.rest.model.CollectionRest; +import org.dspace.app.rest.model.LicenseRest; +import org.dspace.app.rest.projection.Projection; +import org.dspace.content.Collection; +import org.dspace.content.service.CollectionService; +import org.dspace.core.Context; +import org.dspace.core.service.LicenseService; +import org.dspace.services.ConfigurationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Component; + +/** + * TAMU Customization - Customized License Link repository for "license" subresource of an individual collection. + * + * @author Luigi Andrea Pascarelli (luigiandrea.pascarelli at 4science.it) + */ +@Component(CollectionRest.CATEGORY + "." + CollectionRest.NAME + "." + CollectionRest.LICENSES) +public class CollectionLicensesLinkRepository extends AbstractDSpaceRestRepository + implements LinkRestRepository { + + @Autowired + private CollectionService collectionService; + + @Autowired + private LicenseService licenseService; + + @Autowired + private ConfigurationService configurationService; + + // TAMU Customization - get available licenses + @PreAuthorize("hasPermission(#collectionId, 'COLLECTION', 'READ')") + public Page getLicenses(@Nullable HttpServletRequest request, + UUID collectionId, + @Nullable Pageable optionalPageable, + Projection projection) { + try { + Context context = obtainContext(); + Collection collection = collectionService.find(context, collectionId); + if (collection == null) { + throw new ResourceNotFoundException("No such collection: " + collectionId); + } + + List licenses = new ArrayList<>(); + + Pageable pageable = utils.getPageable(optionalPageable); + + for (String filename : licenseService.getLicenseFilenames()) { + String[] parts = filename.split("\\."); + String license = parts[0]; + + boolean isDefault = license.equalsIgnoreCase("default"); + + boolean custom = false; + + String licensePath = String.join(File.separator, + configurationService.getProperty("dspace.dir"), "config", filename); + + String label = configurationService.getProperty(String.join(".", + "license", license, "label")); + + String text = isDefault + ? collection.getLicenseCollection() + : licenseService.getLicenseText(licensePath); + + if (StringUtils.isNotBlank(text)) { + custom = isDefault; + } else { + text = licenseService.getDefaultSubmissionLicense(); + } + + licenses.add(LicenseRest.of(license, label, text, custom)); + } + + return new PageImpl(licenses, pageable, licenses.size()); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + +}