Move the citation generation in a single method again

This commit is contained in:
Kevin Van de Velde
2022-01-26 16:02:49 +01:00
parent a15478178d
commit 42cae867db
5 changed files with 25 additions and 26 deletions

View File

@@ -154,7 +154,7 @@ public class CitationPage extends AbstractCurationTask {
try {
//Create the cited document
InputStream citedInputStream =
citationDocument.getCitedDocument(Curator.curationContext(), bitstream);
citationDocument.makeCitedDocument(Curator.curationContext(), bitstream).getLeft();
//Add the cited document to the approiate bundle
this.addCitedPageToItem(citedInputStream, bundle, pBundle,
dBundle, displayMap, item, bitstream);

View File

@@ -21,6 +21,7 @@ import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
@@ -296,33 +297,30 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
}
@Override
public InputStream getCitedDocument(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException {
byte [] citedDocumentContent = getCitedDocumentContent(context, bitstream);
return new ByteArrayInputStream(citedDocumentContent);
}
@Override
public Long getCitedDocumentLength(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException {
byte[] citedDocumentContent = getCitedDocumentContent(context, bitstream);
return Long.valueOf(citedDocumentContent.length);
}
private byte[] getCitedDocumentContent(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException {
public Pair<InputStream, Long> makeCitedDocument(Context context, Bitstream bitstream)
throws IOException, SQLException, AuthorizeException {
PDDocument document = new PDDocument();
PDDocument sourceDocument = new PDDocument();
try {
Item item = (Item) bitstreamService.getParentObject(context, bitstream);
sourceDocument = sourceDocument.load(bitstreamService.retrieve(context, bitstream));
final InputStream inputStream = bitstreamService.retrieve(context, bitstream);
try {
sourceDocument = sourceDocument.load(inputStream);
} finally {
inputStream.close();
}
PDPage coverPage = new PDPage(citationPageFormat);
generateCoverPage(context, document, coverPage, item);
addCoverPageToDocument(document, sourceDocument, coverPage);
//We already have the full PDF in memory, so keep it there
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
document.save(out);
return out.toByteArray();
byte[] data = out.toByteArray();
return Pair.of(new ByteArrayInputStream(data), Long.valueOf(data.length));
}
} finally {
sourceDocument.close();
document.close();

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
@@ -83,12 +84,8 @@ public interface CitationDocumentService {
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public InputStream getCitedDocument(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException;
public Long getCitedDocumentLength(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException;
public Pair<InputStream, Long> makeCitedDocument(Context context, Bitstream bitstream)
throws IOException, SQLException, AuthorizeException;
/**
* @param page page

View File

@@ -12,6 +12,7 @@ import static org.dspace.app.rest.utils.RegexUtils.REGEX_REQUESTMAPPING_IDENTIFI
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
@@ -21,6 +22,7 @@ import javax.ws.rs.core.Response;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.converter.ConverterService;
import org.dspace.app.rest.exception.DSpaceBadRequestException;
@@ -133,7 +135,9 @@ public class BitstreamRestController {
try {
long filesize;
if (citationDocumentService.isCitationEnabledForBitstream(bit, context)) {
filesize = citationDocumentService.getCitedDocumentLength(context, bit);
final Pair<InputStream, Long> citedDocument = citationDocumentService.makeCitedDocument(context, bit);
filesize = citedDocument.getRight();
citedDocument.getLeft().close();
} else {
filesize = bit.getSizeBytes();
}

View File

@@ -66,7 +66,7 @@ public class BitstreamResource extends AbstractResource {
InputStream out;
if (citationDocumentService.isCitationEnabledForBitstream(bitstream, context)) {
out = citationDocumentService.getCitedDocument(context, bitstream);
out = citationDocumentService.makeCitedDocument(context, bitstream).getLeft();
} else {
out = bitstreamService.retrieve(context, bitstream);
}