Fixed feedback

This commit is contained in:
Raf Ponsaerts
2019-08-16 10:10:49 +02:00
parent fcac5ecb56
commit 62596b1eaa
4 changed files with 34 additions and 76 deletions

View File

@@ -20,7 +20,7 @@ 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.BitstreamPropertiesRest;
import org.dspace.app.rest.model.BitstreamRest;
import org.dspace.app.rest.model.hateoas.BitstreamResource;
import org.dspace.app.rest.utils.ContextUtil;
import org.dspace.app.rest.utils.Utils;
@@ -66,6 +66,12 @@ public class ItemUploadController {
@Autowired
private BitstreamFormatService bitstreamFormatService;
/**
* Method to upload a Bitstream to an Item with the given UUID in the URL. This will create a Bitstream with the
* file provided in the request and attach this to the Item that matches the UUID in the URL.
* This will only work for uploading one file, any extra files will silently be ignored
* @return The created BitstreamResource
*/
@RequestMapping(method = RequestMethod.POST, value = "/bitstreams", headers = "content-type=multipart/form-data")
@PreAuthorize("hasPermission(#uuid, 'ITEM', 'WRITE') && hasPermission(#uuid, 'ITEM', 'ADD')")
public BitstreamResource uploadBitstream(HttpServletRequest request, @PathVariable UUID uuid,
@@ -113,27 +119,27 @@ public class ItemUploadController {
Bitstream bitstream = null;
if (StringUtils.isNotBlank(properties)) {
ObjectMapper mapper = new ObjectMapper();
BitstreamPropertiesRest bitstreamPropertiesRest = null;
BitstreamRest bitstreamRest = null;
try {
bitstreamPropertiesRest = mapper.readValue(properties, BitstreamPropertiesRest.class);
bitstreamRest = mapper.readValue(properties, BitstreamRest.class);
} catch (Exception e) {
throw new UnprocessableEntityException("The properties parameter was incorrect: " + properties);
}
String bundleName = bitstreamPropertiesRest.getBundleName();
String bundleName = bitstreamRest.getBundleName();
if (StringUtils.isBlank(bundleName)) {
throw new UnprocessableEntityException("Properties without a bundleName is not allowed");
}
bitstream = itemService.createSingleBitstream(context, fileInputStream, item, bundleName);
if (bitstreamPropertiesRest.getMetadata() != null) {
metadataConverter.setMetadata(context, bitstream, bitstreamPropertiesRest.getMetadata());
if (bitstreamRest.getMetadata() != null) {
metadataConverter.setMetadata(context, bitstream, bitstreamRest.getMetadata());
}
String name = bitstreamPropertiesRest.getName();
String name = bitstreamRest.getName();
if (StringUtils.isNotBlank(name)) {
bitstream.setName(context, name);
} else {
bitstream.setName(context, originalFilename);
}
Integer sequenceId = bitstreamPropertiesRest.getSequenceId();
Integer sequenceId = bitstreamRest.getSequenceId();
if (sequenceId != null) {
try {
bitstreamService.setSequenceId(context, bitstream, item, sequenceId);

View File

@@ -1,48 +0,0 @@
/**
* 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.model;
public class BitstreamPropertiesRest {
private String name;
private String bundleName;
private Integer sequenceId;
private MetadataRest metadata;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBundleName() {
return bundleName;
}
public void setBundleName(String bundleName) {
this.bundleName = bundleName;
}
public MetadataRest getMetadata() {
return metadata;
}
public void setMetadata(MetadataRest metadata) {
this.metadata = metadata;
}
public Integer getSequenceId() {
return sequenceId;
}
public void setSequenceId(Integer sequenceId) {
this.sequenceId = sequenceId;
}
}

View File

@@ -76,6 +76,7 @@ public class BitstreamRest extends DSpaceObjectRest {
}
@Override
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public String getType() {
return NAME;
}

View File

@@ -23,7 +23,7 @@ import org.dspace.app.rest.builder.CommunityBuilder;
import org.dspace.app.rest.builder.ItemBuilder;
import org.dspace.app.rest.matcher.BitstreamMatcher;
import org.dspace.app.rest.matcher.MetadataMatcher;
import org.dspace.app.rest.model.BitstreamPropertiesRest;
import org.dspace.app.rest.model.BitstreamRest;
import org.dspace.app.rest.model.MetadataRest;
import org.dspace.app.rest.model.MetadataValueRest;
import org.dspace.app.rest.test.AbstractEntityIntegrationTest;
@@ -73,10 +73,10 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
input.getBytes());
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
bitstreamPropertiesRest.setBundleName("TESTINGBUNDLE");
bitstreamPropertiesRest.setName("testing");
bitstreamPropertiesRest.setSequenceId(123456);
BitstreamRest bitstreamRest = new BitstreamRest();
bitstreamRest.setBundleName("TESTINGBUNDLE");
bitstreamRest.setName("testing");
bitstreamRest.setSequenceId(123456);
MetadataRest metadataRest = new MetadataRest();
@@ -96,7 +96,7 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
title.setValue("Title Text");
metadataRest.put("dc.title", title);
bitstreamPropertiesRest.setMetadata(metadataRest);
bitstreamRest.setMetadata(metadataRest);
ObjectMapper mapper = new ObjectMapper();
context.restoreAuthSystemState();
@@ -104,7 +104,7 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
.file(file)
.param("properties", mapper
.writeValueAsString(bitstreamPropertiesRest)))
.writeValueAsString(bitstreamRest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("testing")))
.andExpect(jsonPath("$.bundleName", is("TESTINGBUNDLE")))
@@ -335,9 +335,9 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
input.getBytes());
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
BitstreamRest bitstreamRest = new BitstreamRest();
String testbundle = "TESTBUNDLE";
bitstreamPropertiesRest.setBundleName(testbundle);
bitstreamRest.setBundleName(testbundle);
ObjectMapper mapper = new ObjectMapper();
@@ -347,7 +347,7 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
.perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
.file(file)
.param("properties", mapper
.writeValueAsString(bitstreamPropertiesRest)))
.writeValueAsString(bitstreamRest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.bundleName", is(testbundle)))
.andExpect(jsonPath("$.uuid", notNullValue())).andReturn();
@@ -430,7 +430,7 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
input.getBytes());
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
BitstreamRest bitstreamRest = new BitstreamRest();
ObjectMapper mapper = new ObjectMapper();
@@ -439,7 +439,7 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
.file(file)
.param("properties", mapper
.writeValueAsString(bitstreamPropertiesRest)))
.writeValueAsString(bitstreamRest)))
.andExpect(status().isUnprocessableEntity());
getClient(token).perform(get("/api/core/items/" + item.getID() + "/bitstreams"))
@@ -448,7 +448,6 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
}
//TODO This check on sequence ID doesn't happen
@Test
public void uploadBitstreamSameSequenceIdTwiceUnprocessableEntityException() throws Exception {
context.turnOffAuthorisationSystem();
@@ -476,10 +475,10 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
input.getBytes());
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
bitstreamPropertiesRest.setBundleName("ORIGINAL");
bitstreamPropertiesRest.setName("testing");
bitstreamPropertiesRest.setSequenceId(123456);
BitstreamRest bitstreamRest = new BitstreamRest();
bitstreamRest.setBundleName("ORIGINAL");
bitstreamRest.setName("testing");
bitstreamRest.setSequenceId(123456);
MetadataRest metadataRest = new MetadataRest();
@@ -499,14 +498,14 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
title.setValue("Title Text");
metadataRest.put("dc.title", title);
bitstreamPropertiesRest.setMetadata(metadataRest);
bitstreamRest.setMetadata(metadataRest);
ObjectMapper mapper = new ObjectMapper();
context.restoreAuthSystemState();
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
.file(file)
.param("properties", mapper
.writeValueAsString(bitstreamPropertiesRest)))
.writeValueAsString(bitstreamRest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is("testing")))
.andExpect(jsonPath("$.bundleName", is("ORIGINAL")))
@@ -526,7 +525,7 @@ public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
.file(file)
.param("properties", mapper
.writeValueAsString(bitstreamPropertiesRest)))
.writeValueAsString(bitstreamRest)))
.andExpect(status().isUnprocessableEntity());
getClient(token).perform(get("/api/core/items/" + item.getID() + "/bitstreams"))