Skip to content

Commit 5ecfa18

Browse files
UFAL/File preview better logs (#910)
* Refactored the method for extracting the file and added it into try catch block * Catch the Exception only in the top level of the script with a proper message
1 parent 7b3a1f3 commit 5ecfa18

File tree

4 files changed

+11
-38
lines changed

4 files changed

+11
-38
lines changed

dspace-api/src/main/java/org/dspace/content/PreviewContentServiceImpl.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,15 @@ public boolean canPreview(Context context, Bitstream bitstream) throws SQLExcept
152152

153153
@Override
154154
public List<FileInfo> getFilePreviewContent(Context context, Bitstream bitstream)
155-
throws SQLException, AuthorizeException, IOException {
155+
throws Exception {
156156
InputStream inputStream = null;
157157
List<FileInfo> fileInfos = null;
158158
try {
159159
inputStream = bitstreamService.retrieve(context, bitstream);
160160
} catch (MissingLicenseAgreementException e) { /* Do nothing */ }
161161

162162
if (Objects.nonNull(inputStream)) {
163-
try {
164-
fileInfos = processInputStreamToFilePreview(context, bitstream, inputStream);
165-
} catch (IllegalStateException e) {
166-
log.error("Cannot process Input Stream to file preview because: " + e.getMessage());
167-
}
163+
fileInfos = processInputStreamToFilePreview(context, bitstream, inputStream);
168164
}
169165
return fileInfos;
170166
}
@@ -193,7 +189,7 @@ public FileInfo createFileInfo(PreviewContent pc) {
193189
@Override
194190
public List<FileInfo> processInputStreamToFilePreview(Context context, Bitstream bitstream,
195191
InputStream inputStream)
196-
throws SQLException, IOException {
192+
throws Exception {
197193
List<FileInfo> fileInfos = new ArrayList<>();
198194
String bitstreamMimeType = bitstream.getFormat(context).getMIMEType();
199195
if (bitstreamMimeType.equals("text/plain")) {
@@ -216,12 +212,8 @@ public List<FileInfo> processInputStreamToFilePreview(Context context, Bitstream
216212

217213
String mimeType = bitstream.getFormat(context).getMIMEType();
218214
if (archiveTypes.containsKey(mimeType)) {
219-
try {
220-
data = extractFile(inputStream, archiveTypes.get(mimeType));
221-
fileInfos = FileTreeViewGenerator.parse(data);
222-
} catch (Exception e) {
223-
log.error("Cannot extract file content because: {}", e.getMessage());
224-
}
215+
data = extractFile(inputStream, archiveTypes.get(mimeType));
216+
fileInfos = FileTreeViewGenerator.parse(data);
225217
}
226218
}
227219
return fileInfos;
@@ -420,7 +412,7 @@ private String buildXmlResponse(List<String> filePaths) {
420412
* @param fileType the type of file to extract ("tar" or "zip")
421413
* @return an XML string representing the extracted file paths
422414
*/
423-
private String extractFile(InputStream inputStream, String fileType) {
415+
private String extractFile(InputStream inputStream, String fileType) throws Exception {
424416
List<String> filePaths = new ArrayList<>();
425417
Path tempFile = null;
426418
FileSystem zipFileSystem = null;
@@ -439,8 +431,6 @@ private String extractFile(InputStream inputStream, String fileType) {
439431
zipFileSystem = FileSystems.newFileSystem(tempFile, (ClassLoader) null);
440432
processZipFile(filePaths, zipFileSystem);
441433
}
442-
} catch (IOException e) {
443-
log.error(String.format("An error occurred while extracting file of type %s.", fileType), e);
444434
} finally {
445435
closeFileSystem(zipFileSystem);
446436
deleteTempFile(tempFile);

dspace-api/src/main/java/org/dspace/content/service/PreviewContentService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
package org.dspace.content.service;
99

10-
import java.io.IOException;
1110
import java.io.InputStream;
1211
import java.sql.SQLException;
1312
import java.util.List;
@@ -116,8 +115,7 @@ PreviewContent create(Context context, Bitstream bitstream, String name, String
116115
* @param bitstream ZIP file bitstream
117116
* @return List of FileInfo classes where is wrapped ZIP file content
118117
*/
119-
List<FileInfo> getFilePreviewContent(Context context, Bitstream bitstream)
120-
throws SQLException, AuthorizeException, IOException;
118+
List<FileInfo> getFilePreviewContent(Context context, Bitstream bitstream) throws Exception;
121119

122120
/**
123121
* Create preview content from file info for bitstream.
@@ -153,5 +151,5 @@ List<FileInfo> getFilePreviewContent(Context context, Bitstream bitstream)
153151
* @return List of FileInfo classes where is wrapped ZIP file content
154152
*/
155153
List<FileInfo> processInputStreamToFilePreview(Context context, Bitstream bitstream, InputStream inputStream)
156-
throws SQLException, IOException;
154+
throws Exception;
157155
}

dspace-api/src/main/java/org/dspace/scripts/filepreview/FilePreview.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77
*/
88
package org.dspace.scripts.filepreview;
99

10-
import java.io.IOException;
11-
import java.sql.SQLException;
1210
import java.util.Iterator;
1311
import java.util.List;
1412
import java.util.Objects;
1513
import java.util.UUID;
16-
import javax.xml.parsers.ParserConfigurationException;
1714

1815
import org.apache.commons.cli.ParseException;
19-
import org.apache.commons.compress.archivers.ArchiveException;
2016
import org.apache.commons.lang3.StringUtils;
21-
import org.dspace.authorize.AuthorizeException;
2217
import org.dspace.content.Bitstream;
2318
import org.dspace.content.Bundle;
2419
import org.dspace.content.Item;
@@ -30,7 +25,6 @@
3025
import org.dspace.scripts.DSpaceRunnable;
3126
import org.dspace.util.FileInfo;
3227
import org.dspace.utils.DSpace;
33-
import org.xml.sax.SAXException;
3428

3529
/**
3630
* This class is used to generate a preview for every file in DSpace that should have a preview.
@@ -99,8 +93,7 @@ public void internalRun() throws Exception {
9993
Item item = items.next();
10094
try {
10195
generateItemFilePreviews(context, item.getID());
102-
} catch (SQLException | AuthorizeException | IOException | ParserConfigurationException |
103-
ArchiveException | SAXException e) {
96+
} catch (Exception e) {
10497
handler.logError("Error while generating preview for item with UUID: " + item.getID());
10598
handler.logError(e.getMessage());
10699
}
@@ -115,8 +108,7 @@ public void internalRun() throws Exception {
115108
context.complete();
116109
}
117110

118-
private void generateItemFilePreviews(Context context, UUID itemUUID) throws SQLException, AuthorizeException,
119-
IOException, ParserConfigurationException, ArchiveException, SAXException {
111+
private void generateItemFilePreviews(Context context, UUID itemUUID) throws Exception {
120112
Item item = itemService.find(context, itemUUID);
121113
if (Objects.isNull(item)) {
122114
handler.logError("Item with UUID: " + itemUUID + " not found.");

dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
*/
88
package org.dspace.app.rest.repository;
99

10-
import java.io.IOException;
11-
import java.sql.SQLException;
1210
import java.util.ArrayList;
1311
import java.util.Arrays;
1412
import java.util.List;
1513
import java.util.Objects;
1614
import javax.servlet.http.HttpServletRequest;
17-
import javax.xml.parsers.ParserConfigurationException;
1815

19-
import org.apache.commons.compress.archivers.ArchiveException;
2016
import org.apache.commons.lang3.StringUtils;
2117
import org.apache.logging.log4j.Logger;
2218
import org.dspace.app.rest.Parameter;
@@ -26,7 +22,6 @@
2622
import org.dspace.app.rest.exception.UnprocessableEntityException;
2723
import org.dspace.app.rest.model.MetadataBitstreamWrapperRest;
2824
import org.dspace.app.rest.model.wrapper.MetadataBitstreamWrapper;
29-
import org.dspace.authorize.AuthorizeException;
3025
import org.dspace.content.Bitstream;
3126
import org.dspace.content.Bundle;
3227
import org.dspace.content.DSpaceObject;
@@ -43,7 +38,6 @@
4338
import org.springframework.data.domain.PageImpl;
4439
import org.springframework.data.domain.Pageable;
4540
import org.springframework.stereotype.Component;
46-
import org.xml.sax.SAXException;
4741

4842
/**
4943
* This controller returns content of the bitstream to the `Preview` box in the Item View.
@@ -68,8 +62,7 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository<Metada
6862
public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handle", required = true) String handle,
6963
@Parameter(value = "fileGrpType") String fileGrpType,
7064
Pageable pageable)
71-
throws SQLException, ParserConfigurationException, IOException, SAXException, AuthorizeException,
72-
ArchiveException {
65+
throws Exception {
7366
if (StringUtils.isBlank(handle)) {
7467
throw new DSpaceBadRequestException("handle cannot be null!");
7568
}

0 commit comments

Comments
 (0)