From 64d42fc8b31f9684e395c81a39e43cae66248536 Mon Sep 17 00:00:00 2001 From: car031 Date: Wed, 1 May 2024 10:40:59 +0200 Subject: [PATCH] close zipFile after usage --- .../core/parser/OpenOfficeParser.java | 6 +- .../core/transfer/InMemoryZipImport.java | 79 +++++----- .../client/system/update/PatchPanel.java | 135 +++++++++++------- .../client/system/update/UpdatePanel.java | 61 +++++--- logicaldoc-gui/war/skin/style.css | 8 ++ .../java/com/logicaldoc/util/io/ZipUtil.java | 26 +++- .../web/service/SystemServiceImpl.java | 3 +- 7 files changed, 198 insertions(+), 120 deletions(-) diff --git a/logicaldoc-core/src/main/java/com/logicaldoc/core/parser/OpenOfficeParser.java b/logicaldoc-core/src/main/java/com/logicaldoc/core/parser/OpenOfficeParser.java index 75cfe4732..3d2f44aaf 100644 --- a/logicaldoc-core/src/main/java/com/logicaldoc/core/parser/OpenOfficeParser.java +++ b/logicaldoc-core/src/main/java/com/logicaldoc/core/parser/OpenOfficeParser.java @@ -239,8 +239,8 @@ public int countPages(File input, String filename) { } int pages = 1; - ZipUtil zipUtil = new ZipUtil(); - try (InputStream is = zipUtil.getEntryStream(input, "meta.xml")) { + + try (ZipUtil zipUtil = new ZipUtil(); InputStream is = zipUtil.getEntryStream(input, "meta.xml")) { OpenOfficeMetadataHandler metadataHandler = new OpenOfficeMetadataHandler(); xmlReader.setContentHandler(metadataHandler); xmlReader.parse(new InputSource(is)); @@ -250,7 +250,7 @@ public int countPages(File input, String filename) { log.warn("Failed to extract OpenOffice meta.xml entry", t); } - try (InputStream is = zipUtil.getEntryStream(input, "content.xml")) { + try (ZipUtil zipUtil = new ZipUtil(); InputStream is = zipUtil.getEntryStream(input, "content.xml")) { OpenOfficePresentationMetadataHandler metadataHandler = new OpenOfficePresentationMetadataHandler(); xmlReader.setContentHandler(metadataHandler); xmlReader.parse(new InputSource(is)); diff --git a/logicaldoc-core/src/main/java/com/logicaldoc/core/transfer/InMemoryZipImport.java b/logicaldoc-core/src/main/java/com/logicaldoc/core/transfer/InMemoryZipImport.java index 803252f83..f6279ad3c 100644 --- a/logicaldoc-core/src/main/java/com/logicaldoc/core/transfer/InMemoryZipImport.java +++ b/logicaldoc-core/src/main/java/com/logicaldoc/core/transfer/InMemoryZipImport.java @@ -61,45 +61,46 @@ private void extractEntries(File sourceZipFile, Folder parent, String sessionId) DocumentManager docManager = (DocumentManager) Context.get().getBean(DocumentManager.class); // Open the Zip and list all the contents - ZipUtil zipUtil = new ZipUtil(); - zipUtil.setFileNameCharset(fileNameCharset); - List entries = zipUtil.listEntries(sourceZipFile); - - for (String entry : entries) { - String fileName = FileUtil.getName(entry); - String title = FileUtil.getBaseName(fileName); - - if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(title)) - continue; - - String relativePath = FileUtil.getPath(entry); - if (relativePath.startsWith("/")) - relativePath = relativePath.substring(1); - if (relativePath.endsWith("/")) - relativePath = relativePath.substring(0, relativePath.length() - 1); - - // Ensure to have the proper folder to upload the file into - FolderHistory folderTransaction = new FolderHistory(); - folderTransaction.setSessionId(sessionId); - folderTransaction.setUser(user); - Folder folder = fDao.createPath(parent, relativePath, true, folderTransaction); - - // Create the document - Document doc = new Document(docVo); - doc.setId(0L); - doc.setFileName(fileName); - doc.setFolder(folder); - - DocumentHistory history = new DocumentHistory(); - history.setEvent(DocumentEvent.STORED.toString()); - history.setComment(""); - history.setUser(user); - history.setSessionId(sessionId); - - try { - docManager.create(zipUtil.getEntryStream(sourceZipFile, entry), doc, history); - } catch (Exception e) { - logger.warn("InMemoryZipImport unable to import ZIP entry {}", entry, e); + try (ZipUtil zipUtil = new ZipUtil();) { + zipUtil.setFileNameCharset(fileNameCharset); + List entries = zipUtil.listEntries(sourceZipFile); + + for (String entry : entries) { + String fileName = FileUtil.getName(entry); + String title = FileUtil.getBaseName(fileName); + + if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(title)) + continue; + + String relativePath = FileUtil.getPath(entry); + if (relativePath.startsWith("/")) + relativePath = relativePath.substring(1); + if (relativePath.endsWith("/")) + relativePath = relativePath.substring(0, relativePath.length() - 1); + + // Ensure to have the proper folder to upload the file into + FolderHistory folderTransaction = new FolderHistory(); + folderTransaction.setSessionId(sessionId); + folderTransaction.setUser(user); + Folder folder = fDao.createPath(parent, relativePath, true, folderTransaction); + + // Create the document + Document doc = new Document(docVo); + doc.setId(0L); + doc.setFileName(fileName); + doc.setFolder(folder); + + DocumentHistory history = new DocumentHistory(); + history.setEvent(DocumentEvent.STORED.toString()); + history.setComment(""); + history.setUser(user); + history.setSessionId(sessionId); + + try { + docManager.create(zipUtil.getEntryStream(sourceZipFile, entry), doc, history); + } catch (Exception e) { + logger.warn("InMemoryZipImport unable to import ZIP entry {}", entry, e); + } } } } diff --git a/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/PatchPanel.java b/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/PatchPanel.java index 0df5d5046..c84dec08e 100644 --- a/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/PatchPanel.java +++ b/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/PatchPanel.java @@ -26,12 +26,14 @@ import com.logicaldoc.gui.common.client.widgets.grid.FileSizeListGridField; import com.logicaldoc.gui.frontend.client.services.UpdateService; import com.smartgwt.client.types.Alignment; +import com.smartgwt.client.types.ContentsType; import com.smartgwt.client.types.ExpansionMode; import com.smartgwt.client.types.ListGridFieldType; import com.smartgwt.client.types.SelectionStyle; import com.smartgwt.client.types.TitleOrientation; import com.smartgwt.client.types.VerticalAlignment; import com.smartgwt.client.util.SC; +import com.smartgwt.client.widgets.HTMLPane; import com.smartgwt.client.widgets.IButton; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.Progressbar; @@ -42,7 +44,6 @@ import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.grid.ListGridRecord; -import com.smartgwt.client.widgets.grid.events.CellContextClickEvent; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.layout.VLayout; import com.smartgwt.client.widgets.menu.Menu; @@ -162,9 +163,9 @@ protected String getCellCSSText(ListGridRecord rec, int rowNum, int colNum) { list.setDetailField(DESCRIPTION); list.setFields(id, name, rating, date, size, installed, restart, file, local); - list.addCellContextClickHandler((CellContextClickEvent event) -> { + list.addCellContextClickHandler(contextClick -> { showContextMenu(list); - event.cancel(); + contextClick.cancel(); }); List records = new ArrayList<>(); @@ -193,7 +194,7 @@ protected String getCellCSSText(ListGridRecord rec, int rowNum, int colNum) { addMember(listPanel); upload.setAutoFit(true); - upload.addClickHandler(event -> new PatchUploader(this).show()); + upload.addClickHandler(click -> new PatchUploader(this).show()); } private void switchDetailView(GUIPatch patch) { @@ -341,44 +342,43 @@ private VLayout prepareActionBar(GUIPatch patch) { bar.setPercentDone(0); download.setDisabled(true); - UpdateService.Instance.get().downloadPatch(patch.getId(), fileName, patch.getSize(), - new AsyncCallback<>() { + UpdateService.Instance.get().downloadPatch(patch.getId(), fileName, patch.getSize(), new AsyncCallback<>() { - @Override - public void onFailure(Throwable caught) { - GuiLog.serverError(caught); - download.setDisabled(false); - } + @Override + public void onFailure(Throwable caught) { + GuiLog.serverError(caught); + download.setDisabled(false); + } + + @Override + public void onSuccess(Void arg) { + confirmPatch.setVisible(false); + + new Timer() { + public void run() { + UpdateService.Instance.get().checkDownloadStatus(new AsyncCallback<>() { + + @Override + public void onFailure(Throwable caught) { + GuiLog.serverError(caught); + } + + @Override + public void onSuccess(List status) { + bar.setPercentDone(status.get(1)); - @Override - public void onSuccess(Void arg) { - confirmPatch.setVisible(false); - - new Timer() { - public void run() { - UpdateService.Instance.get().checkDownloadStatus(new AsyncCallback<>() { - - @Override - public void onFailure(Throwable caught) { - GuiLog.serverError(caught); - } - - @Override - public void onSuccess(List status) { - bar.setPercentDone(status.get(1)); - - if (status.get(1) == 100) { - download.setDisabled(false); - confirmPatch.setVisible(true); - displayNotes(fileName); - } else - schedule(50); - } - }); + if (status.get(1) == 100) { + download.setDisabled(false); + confirmPatch.setVisible(true); + displayNotes(fileName); + } else + schedule(50); } - }.schedule(50); + }); } - }); + }.schedule(50); + } + }); }); HLayout buttonCanvas = new HLayout(); @@ -403,22 +403,49 @@ public void onFailure(Throwable caught) { @Override public void onSuccess(List infos) { - DynamicForm form = new DynamicForm(); - form.setTitleOrientation(TitleOrientation.TOP); - form.setColWidths("*"); - form.setNumCols(1); - - TextAreaItem changelog = ItemFactory.newTextAreaItem("changelog", infos.get(0)); - changelog.setWidth("100%"); - changelog.setHeight(220); - - TextAreaItem patchNotes = ItemFactory.newTextAreaItem("patchnotes", infos.get(1)); - patchNotes.setWidth("100%"); - patchNotes.setHeight(220); - - form.setItems(patchNotes, changelog); - - notesPanel.addMember(form); + VLayout panel = new VLayout(); + + Label notesLabel = new Label(I18N.message("patchnotes")); + notesLabel.setWrap(false); + notesLabel.setAutoFit(true); + notesLabel.setHeight(20); + notesLabel.setStyleName("update-notes"); + String notesContent = infos.get(1); + if (!notesContent.contains(""); + + HTMLPane notesContentPanel = new HTMLPane(); + notesContentPanel.setWidth100(); + notesContentPanel.setHeight("50%"); + notesContentPanel.setShowEdges(true); + notesContentPanel.setContents(notesContent); + notesContentPanel.setContentsType(ContentsType.FRAGMENT); + + Label changesLabel = new Label(I18N.message("changelog")); + changesLabel.setWrap(false); + changesLabel.setAutoFit(true); + changesLabel.setHeight(20); + changesLabel.setStyleName("update-changelog"); + String changesContent = infos.get(0); + if (!changesContent.contains(""); + + HTMLPane changesContentPanel = new HTMLPane(); + changesContentPanel.setWidth100(); + changesContentPanel.setHeight("50%"); + changesContentPanel.setShowEdges(true); + changesContentPanel.setContents(changesContent); + changesContentPanel.setContentsType(ContentsType.FRAGMENT); + + Label separator = new Label(" "); + separator.setHeight(10); + + panel.setMembers(notesLabel, notesContentPanel, separator, changesLabel, changesContentPanel); + + notesPanel.addMember(panel); } }); } diff --git a/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/UpdatePanel.java b/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/UpdatePanel.java index f6e842d3d..c43f34404 100644 --- a/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/UpdatePanel.java +++ b/logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/system/update/UpdatePanel.java @@ -22,9 +22,11 @@ import com.logicaldoc.gui.common.client.widgets.FeatureDisabled; import com.logicaldoc.gui.frontend.client.services.UpdateService; import com.smartgwt.client.types.Alignment; +import com.smartgwt.client.types.ContentsType; import com.smartgwt.client.types.TitleOrientation; import com.smartgwt.client.types.VerticalAlignment; import com.smartgwt.client.util.SC; +import com.smartgwt.client.widgets.HTMLPane; import com.smartgwt.client.widgets.IButton; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.Progressbar; @@ -279,22 +281,49 @@ public void onFailure(Throwable caught) { @Override public void onSuccess(List infos) { - DynamicForm form = new DynamicForm(); - form.setTitleOrientation(TitleOrientation.TOP); - form.setColWidths("*"); - form.setNumCols(1); - - TextAreaItem changelog = ItemFactory.newTextAreaItem("changelog", infos.get(0)); - changelog.setWidth("100%"); - changelog.setHeight(220); - - TextAreaItem updatenotes = ItemFactory.newTextAreaItem("updatenotes", infos.get(1)); - updatenotes.setWidth("100%"); - updatenotes.setHeight(220); - - form.setItems(updatenotes, changelog); - - notesPanel.addMember(form); + VLayout panel = new VLayout(); + + Label notesLabel = new Label(I18N.message("updatenotes")); + notesLabel.setWrap(false); + notesLabel.setAutoFit(true); + notesLabel.setHeight(20); + notesLabel.setStyleName("update-notes"); + String notesContent = infos.get(1); + if (!notesContent.contains(""); + + HTMLPane notesContentPanel = new HTMLPane(); + notesContentPanel.setWidth100(); + notesContentPanel.setHeight("50%"); + notesContentPanel.setShowEdges(true); + notesContentPanel.setContents(notesContent); + notesContentPanel.setContentsType(ContentsType.FRAGMENT); + + Label changesLabel = new Label(I18N.message("changelog")); + changesLabel.setWrap(false); + changesLabel.setAutoFit(true); + changesLabel.setHeight(20); + changesLabel.setStyleName("update-changelog"); + String changesContent = infos.get(0); + if (!changesContent.contains(""); + + HTMLPane changesContentPanel = new HTMLPane(); + changesContentPanel.setWidth100(); + changesContentPanel.setHeight("50%"); + changesContentPanel.setShowEdges(true); + changesContentPanel.setContents(changesContent); + changesContentPanel.setContentsType(ContentsType.FRAGMENT); + + Label separator = new Label(" "); + separator.setHeight(10); + + panel.setMembers(notesLabel, notesContentPanel, separator, changesLabel, changesContentPanel); + + notesPanel.addMember(panel); } }); } diff --git a/logicaldoc-gui/war/skin/style.css b/logicaldoc-gui/war/skin/style.css index ce0ae4bea..b373c665b 100644 --- a/logicaldoc-gui/war/skin/style.css +++ b/logicaldoc-gui/war/skin/style.css @@ -450,3 +450,11 @@ border-radius: 0px; outline: none; } + +.update-changelog, .update-notes{ + font-family: Arial, Bitstream Vera Sans, sans-serif; + font-size: 14pt; + font-weight: bold; + white-space: nowrap; + color: red; +} diff --git a/logicaldoc-util/src/main/java/com/logicaldoc/util/io/ZipUtil.java b/logicaldoc-util/src/main/java/com/logicaldoc/util/io/ZipUtil.java index b39379e9a..e1a77efb4 100644 --- a/logicaldoc-util/src/main/java/com/logicaldoc/util/io/ZipUtil.java +++ b/logicaldoc-util/src/main/java/com/logicaldoc/util/io/ZipUtil.java @@ -3,6 +3,7 @@ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -38,7 +39,7 @@ * @author Marco Meschieri - LogicalDOC * @version 4.0 */ -public class ZipUtil { +public class ZipUtil implements Closeable { private static final String ZIP_FILE_S_LOOKS_LIKE_A_ZIP_BOMB_ENTRIES = "Zip file %s looks like a Zip Bomb Attack: can lead to inodes exhaustion of the system and is over the maximum allowed of %d"; @@ -65,6 +66,8 @@ public class ZipUtil { */ private double maxCompressionRatio = 30D; + private ZipFile zFile; + public ZipUtil() { try { maxEntries = Context.get().getProperties().getInt("zip.maxentries", 100000); @@ -107,8 +110,7 @@ public List listZipEntries(File zipFile) { public List listEntries(File zipFile) { List files = new ArrayList<>(); - try { - ZipFile zFile = new ZipFile(zipFile); + try (ZipFile zFile = new ZipFile(zipFile);) { setCharset(zFile); List fileHeaders = zFile.getFileHeaders(); @@ -277,6 +279,8 @@ public long unzip(File zipFile, String entry, File target) throws IOException { bos.flush(); return totalSizeEntry; + } finally { + zFile.close(); } } @@ -293,8 +297,8 @@ public byte[] getEntryBytes(File zipFile, String entry) { entry = entry.substring(1); InputStream entryStream = null; - try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) { - ZipFile zFile = new ZipFile(zipFile); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipFile zFile = new ZipFile(zipFile);) { + setCharset(zFile); FileHeader header = zFile.getFileHeader(entry); @@ -332,7 +336,7 @@ public InputStream getEntryStream(File zipFile, String entry) { entry = entry.substring(1); try { - ZipFile zFile = new ZipFile(zipFile); + zFile = new ZipFile(zipFile); setCharset(zFile); FileHeader header = zFile.getFileHeader(entry); return zFile.getInputStream(header); @@ -342,6 +346,16 @@ public InputStream getEntryStream(File zipFile, String entry) { } } + @Override + public void close() { + if (zFile != null) + try { + zFile.close(); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + public String getEntryContent(File zip, String entry) throws IOException { return IOUtil.readStream(getEntryStream(zip, entry)); } diff --git a/logicaldoc-webapp/src/main/java/com/logicaldoc/web/service/SystemServiceImpl.java b/logicaldoc-webapp/src/main/java/com/logicaldoc/web/service/SystemServiceImpl.java index 81e3f0941..6059878ff 100644 --- a/logicaldoc-webapp/src/main/java/com/logicaldoc/web/service/SystemServiceImpl.java +++ b/logicaldoc-webapp/src/main/java/com/logicaldoc/web/service/SystemServiceImpl.java @@ -946,7 +946,7 @@ public void installPlugin() throws ServerException { if (uploadedFilesMap == null || uploadedFilesMap.size() < 1) throw new ServerException("Cannot find a plugin package to install"); - try { + try (ZipUtil zipUtil = new ZipUtil();) { File pluginPackage = uploadedFilesMap.values().iterator().next(); ContextProperties config = Context.get().getProperties(); File rootFolder; @@ -959,7 +959,6 @@ public void installPlugin() throws ServerException { String pluginVersion = null; String pluginJar = null; - ZipUtil zipUtil = new ZipUtil(); try (InputStream pluginStream = zipUtil.getEntryStream(pluginPackage, "/plugin.xml")) { if (pluginStream == null) throw new ServerException("The plugin package does not include the descriptor plugin.xml");