diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java index 74834e5d6d5..3c716e05bea 100644 --- a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java @@ -10,9 +10,9 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.BooleanUtils; +import org.dspace.app.rest.utils.ProxyLicenseUtils; import org.dspace.content.InProgressSubmission; import org.dspace.content.Item; -import org.dspace.content.ProxyLicenseUtils; import org.dspace.content.service.ItemService; import org.dspace.core.Context; import org.springframework.beans.factory.annotation.Autowired; diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java index e7f7c4049d3..2130364e158 100644 --- a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java @@ -9,9 +9,9 @@ import javax.servlet.http.HttpServletRequest; +import org.dspace.app.rest.utils.ProxyLicenseUtils; import org.dspace.content.InProgressSubmission; import org.dspace.content.Item; -import org.dspace.content.ProxyLicenseUtils; import org.dspace.content.service.ItemService; import org.dspace.core.Context; import org.springframework.beans.factory.annotation.Autowired; diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseSelectedAddPatchOperation.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseSelectedAddPatchOperation.java index 69bb3a06714..afd7d893f9a 100644 --- a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseSelectedAddPatchOperation.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseSelectedAddPatchOperation.java @@ -8,19 +8,15 @@ package org.dspace.app.rest.submit.factory.impl; import java.io.File; -import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; -import org.dspace.content.Bitstream; -import org.dspace.content.Bundle; +import org.dspace.app.rest.utils.ProxyLicenseUtils; import org.dspace.content.InProgressSubmission; import org.dspace.content.Item; import org.dspace.content.LicenseUtils; -import org.dspace.content.ProxyLicenseUtils; import org.dspace.content.service.BundleService; import org.dspace.content.service.ItemService; -import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.core.service.LicenseService; import org.dspace.eperson.EPerson; @@ -109,21 +105,6 @@ void add(Context context, HttpServletRequest currentRequest, InProgressSubmissio : licenseService.getLicenseText(licensePath); if (StringUtils.isNotBlank(licenseText)) { - if (selected.equalsIgnoreCase("proxy")) { - List licenseBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME); - if (licenseBundles.size() > 0) { - Bundle licenseBundle = licenseBundles.get(0); - for (Bitstream bitstream: licenseBundle.getBitstreams()) { - if (Constants.LICENSE_BITSTREAM_NAME.equals(bitstream.getName())) { - bundleService.removeBitstream(context, licenseBundle, bitstream); - break; - } - } - } - } else { - itemService.removeDSpaceLicense(context, item); - } - ProxyLicenseUtils.addLicense(context, item, selected, licenseText); } else { throw new RuntimeException(String.format("Unable to find license file at %s", licenseFilename)); diff --git a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/step/ProxyLicenseStep.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/step/ProxyLicenseStep.java index ef16d534d88..107cd90bd0d 100644 --- a/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/step/ProxyLicenseStep.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/submit/step/ProxyLicenseStep.java @@ -1,7 +1,5 @@ package org.dspace.app.rest.submit.step; -import java.io.BufferedInputStream; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -19,10 +17,9 @@ import org.dspace.app.rest.submit.UploadableStep; import org.dspace.app.rest.submit.factory.PatchOperationFactory; import org.dspace.app.rest.submit.factory.impl.PatchOperation; -import org.dspace.app.rest.utils.Utils; +import org.dspace.app.rest.utils.ProxyLicenseUtils; import org.dspace.app.util.SubmissionStepConfig; import org.dspace.content.Bitstream; -import org.dspace.content.BitstreamFormat; import org.dspace.content.Bundle; import org.dspace.content.InProgressSubmission; import org.dspace.content.Item; @@ -36,8 +33,6 @@ public class ProxyLicenseStep extends LicenseStep implements UploadableStep { private static final String LICENSE_STEP_SELECTED_OPERATION_ENTRY = "selected"; - private static final String PROXY_LICENSE_NAME = "PERMISSION"; - private static final String DCTERMS_RIGHTSDATE = "dcterms.accessRights"; private static final String DCTERMS_ALTERNATIVE = "dcterms.alternative"; @@ -53,16 +48,16 @@ public DataLicense getData(SubmissionService submissionService, InProgressSubmis DataLicense result = new DataLicense(); - List bundles = itemService.getBundles(obj.getItem(), Constants.LICENSE_BUNDLE_NAME); - Bitstream licenseBitstream = null; Bitstream proxyBitstream = null; - for (Bundle bundle : bundles) { - for (Bitstream bitstream : bundle.getBitstreams()) { + List licenseBundles = itemService.getBundles(obj.getItem(), Constants.LICENSE_BUNDLE_NAME); + + if (licenseBundles.size() > 0) { + for (Bitstream bitstream : licenseBundles.get(0).getBitstreams()) { if (bitstream.getName().equals(Constants.LICENSE_BITSTREAM_NAME)) { licenseBitstream = bitstream; - } else if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) { + } else if (bitstream.getName().startsWith(ProxyLicenseUtils.PROXY_LICENSE_NAME)) { proxyBitstream = bitstream; } } @@ -116,57 +111,14 @@ public ErrorRest upload(Context context, SubmissionService submissionService, Su InProgressSubmission wsi, MultipartFile file) { Item item = wsi.getItem(); - List licenseBundles = null; - Bundle licenseBundle = null; - Bitstream proxyBitstream = null; - - try (InputStream in = new BufferedInputStream(file.getInputStream())) { - - licenseBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME); - - if (licenseBundles.size() > 0) { - licenseBundle = licenseBundles.get(0); - for (Bitstream bitstream: licenseBundle.getBitstreams()) { - if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) { - bundleService.removeBitstream(context, licenseBundle, bitstream); - break; - } - } - } else { - licenseBundle = bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME); - } - - proxyBitstream = bitstreamService.create(context, licenseBundle, in); - - String filename = Utils.getFileName(file); - String[] parts = filename.split("\\."); - String permissionLicenseName = parts.length == 1 - ? String.join(".", PROXY_LICENSE_NAME, "license") - : String.join(".", PROXY_LICENSE_NAME, parts[parts.length - 1]); - - proxyBitstream.setName(context, permissionLicenseName); - - proxyBitstream.setSource(context, file.getOriginalFilename()); - proxyBitstream.setDescription(context, "Proxy license"); - - BitstreamFormat bf = bitstreamFormatService.guessFormat(context, proxyBitstream); - - proxyBitstream.setFormat(context, bf); - - bitstreamService.update(context, proxyBitstream); - + try { + ProxyLicenseUtils.addProxyLicense(context, item, file); } catch (Exception e) { log.error(e.getMessage(), e); ErrorRest result = new ErrorRest(); result.setMessage(e.getMessage()); - if (licenseBundles != null && licenseBundles.size() > 0) { - result.getPaths().add( - "/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId() + "/files/" + - licenseBundles.get(0).getBitstreams().size()); - } else { - result.getPaths() - .add("/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId()); - } + result.getPaths() + .add("/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId()); return result; } diff --git a/dspace/modules/additions/src/main/java/org/dspace/content/ProxyLicenseUtils.java b/dspace/modules/server/src/main/java/org/dspace/app/rest/utils/ProxyLicenseUtils.java similarity index 59% rename from dspace/modules/additions/src/main/java/org/dspace/content/ProxyLicenseUtils.java rename to dspace/modules/server/src/main/java/org/dspace/app/rest/utils/ProxyLicenseUtils.java index a39952683b9..7429c6f896d 100644 --- a/dspace/modules/additions/src/main/java/org/dspace/content/ProxyLicenseUtils.java +++ b/dspace/modules/server/src/main/java/org/dspace/app/rest/utils/ProxyLicenseUtils.java @@ -5,14 +5,21 @@ * * http://www.dspace.org/license/ */ -package org.dspace.content; +package org.dspace.app.rest.utils; +import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.sql.SQLException; import java.util.List; import org.dspace.authorize.AuthorizeException; +import org.dspace.content.Bitstream; +import org.dspace.content.BitstreamFormat; +import org.dspace.content.Bundle; +import org.dspace.content.DCDate; +import org.dspace.content.Item; import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.service.BitstreamFormatService; import org.dspace.content.service.BitstreamService; @@ -20,6 +27,7 @@ import org.dspace.content.service.ItemService; import org.dspace.core.Constants; import org.dspace.core.Context; +import org.springframework.web.multipart.MultipartFile; /** * TAMU Customization - Proxy license utilility for decoupling license selection from accepting licence @@ -31,23 +39,72 @@ public class ProxyLicenseUtils { private static final BundleService bundleService = ContentServiceFactory.getInstance().getBundleService(); private static final ItemService itemService = ContentServiceFactory.getInstance().getItemService(); + public static final String PROXY_LICENSE_NAME = "PERMISSION"; + /** * Default constructor */ private ProxyLicenseUtils() { } + /** + * Store a copy of the proxy license as the user uploaded for the item. + * + * @param context the dspace context + * @param item the item object of the license + * @param file the multipart file upload + * @throws SQLException if database error + * @throws IOException if IO error + * @throws AuthorizeException if authorization error + */ + public static void addProxyLicense(Context context, Item item, MultipartFile file) + throws SQLException, IOException, AuthorizeException { + + InputStream is = new BufferedInputStream(file.getInputStream()); + + List licenseBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME); + Bundle licenseBundle = licenseBundles.size() > 0 + ? licenseBundles.get(0) + : bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME); + + // remove existing proxy license bitstream + for (Bitstream bitstream: licenseBundle.getBitstreams()) { + if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) { + bundleService.removeBitstream(context, licenseBundle, bitstream); + } + } + + Bitstream bitstream = bitstreamService.create(context, licenseBundle, is); + + String filename = Utils.getFileName(file); + String[] parts = filename.split("\\."); + String proxyLicenseName = parts.length == 1 + ? String.join(".", PROXY_LICENSE_NAME, "license") + : String.join(".", PROXY_LICENSE_NAME, parts[parts.length - 1]); + + bitstream.setName(context, proxyLicenseName); + + bitstream.setSource(context, file.getOriginalFilename()); + bitstream.setDescription(context, "Proxy license"); + + BitstreamFormat bf = bitstreamFormatService.guessFormat(context, bitstream); + + bitstream.setFormat(context, bf); + + bitstreamService.update(context, bitstream); + } + /** * Store a copy of the license as the user selected for the item. * * @param context the dspace context * @param item the item object of the license - * @param selection the license the user selected + * @param selected the license the user selected * @param licenseText the license text * @throws SQLException if database error * @throws IOException if IO error * @throws AuthorizeException if authorization error */ - public static void addLicense(Context context, Item item, String selection, String licenseText) + public static void addLicense(Context context, Item item, String selected, String licenseText) throws SQLException, IOException, AuthorizeException { // Store text as a bitstream @@ -59,6 +116,22 @@ public static void addLicense(Context context, Item item, String selection, Stri ? licenseBundles.get(0) : bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME); + // remove existing license bitstream + for (Bitstream bitstream: licenseBundle.getBitstreams()) { + if (Constants.LICENSE_BITSTREAM_NAME.equals(bitstream.getName())) { + bundleService.removeBitstream(context, licenseBundle, bitstream); + } + } + + // if proxy license not selected, remove proxy license + if (!selected.equalsIgnoreCase("proxy")) { + for (Bitstream bitstream: licenseBundle.getBitstreams()) { + if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) { + bundleService.removeBitstream(context, licenseBundle, bitstream); + } + } + } + Bitstream bitstream = bitstreamService.create(context, licenseBundle, is); // Now set the format and name of the bitstream @@ -68,10 +141,10 @@ public static void addLicense(Context context, Item item, String selection, Stri // Find the License format BitstreamFormat bf = bitstreamFormatService.findByShortDescription(context, "License"); - bitstream.setFormat(bf); + bitstream.setFormat(context, bf); bitstreamService - .setMetadataSingleValue(context, bitstream, "dcterms", "alternative", null, null, selection); + .setMetadataSingleValue(context, bitstream, "dcterms", "alternative", null, null, selected); bitstreamService.update(context, bitstream); }