mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
65188: Implement feedback
This commit is contained in:
@@ -8,26 +8,22 @@
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static org.dspace.app.rest.utils.RegexUtils.REGEX_REQUESTMAPPING_IDENTIFIER_AS_UUID;
|
||||
import static org.dspace.core.Constants.ADD;
|
||||
import static org.dspace.core.Constants.BUNDLE;
|
||||
import static org.dspace.core.Constants.REMOVE;
|
||||
import static org.dspace.core.Constants.WRITE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.dspace.app.rest.converter.DSpaceObjectConverter;
|
||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.link.HalLinkService;
|
||||
import org.dspace.app.rest.model.BitstreamRest;
|
||||
import org.dspace.app.rest.model.BundleRest;
|
||||
import org.dspace.app.rest.model.hateoas.BundleResource;
|
||||
import org.dspace.app.rest.repository.BitstreamRestRepository;
|
||||
import org.dspace.app.rest.utils.ContextUtil;
|
||||
import org.dspace.app.rest.utils.Utils;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
@@ -35,7 +31,6 @@ import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.DSpaceObject;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.service.BitstreamService;
|
||||
import org.dspace.content.service.BundleService;
|
||||
import org.dspace.core.Context;
|
||||
@@ -69,6 +64,9 @@ public class BitstreamController {
|
||||
@Autowired
|
||||
AuthorizeService authorizeService;
|
||||
|
||||
@Autowired
|
||||
BitstreamRestRepository bitstreamRestRepository;
|
||||
|
||||
@Autowired
|
||||
DSpaceObjectConverter<Bundle, BundleRest> dsoConverter;
|
||||
|
||||
@@ -142,7 +140,7 @@ public class BitstreamController {
|
||||
"or the data cannot be resolved to a bundle.");
|
||||
}
|
||||
|
||||
Bundle targetBundle = performBitstreamMove(context, uuid, (Bundle) dsoList.get(0));
|
||||
Bundle targetBundle = bitstreamRestRepository.performBitstreamMove(context, uuid, (Bundle) dsoList.get(0));
|
||||
|
||||
if (targetBundle == null) {
|
||||
return null;
|
||||
@@ -156,58 +154,4 @@ public class BitstreamController {
|
||||
|
||||
}
|
||||
|
||||
private Bundle performBitstreamMove(final Context context, final UUID uuid, final Bundle targetBundle)
|
||||
throws SQLException, IOException, AuthorizeException {
|
||||
|
||||
Bitstream bitstream = bitstreamService.find(context, uuid);
|
||||
|
||||
|
||||
if (bitstream == null) {
|
||||
throw new ResourceNotFoundException("Bitstream with id: " + uuid + " not found");
|
||||
}
|
||||
if (bitstream.getBundles().contains(targetBundle)) {
|
||||
throw new DSpaceBadRequestException("The provided bitstream is already in the target bundle");
|
||||
}
|
||||
|
||||
List<Bundle> bundles = new LinkedList<>();
|
||||
bundles.addAll(bitstream.getBundles());
|
||||
|
||||
if (hasSufficientPermissions(context, bundles, targetBundle)) {
|
||||
bundleService.addBitstream(context, targetBundle, bitstream);
|
||||
bundleService.update(context, targetBundle);
|
||||
for (Bundle bundle : bundles) {
|
||||
bundleService.removeBitstream(context, bundle, bitstream);
|
||||
bundleService.update(context, bundle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return targetBundle;
|
||||
}
|
||||
|
||||
private boolean hasSufficientPermissions(final Context context, final List<Bundle> bundles,
|
||||
final Bundle targetBundle) throws SQLException, AuthorizeException {
|
||||
for (Bundle bundle : bundles) {
|
||||
if (!authorizeService.authorizeActionBoolean(context, bundle, WRITE) || !authorizeService
|
||||
.authorizeActionBoolean(context, bundle, REMOVE)) {
|
||||
throw new AuthorizeException(
|
||||
"The current user does not have WRITE and REMOVE access to the current bundle: " + bundle
|
||||
.getID());
|
||||
}
|
||||
}
|
||||
if (!authorizeService.authorizeActionBoolean(context, targetBundle, WRITE) || !authorizeService
|
||||
.authorizeActionBoolean(context, targetBundle, ADD)) {
|
||||
throw new AuthorizeException(
|
||||
"The current user does not have WRITE and ADD access to the target bundle: " + targetBundle
|
||||
.getID());
|
||||
}
|
||||
for (Item item : targetBundle.getItems()) {
|
||||
if (!authorizeService.authorizeActionBoolean(context, item, WRITE)) {
|
||||
throw new AuthorizeException(
|
||||
"The current user does not have WRITE access to the target bundle's item: " + item.getID());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@@ -10,32 +10,20 @@ package org.dspace.app.rest;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.dspace.app.rest.converter.BitstreamConverter;
|
||||
import org.dspace.app.rest.converter.MetadataConverter;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.BitstreamRest;
|
||||
import org.dspace.app.rest.model.hateoas.BitstreamResource;
|
||||
import org.dspace.app.rest.repository.BundleRestRepository;
|
||||
import org.dspace.app.rest.utils.ContextUtil;
|
||||
import org.dspace.app.rest.utils.Utils;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.BitstreamFormat;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.service.BitstreamFormatService;
|
||||
import org.dspace.content.service.BitstreamService;
|
||||
import org.dspace.content.service.BundleService;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.rest.webmvc.ControllerUtils;
|
||||
@@ -43,7 +31,6 @@ import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -61,26 +48,14 @@ public class BundleController {
|
||||
@Autowired
|
||||
protected Utils utils;
|
||||
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
|
||||
@Autowired
|
||||
private BundleService bundleService;
|
||||
|
||||
@Autowired
|
||||
private BitstreamService bitstreamService;
|
||||
|
||||
@Autowired
|
||||
private BitstreamConverter bitstreamConverter;
|
||||
|
||||
@Autowired
|
||||
private MetadataConverter metadataConverter;
|
||||
|
||||
@Autowired
|
||||
private BitstreamFormatService bitstreamFormatService;
|
||||
|
||||
@Autowired
|
||||
private AuthorizeService authorizeService;
|
||||
private BundleRestRepository bundleRestRepository;
|
||||
|
||||
/**
|
||||
* Method to upload a Bitstream to a Bundle with the given UUID in the URL. This will create a Bitstream with the
|
||||
@@ -97,7 +72,6 @@ public class BundleController {
|
||||
|
||||
Context context = ContextUtil.obtainContext(request);
|
||||
Bundle bundle = null;
|
||||
Item item = null;
|
||||
Bitstream bitstream = null;
|
||||
try {
|
||||
bundle = bundleService.find(context, uuid);
|
||||
@@ -115,76 +89,11 @@ public class BundleController {
|
||||
e);
|
||||
throw new UnprocessableEntityException("The InputStream from the file couldn't be read", e);
|
||||
}
|
||||
try {
|
||||
List<Item> items = bundle.getItems();
|
||||
if (!items.isEmpty()) {
|
||||
item = items.get(0);
|
||||
}
|
||||
if (item != null && !(authorizeService.authorizeActionBoolean(context, item, Constants.WRITE)
|
||||
&& authorizeService.authorizeActionBoolean(context, item, Constants.ADD))) {
|
||||
throw new AccessDeniedException("You do not have write rights to update the Bundle's item");
|
||||
}
|
||||
bitstream = processBitstreamCreation(context, bundle, fileInputStream, properties,
|
||||
uploadfile.getOriginalFilename());
|
||||
if (item != null) {
|
||||
itemService.update(context, item);
|
||||
}
|
||||
bundleService.update(context, bundle);
|
||||
context.commit();
|
||||
} catch (AuthorizeException | IOException | SQLException e) {
|
||||
String message = "Something went wrong with trying to create the single bitstream for file with filename: "
|
||||
+ uploadfile.getOriginalFilename()
|
||||
+ " for item with uuid: " + uuid + " and possible properties: " + properties;
|
||||
log.error(message, e);
|
||||
throw new RuntimeException(message, e);
|
||||
}
|
||||
|
||||
bitstream = bundleRestRepository.uploadBitstream(context, bundle, uploadfile.getOriginalFilename(),
|
||||
fileInputStream, properties);
|
||||
|
||||
BitstreamResource bitstreamResource = new BitstreamResource(bitstreamConverter.fromModel(bitstream), utils);
|
||||
return ControllerUtils.toResponseEntity(HttpStatus.CREATED, null, bitstreamResource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the bitstream based on the given parameters
|
||||
*
|
||||
* @param context The context
|
||||
* @param bundle The bundle where the bitstream should be stored
|
||||
* @param fileInputStream The input stream used to create the bitstream
|
||||
* @param properties The properties to be assigned to the bitstream
|
||||
* @param originalFilename The filename as it was uploaded
|
||||
* @return The bitstream which has been created
|
||||
*/
|
||||
private Bitstream processBitstreamCreation(Context context, Bundle bundle, InputStream fileInputStream,
|
||||
String properties, String originalFilename)
|
||||
throws AuthorizeException, IOException, SQLException {
|
||||
|
||||
Bitstream bitstream = null;
|
||||
if (StringUtils.isNotBlank(properties)) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
BitstreamRest bitstreamRest = null;
|
||||
try {
|
||||
bitstreamRest = mapper.readValue(properties, BitstreamRest.class);
|
||||
} catch (Exception e) {
|
||||
throw new UnprocessableEntityException("The properties parameter was incorrect: " + properties);
|
||||
}
|
||||
bitstream = bitstreamService.create(context, bundle, fileInputStream);
|
||||
if (bitstreamRest.getMetadata() != null) {
|
||||
metadataConverter.setMetadata(context, bitstream, bitstreamRest.getMetadata());
|
||||
}
|
||||
String name = bitstreamRest.getName();
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
bitstream.setName(context, name);
|
||||
} else {
|
||||
bitstream.setName(context, originalFilename);
|
||||
}
|
||||
|
||||
} else {
|
||||
bitstream = bitstreamService.create(context, bundle, fileInputStream);
|
||||
bitstream.setName(context, originalFilename);
|
||||
|
||||
}
|
||||
BitstreamFormat bitstreamFormat = bitstreamFormatService.guessFormat(context, bitstream);
|
||||
bitstreamService.setFormat(context, bitstream, bitstreamFormat);
|
||||
bitstreamService.update(context, bitstream);
|
||||
|
||||
return bitstream;
|
||||
}
|
||||
}
|
||||
|
@@ -18,16 +18,15 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.dspace.app.rest.converter.BundleConverter;
|
||||
import org.dspace.app.rest.converter.MetadataConverter;
|
||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.BundleRest;
|
||||
import org.dspace.app.rest.model.hateoas.BundleResource;
|
||||
import org.dspace.app.rest.repository.ItemRestRepository;
|
||||
import org.dspace.app.rest.utils.ContextUtil;
|
||||
import org.dspace.app.rest.utils.Utils;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.service.BundleService;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -50,7 +49,7 @@ public class ItemController {
|
||||
ItemService itemService;
|
||||
|
||||
@Autowired
|
||||
BundleService bundleService;
|
||||
ItemRestRepository itemRestRepository;
|
||||
|
||||
@Autowired
|
||||
BundleConverter converter;
|
||||
@@ -58,6 +57,7 @@ public class ItemController {
|
||||
@Autowired
|
||||
MetadataConverter metadataConverter;
|
||||
|
||||
|
||||
@Autowired
|
||||
Utils utils;
|
||||
|
||||
@@ -82,16 +82,7 @@ public class ItemController {
|
||||
throw new UnprocessableEntityException("Could not parse request body");
|
||||
}
|
||||
|
||||
if (item.getBundles(bundleRest.getName()).size() > 0) {
|
||||
throw new DSpaceBadRequestException("The bundle name already exists in the item");
|
||||
}
|
||||
|
||||
Bundle bundle = bundleService.create(context, item, bundleRest.getName());
|
||||
|
||||
metadataConverter.setMetadata(context, bundle, bundleRest.getMetadata());
|
||||
bundle.setName(context, bundleRest.getName());
|
||||
|
||||
context.commit();
|
||||
Bundle bundle = itemRestRepository.addBundleToItem(context, item, bundleRest);
|
||||
|
||||
BundleResource bundleResource = new BundleResource(converter.convert(bundle), utils);
|
||||
return ControllerUtils.toResponseEntity(HttpStatus.CREATED, null, bundleResource);
|
||||
|
@@ -7,24 +7,34 @@
|
||||
*/
|
||||
package org.dspace.app.rest.repository;
|
||||
|
||||
import static org.dspace.core.Constants.ADD;
|
||||
import static org.dspace.core.Constants.REMOVE;
|
||||
import static org.dspace.core.Constants.WRITE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.dspace.app.rest.converter.BitstreamConverter;
|
||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.BitstreamRest;
|
||||
import org.dspace.app.rest.model.hateoas.BitstreamResource;
|
||||
import org.dspace.app.rest.model.patch.Patch;
|
||||
import org.dspace.app.rest.repository.patch.DSpaceObjectPatch;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.service.BitstreamService;
|
||||
import org.dspace.content.service.BundleService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -46,6 +56,12 @@ public class BitstreamRestRepository extends DSpaceObjectRestRepository<Bitstrea
|
||||
|
||||
private final BitstreamService bs;
|
||||
|
||||
@Autowired
|
||||
BundleService bundleService;
|
||||
|
||||
@Autowired
|
||||
AuthorizeService authorizeService;
|
||||
|
||||
@Autowired
|
||||
public BitstreamRestRepository(BitstreamService dsoService,
|
||||
BitstreamConverter dsoConverter) {
|
||||
@@ -149,4 +165,58 @@ public class BitstreamRestRepository extends DSpaceObjectRestRepository<Bitstrea
|
||||
context.abort();
|
||||
return is;
|
||||
}
|
||||
|
||||
public Bundle performBitstreamMove(final Context context, final UUID uuid, final Bundle targetBundle)
|
||||
throws SQLException, IOException, AuthorizeException {
|
||||
|
||||
Bitstream bitstream = bs.find(context, uuid);
|
||||
|
||||
|
||||
if (bitstream == null) {
|
||||
throw new ResourceNotFoundException("Bitstream with id: " + uuid + " not found");
|
||||
}
|
||||
if (bitstream.getBundles().contains(targetBundle)) {
|
||||
throw new DSpaceBadRequestException("The provided bitstream is already in the target bundle");
|
||||
}
|
||||
|
||||
List<Bundle> bundles = new LinkedList<>();
|
||||
bundles.addAll(bitstream.getBundles());
|
||||
|
||||
if (hasSufficientMovePermissions(context, bundles, targetBundle)) {
|
||||
bundleService.addBitstream(context, targetBundle, bitstream);
|
||||
bundleService.update(context, targetBundle);
|
||||
for (Bundle bundle : bundles) {
|
||||
bundleService.removeBitstream(context, bundle, bitstream);
|
||||
bundleService.update(context, bundle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return targetBundle;
|
||||
}
|
||||
|
||||
private boolean hasSufficientMovePermissions(final Context context, final List<Bundle> bundles,
|
||||
final Bundle targetBundle) throws SQLException, AuthorizeException {
|
||||
for (Bundle bundle : bundles) {
|
||||
if (!authorizeService.authorizeActionBoolean(context, bundle, WRITE) || !authorizeService
|
||||
.authorizeActionBoolean(context, bundle, REMOVE)) {
|
||||
throw new AuthorizeException(
|
||||
"The current user does not have WRITE and REMOVE access to the current bundle: " + bundle
|
||||
.getID());
|
||||
}
|
||||
}
|
||||
if (!authorizeService.authorizeActionBoolean(context, targetBundle, WRITE) || !authorizeService
|
||||
.authorizeActionBoolean(context, targetBundle, ADD)) {
|
||||
throw new AuthorizeException(
|
||||
"The current user does not have WRITE and ADD access to the target bundle: " + targetBundle
|
||||
.getID());
|
||||
}
|
||||
for (Item item : targetBundle.getItems()) {
|
||||
if (!authorizeService.authorizeActionBoolean(context, item, WRITE)) {
|
||||
throw new AuthorizeException(
|
||||
"The current user does not have WRITE access to the target bundle's item: " + item.getID());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -7,24 +7,41 @@
|
||||
*/
|
||||
package org.dspace.app.rest.repository;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.dspace.app.rest.converter.BundleConverter;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.BitstreamRest;
|
||||
import org.dspace.app.rest.model.BundleRest;
|
||||
import org.dspace.app.rest.model.hateoas.BundleResource;
|
||||
import org.dspace.app.rest.model.hateoas.DSpaceResource;
|
||||
import org.dspace.app.rest.model.patch.Patch;
|
||||
import org.dspace.app.rest.repository.patch.BundlePatch;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.BitstreamFormat;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.service.BitstreamFormatService;
|
||||
import org.dspace.content.service.BitstreamService;
|
||||
import org.dspace.content.service.BundleService;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -37,12 +54,28 @@ import org.springframework.stereotype.Component;
|
||||
@Component(BundleRest.CATEGORY + "." + BundleRest.NAME)
|
||||
public class BundleRestRepository extends DSpaceObjectRestRepository<Bundle, BundleRest> {
|
||||
|
||||
private static final Logger log = LogManager.getLogger();
|
||||
|
||||
|
||||
@Autowired
|
||||
private BundleService bundleService;
|
||||
|
||||
@Autowired
|
||||
private BundlePatch bundlePatch;
|
||||
|
||||
@Autowired
|
||||
private AuthorizeService authorizeService;
|
||||
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
|
||||
@Autowired
|
||||
private BitstreamService bitstreamService;
|
||||
|
||||
@Autowired
|
||||
private BitstreamFormatService bitstreamFormatService;
|
||||
|
||||
|
||||
public BundleRestRepository(BundleService dsoService,
|
||||
BundleConverter dsoConverter,
|
||||
BundlePatch dsoPatch) {
|
||||
@@ -75,6 +108,83 @@ public class BundleRestRepository extends DSpaceObjectRestRepository<Bundle, Bun
|
||||
patchDSpaceObject(apiCategory, model, uuid, patch);
|
||||
}
|
||||
|
||||
public Bitstream uploadBitstream(Context context, Bundle bundle, String fileName, InputStream fileInputStream,
|
||||
String properties) {
|
||||
Item item = null;
|
||||
Bitstream bitstream = null;
|
||||
try {
|
||||
List<Item> items = bundle.getItems();
|
||||
if (!items.isEmpty()) {
|
||||
item = items.get(0);
|
||||
}
|
||||
if (item != null && !(authorizeService.authorizeActionBoolean(context, item, Constants.WRITE)
|
||||
&& authorizeService.authorizeActionBoolean(context, item, Constants.ADD))) {
|
||||
throw new AccessDeniedException("You do not have write rights to update the Bundle's item");
|
||||
}
|
||||
bitstream = processBitstreamCreation(context, bundle, fileInputStream, properties,
|
||||
fileName);
|
||||
if (item != null) {
|
||||
itemService.update(context, item);
|
||||
}
|
||||
bundleService.update(context, bundle);
|
||||
context.commit();
|
||||
} catch (AuthorizeException | IOException | SQLException e) {
|
||||
String message = "Something went wrong with trying to create the single bitstream for file with filename: "
|
||||
+ fileName
|
||||
+ " for item with uuid: " + bundle.getID() + " and possible properties: " + properties;
|
||||
log.error(message, e);
|
||||
throw new RuntimeException(message, e);
|
||||
}
|
||||
|
||||
return bitstream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the bitstream based on the given parameters
|
||||
*
|
||||
* @param context The context
|
||||
* @param bundle The bundle where the bitstream should be stored
|
||||
* @param fileInputStream The input stream used to create the bitstream
|
||||
* @param properties The properties to be assigned to the bitstream
|
||||
* @param originalFilename The filename as it was uploaded
|
||||
* @return The bitstream which has been created
|
||||
*/
|
||||
private Bitstream processBitstreamCreation(Context context, Bundle bundle, InputStream fileInputStream,
|
||||
String properties, String originalFilename)
|
||||
throws AuthorizeException, IOException, SQLException {
|
||||
|
||||
Bitstream bitstream = null;
|
||||
if (StringUtils.isNotBlank(properties)) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
BitstreamRest bitstreamRest = null;
|
||||
try {
|
||||
bitstreamRest = mapper.readValue(properties, BitstreamRest.class);
|
||||
} catch (Exception e) {
|
||||
throw new UnprocessableEntityException("The properties parameter was incorrect: " + properties);
|
||||
}
|
||||
bitstream = bitstreamService.create(context, bundle, fileInputStream);
|
||||
if (bitstreamRest.getMetadata() != null) {
|
||||
metadataConverter.setMetadata(context, bitstream, bitstreamRest.getMetadata());
|
||||
}
|
||||
String name = bitstreamRest.getName();
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
bitstream.setName(context, name);
|
||||
} else {
|
||||
bitstream.setName(context, originalFilename);
|
||||
}
|
||||
|
||||
} else {
|
||||
bitstream = bitstreamService.create(context, bundle, fileInputStream);
|
||||
bitstream.setName(context, originalFilename);
|
||||
|
||||
}
|
||||
BitstreamFormat bitstreamFormat = bitstreamFormatService.guessFormat(context, bitstream);
|
||||
bitstreamService.setFormat(context, bitstream, bitstreamFormat);
|
||||
bitstreamService.update(context, bitstream);
|
||||
|
||||
return bitstream;
|
||||
}
|
||||
|
||||
public Class<BundleRest> getDomainClass() {
|
||||
return BundleRest.class;
|
||||
}
|
||||
|
@@ -25,14 +25,17 @@ import org.dspace.app.rest.converter.MetadataConverter;
|
||||
import org.dspace.app.rest.exception.DSpaceBadRequestException;
|
||||
import org.dspace.app.rest.exception.RepositoryMethodNotImplementedException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.BundleRest;
|
||||
import org.dspace.app.rest.model.ItemRest;
|
||||
import org.dspace.app.rest.model.hateoas.ItemResource;
|
||||
import org.dspace.app.rest.model.patch.Patch;
|
||||
import org.dspace.app.rest.repository.patch.ItemPatch;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Collection;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.WorkspaceItem;
|
||||
import org.dspace.content.service.BundleService;
|
||||
import org.dspace.content.service.CollectionService;
|
||||
import org.dspace.content.service.InstallItemService;
|
||||
import org.dspace.content.service.ItemService;
|
||||
@@ -66,6 +69,9 @@ public class ItemRestRepository extends DSpaceObjectRestRepository<Item, ItemRes
|
||||
@Autowired
|
||||
ItemPatch itemPatch;
|
||||
|
||||
@Autowired
|
||||
BundleService bundleService;
|
||||
|
||||
@Autowired
|
||||
WorkspaceItemService workspaceItemService;
|
||||
|
||||
@@ -129,7 +135,7 @@ public class ItemRestRepository extends DSpaceObjectRestRepository<Item, ItemRes
|
||||
|
||||
@Override
|
||||
protected void updateDSpaceObject(Item item, ItemRest itemRest)
|
||||
throws AuthorizeException, SQLException {
|
||||
throws AuthorizeException, SQLException {
|
||||
super.updateDSpaceObject(item, itemRest);
|
||||
|
||||
Context context = obtainContext();
|
||||
@@ -164,15 +170,15 @@ public class ItemRestRepository extends DSpaceObjectRestRepository<Item, ItemRes
|
||||
item = is.find(context, id);
|
||||
if (item == null) {
|
||||
throw new ResourceNotFoundException(ItemRest.CATEGORY + "." + ItemRest.NAME +
|
||||
" with id: " + id + " not found");
|
||||
" with id: " + id + " not found");
|
||||
}
|
||||
if (is.isInProgressSubmission(context, item)) {
|
||||
throw new UnprocessableEntityException("The item cannot be deleted. "
|
||||
+ "It's part of a in-progress submission.");
|
||||
+ "It's part of a in-progress submission.");
|
||||
}
|
||||
if (item.getTemplateItemOf() != null) {
|
||||
throw new UnprocessableEntityException("The item cannot be deleted. "
|
||||
+ "It's a template for a collection");
|
||||
+ "It's a template for a collection");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
@@ -205,7 +211,7 @@ public class ItemRestRepository extends DSpaceObjectRestRepository<Item, ItemRes
|
||||
Collection collection = collectionService.find(context, owningCollectionUuid);
|
||||
if (collection == null) {
|
||||
throw new DSpaceBadRequestException("The given owningCollection parameter is invalid: "
|
||||
+ owningCollectionUuid);
|
||||
+ owningCollectionUuid);
|
||||
}
|
||||
WorkspaceItem workspaceItem = workspaceItemService.create(context, collection, false);
|
||||
Item item = workspaceItem.getItem();
|
||||
@@ -243,9 +249,26 @@ public class ItemRestRepository extends DSpaceObjectRestRepository<Item, ItemRes
|
||||
metadataConverter.setMetadata(context, item, itemRest.getMetadata());
|
||||
} else {
|
||||
throw new IllegalArgumentException("The UUID in the Json and the UUID in the url do not match: "
|
||||
+ uuid + ", "
|
||||
+ itemRest.getId());
|
||||
+ uuid + ", "
|
||||
+ itemRest.getId());
|
||||
}
|
||||
return dsoConverter.fromModel(item);
|
||||
}
|
||||
|
||||
public Bundle addBundleToItem(Context context, Item item, BundleRest bundleRest)
|
||||
throws SQLException, AuthorizeException {
|
||||
if (item.getBundles(bundleRest.getName()).size() > 0) {
|
||||
throw new DSpaceBadRequestException("The bundle name already exists in the item");
|
||||
}
|
||||
|
||||
Bundle bundle = bundleService.create(context, item, bundleRest.getName());
|
||||
|
||||
metadataConverter.setMetadata(context, bundle, bundleRest.getMetadata());
|
||||
bundle.setName(context, bundleRest.getName());
|
||||
|
||||
context.commit();
|
||||
|
||||
return bundle;
|
||||
|
||||
}
|
||||
}
|
@@ -127,10 +127,12 @@ public class BundleControllerIT extends AbstractEntityIntegrationTest {
|
||||
Map<String, Object> map = mapper.readValue(content, Map.class);
|
||||
String bitstreamId = String.valueOf(map.get("id"));
|
||||
|
||||
|
||||
getClient(token).perform(get("/api/core/bundles/" + bundle.getID() + "/bitstreams"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("_embedded.bitstreams", Matchers.hasItem(
|
||||
BitstreamMatcher.matchBitstreamEntry(UUID.fromString(bitstreamId), file.getSize()))));
|
||||
BitstreamMatcher.matchBitstreamEntry(UUID.fromString(bitstreamId), file.getSize(),
|
||||
bitstreamRest.getName(), "description"))));
|
||||
|
||||
}
|
||||
|
||||
|
@@ -42,6 +42,24 @@ public class BitstreamMatcher {
|
||||
);
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchBitstreamEntry(UUID uuid, long size, String name, String description) {
|
||||
return allOf(
|
||||
//Check ID and size
|
||||
hasJsonPath("$.uuid", is(uuid.toString())),
|
||||
hasJsonPath("$.sizeBytes", is((int) size)),
|
||||
hasJsonPath("$.metadata", allOf(
|
||||
matchMetadata("dc.title", name),
|
||||
matchMetadata("dc.description", description)
|
||||
)),
|
||||
//Make sure we have a checksum
|
||||
hasJsonPath("$.checkSum", matchChecksum()),
|
||||
//Make sure we have a valid format
|
||||
hasJsonPath("$._embedded.format", matchFormat()),
|
||||
//Check links
|
||||
matchBitstreamLinks(uuid)
|
||||
);
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchBitstreamEntry(UUID uuid, long size) {
|
||||
return allOf(
|
||||
//Check ID and size
|
||||
|
Reference in New Issue
Block a user