From 2299adb704f7ebb2f2da72f4a9b51ea395966ea1 Mon Sep 17 00:00:00 2001 From: Ondro Mihalyi Date: Sun, 9 Jun 2024 01:13:17 +0200 Subject: [PATCH 1/2] Context root doesn't include a random string For apps deployed via Admin Console, the uploaded file was copied to a temp file, which contained a random string. Now, the temp directory has a random string, but the file in it has the same name. Context root is generated from the file name and doesn't include a random string with this commit. Signed-off-by:Ondro Mihalyi --- .../admingui/handlers/WoodstockHandler.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java b/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java index bfce5ccd265..3bf3581a8e5 100644 --- a/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java +++ b/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java @@ -47,7 +47,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; -import java.security.SecureRandom; import java.util.List; import java.util.ListIterator; import java.util.ArrayList; @@ -144,24 +143,17 @@ public static void uploadFileToTempDir(HandlerContext handlerCtx) { if (lastIndex != -1) { name = name.substring(lastIndex + 1, name.length()); } - int index = name.indexOf("."); + int index = name.lastIndexOf("."); if (index <= 0) { logger.info("name=" + name + ",index=" + index); String mesg = GuiUtil.getMessage("msg.deploy.nullArchiveError"); GuiUtil.handleError(handlerCtx, mesg); return; } - String suffix = name.substring(index); - String prefix = name.substring(0, index); - handlerCtx.setOutputValue("origPath", prefix); + handlerCtx.setOutputValue("origPath", name); try { - //createTempFile requires min. of 3 char for prefix. - if (prefix.length() <= 2) { - prefix = prefix + new SecureRandom().nextInt(100000); - } - - tmpFile = File.createTempFile(prefix, suffix); - tmpFile.deleteOnExit(); + // keep the filename for the temp file, it's used to generate the context root + tmpFile = createTempFileWithOriginalFileName(name); //delete the file, otherwise FileUtils#moveTo throws file already exist error tmpFile.delete(); @@ -193,6 +185,16 @@ public static void uploadFileToTempDir(HandlerContext handlerCtx) { handlerCtx.setOutputValue("uploadedTempFile", uploadTmpFile); } + private static File createTempFileWithOriginalFileName(String name) throws IOException { + final Path dir = Files.createTempDirectory(name); + dir.toFile().deleteOnExit(); + final Path file = dir.resolve(name); + Files.createFile(file); + final File result = file.toFile(); + result.deleteOnExit(); + return result; + } + /** *

* This handler enable or disable the table text field according to the From 4cbf5af4c58abb2bbcd91bd7275534c93d087626 Mon Sep 17 00:00:00 2001 From: Ondro Mihalyi Date: Tue, 11 Jun 2024 02:16:05 +0200 Subject: [PATCH 2/2] Context root doesn't include a random string - also remove after file is uploaded - improve file handling for uploaded files - give an error in GUI if IOException Signed-off-by:Ondro Mihalyi --- .../org/glassfish/admingui/common/util/GuiUtil.java | 3 ++- .../admingui/handlers/WoodstockHandler.java | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/util/GuiUtil.java b/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/util/GuiUtil.java index b9ef2af1d0a..6bead61aba4 100644 --- a/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/util/GuiUtil.java +++ b/appserver/admingui/common/src/main/java/org/glassfish/admingui/common/util/GuiUtil.java @@ -395,7 +395,8 @@ public static void prepareSuccessful(HandlerContext handlerCtx) { */ public static void prepareException(HandlerContext handlerCtx, Throwable ex) { Throwable rootException = getRootCause(ex); - prepareAlert("error", GuiUtil.getMessage("msg.Error"), rootException.getMessage()); + prepareAlert("error", GuiUtil.getMessage("msg.Error"), rootException.getClass().getSimpleName() + + ": " + rootException.getMessage()); GuiUtil.getLogger().info(GuiUtil.getCommonMessage("LOG_EXCEPTION_OCCURED") + ex.getLocalizedMessage()); if (GuiUtil.getLogger().isLoggable(Level.FINE)){ ex.printStackTrace(); diff --git a/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java b/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java index 3bf3581a8e5..07c415e6fbf 100644 --- a/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java +++ b/appserver/admingui/core/src/main/java/org/glassfish/admingui/handlers/WoodstockHandler.java @@ -92,7 +92,7 @@ public static void deleteFileFromTempDir(HandlerContext handlerCtx) throws IOExc if (Files.exists(pathToFile)) { try { Files.delete(pathToFile); - + Files.delete(pathToFile.getParent()); } catch (IOException x) { logger.log(Level.WARNING, prepareFileNotDeletedMessage(deleteTempFile)); @@ -155,8 +155,9 @@ public static void uploadFileToTempDir(HandlerContext handlerCtx) { // keep the filename for the temp file, it's used to generate the context root tmpFile = createTempFileWithOriginalFileName(name); - //delete the file, otherwise FileUtils#moveTo throws file already exist error - tmpFile.delete(); + // delete the file, otherwise FileUtils#moveTo called inside uploadedFile.write + // throws file already exist error + Files.deleteIfExists(tmpFile.toPath()); if (logger.isLoggable(Level.FINE)) { logger.fine(GuiUtil.getCommonMessage("log.writeToTmpFile")); @@ -170,11 +171,13 @@ public static void uploadFileToTempDir(HandlerContext handlerCtx) { } catch (IOException ioex) { try { if (tmpFile != null) { - uploadTmpFile = tmpFile.getAbsolutePath(); + Files.deleteIfExists(tmpFile.toPath()); + Files.deleteIfExists(tmpFile.toPath().getParent()); } } catch (Exception ex) { - //Handle AbsolutePathException here + // ignore nested exception, handle the original exception only } + GuiUtil.handleException(handlerCtx, ioex); } catch (Exception ex) { GuiUtil.handleException(handlerCtx, ex); }