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 { try {
//Create the cited document //Create the cited document
InputStream citedInputStream = InputStream citedInputStream =
citationDocument.getCitedDocument(Curator.curationContext(), bitstream); citationDocument.makeCitedDocument(Curator.curationContext(), bitstream).getLeft();
//Add the cited document to the approiate bundle //Add the cited document to the approiate bundle
this.addCitedPageToItem(citedInputStream, bundle, pBundle, this.addCitedPageToItem(citedInputStream, bundle, pBundle,
dBundle, displayMap, item, bitstream); dBundle, displayMap, item, bitstream);

View File

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

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.SQLException; import java.sql.SQLException;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
@@ -83,12 +84,8 @@ public interface CitationDocumentService {
* @throws SQLException if database error * @throws SQLException if database error
* @throws AuthorizeException if authorization error * @throws AuthorizeException if authorization error
*/ */
public InputStream getCitedDocument(Context context, Bitstream bitstream) public Pair<InputStream, Long> makeCitedDocument(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException; throws IOException, SQLException, AuthorizeException;
public Long getCitedDocumentLength(Context context, Bitstream bitstream)
throws SQLException, AuthorizeException, IOException;
/** /**
* @param page page * @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 static org.springframework.web.bind.annotation.RequestMethod.PUT;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@@ -21,6 +22,7 @@ import javax.ws.rs.core.Response;
import org.apache.catalina.connector.ClientAbortException; import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.converter.ConverterService; import org.dspace.app.rest.converter.ConverterService;
import org.dspace.app.rest.exception.DSpaceBadRequestException; import org.dspace.app.rest.exception.DSpaceBadRequestException;
@@ -133,7 +135,9 @@ public class BitstreamRestController {
try { try {
long filesize; long filesize;
if (citationDocumentService.isCitationEnabledForBitstream(bit, context)) { 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 { } else {
filesize = bit.getSizeBytes(); filesize = bit.getSizeBytes();
} }

View File

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