DS-3533 Make bitstream subresource embeds optional

This commit is contained in:
Chris Wilper
2019-12-11 11:27:59 -05:00
parent 2884a80958
commit f98e011738
3 changed files with 62 additions and 18 deletions

View File

@@ -48,11 +48,6 @@ public class BitstreamConverter extends DSpaceObjectConverter<Bitstream, Bitstre
checksum.setCheckSumAlgorithm(obj.getChecksumAlgorithm());
checksum.setValue(obj.getChecksum());
b.setCheckSum(checksum);
try {
b.setFormat(converter.toRest(obj.getFormat(null), projection));
} catch (SQLException e) {
throw new RuntimeException(e);
}
b.setSizeBytes(obj.getSizeBytes());
return b;
}

View File

@@ -15,17 +15,23 @@ import com.fasterxml.jackson.annotation.JsonProperty.Access;
*
* @author Andrea Bollini (andrea.bollini at 4science.it)
*/
@LinksRest(links = {
@LinkRest(
name = BitstreamRest.FORMAT,
linkClass = BitstreamFormatRest.class,
method = "getFormat",
embedOptional = true
)
})
public class BitstreamRest extends DSpaceObjectRest {
public static final String PLURAL_NAME = "bitstreams";
public static final String NAME = "bitstream";
public static final String CATEGORY = RestAddressableModel.CORE;
public static final String FORMAT = "format";
private String bundleName;
// avoid to serialize this object inline as we want a full resource embedded
// TODO extends this annotation to provide information about lazy loading
// and projection behavior
@JsonProperty(access = Access.WRITE_ONLY)
private BitstreamFormatRest format;
private Long sizeBytes;
private CheckSumRest checkSum;
// sequenceId is READ_ONLY because it is assigned by the ItemService (as it must be unique within an Item)
@@ -40,14 +46,6 @@ public class BitstreamRest extends DSpaceObjectRest {
this.bundleName = bundleName;
}
public BitstreamFormatRest getFormat() {
return format;
}
public void setFormat(BitstreamFormatRest format) {
this.format = format;
}
public Long getSizeBytes() {
return sizeBytes;
}

View File

@@ -0,0 +1,51 @@
/**
* 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.sql.SQLException;
import java.util.UUID;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import org.dspace.app.rest.model.BitstreamFormatRest;
import org.dspace.app.rest.model.BitstreamRest;
import org.dspace.app.rest.projection.Projection;
import org.dspace.content.Bitstream;
import org.dspace.content.service.BitstreamService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
/**
* Link repository for "format" subresource of an individual bitstream.
*/
@Component(BitstreamRest.CATEGORY + "." + BitstreamRest.NAME + "." + BitstreamRest.FORMAT)
public class BitstreamFormatLinkRepository extends AbstractDSpaceRestRepository
implements LinkRestRepository {
@Autowired
BitstreamService bitstreamService;
//@PreAuthorize("hasPermission(#bitstreamId, 'BITSTREAM', 'READ')")
public BitstreamFormatRest getFormat(@Nullable HttpServletRequest request,
UUID bitstreamId,
@Nullable Pageable optionalPageable,
Projection projection) {
try {
Context context = obtainContext();
Bitstream bitstream = bitstreamService.find(context, bitstreamId);
if (bitstream == null || bitstream.getFormat(context) == null) {
return null;
}
return converter.toRest(bitstream.getFormat(context), projection);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}