Skip to content

Commit

Permalink
Restore get license link and endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Jan 20, 2024
1 parent 8eb9875 commit 4d1e8f2
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -51,51 +51,46 @@ public class CollectionLicenseLinkRepository extends AbstractDSpaceRestRepositor
private ConfigurationService configurationService;

@PreAuthorize("hasPermission(#collectionId, 'COLLECTION', 'READ')")
public Page<LicenseRest> 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);
if (collection == null) {
throw new ResourceNotFoundException("No such collection: " + collectionId);
}

List<LicenseRest> 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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<LicenseRest> 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<LicenseRest> 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);
}
}

}

0 comments on commit 4d1e8f2

Please sign in to comment.