From c338c1a15364329817c48b9f2f0c2fc3dfd8844d Mon Sep 17 00:00:00 2001 From: OlexYarm Date: Fri, 6 Oct 2023 12:07:44 -0400 Subject: [PATCH 1/3] Fixed issue 10305 Embedded Jetty server fails to start when requests log path in RequestLogWriter contains not existed directory Changed error message when requests log path in RequestLogWriter contains not existed directory. --- .../jetty/util/RolloverFileOutputStream.java | 14 ++++++- .../util/RolloverFileOutputStreamTest.java | 41 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java index e30b443acf1..94bfae53f89 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java @@ -233,9 +233,19 @@ public class RolloverFileOutputStream extends OutputStream File file = new File(_filename); _filename = file.getCanonicalPath(); file = new File(_filename); - File dir = new File(file.getParent()); - if (!dir.isDirectory() || !dir.canWrite()) + File dir = file.getParentFile(); + if (!dir.exists()) + { + throw new IOException("Log directory does not exist. Path=" + dir); + } + else if (!dir.isDirectory()) + { + throw new IOException("Path for Log directory is not a directory. Path=" + dir); + } + else if (!dir.canWrite()) + { throw new IOException("Cannot write log directory " + dir); + } // Is this a rollover file? String filename = file.getName(); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java index ee3872f98ea..7c1f6710643 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java @@ -181,6 +181,47 @@ public class RolloverFileOutputStreamTest assertSequence(midnight, expected); } + @Test + public void testMissingDirectory() + { + + ZoneId zone = toZoneId("Australia/Sydney"); + ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone); + String templateString = "missingDir/test-rofos-yyyy_mm_dd.log"; + String excMessageExpected = "Log directory does not exist."; + + try (RolloverFileOutputStream rofos = + new RolloverFileOutputStream(templateString, false, 3, TimeZone.getTimeZone(zone), null, null, now)) + { + rofos.write("TICK".getBytes()); + rofos.flush(); + } + catch (Exception ex) + { + boolean exClassOK = false; + if (ex instanceof java.io.IOException) + { + exClassOK = true; + } + assertThat("Exception Type", exClassOK, is(true)); + + String excMessageActual = ex.getMessage(); + boolean messageOK = false; + if (excMessageActual != null) + { + excMessageActual = excMessageActual.trim(); + if (!excMessageActual.isEmpty()) + { + if (excMessageActual.contains(excMessageExpected)) + { + messageOK = true; + } + } + } + assertThat("Exception Message", messageOK, is(true)); + } + } + @Test public void testFileHandling() throws Exception { From 40a31b839475f20d9b8639c738444d9fe3307462 Mon Sep 17 00:00:00 2001 From: OlexYarm Date: Mon, 16 Oct 2023 18:26:59 -0400 Subject: [PATCH 2/3] Update RolloverFileOutputStreamTest.java Simplified test class as suggested --- .../util/RolloverFileOutputStreamTest.java | 43 ++++++------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java index 7c1f6710643..fbe120cd945 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java @@ -36,7 +36,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertNotNull; @ExtendWith(WorkDirExtension.class) public class RolloverFileOutputStreamTest @@ -185,41 +188,21 @@ public class RolloverFileOutputStreamTest public void testMissingDirectory() { - ZoneId zone = toZoneId("Australia/Sydney"); - ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone); String templateString = "missingDir/test-rofos-yyyy_mm_dd.log"; - String excMessageExpected = "Log directory does not exist."; - - try (RolloverFileOutputStream rofos = - new RolloverFileOutputStream(templateString, false, 3, TimeZone.getTimeZone(zone), null, null, now)) + String excMessageActual = null; + Throwable error = null; + try (RolloverFileOutputStream rofos = new RolloverFileOutputStream(templateString)) { - rofos.write("TICK".getBytes()); - rofos.flush(); + throw new IllegalStateException(); } - catch (Exception ex) + catch (Throwable t) { - boolean exClassOK = false; - if (ex instanceof java.io.IOException) - { - exClassOK = true; - } - assertThat("Exception Type", exClassOK, is(true)); - - String excMessageActual = ex.getMessage(); - boolean messageOK = false; - if (excMessageActual != null) - { - excMessageActual = excMessageActual.trim(); - if (!excMessageActual.isEmpty()) - { - if (excMessageActual.contains(excMessageExpected)) - { - messageOK = true; - } - } - } - assertThat("Exception Message", messageOK, is(true)); + error = t; + excMessageActual = t.getMessage(); } + assertNotNull(error); + assertThat(error, instanceOf(IOException.class)); + assertThat(excMessageActual, containsString("Log directory does not exist.")); } @Test From 4b6627ae0ccd33c85684bff395a5d28b1b6b653f Mon Sep 17 00:00:00 2001 From: OlexYarm Date: Mon, 16 Oct 2023 18:48:09 -0400 Subject: [PATCH 3/3] Update RolloverFileOutputStreamTest.java more simplification --- .../eclipse/jetty/util/RolloverFileOutputStreamTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java index fbe120cd945..c19bc4184fe 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java @@ -189,8 +189,7 @@ public class RolloverFileOutputStreamTest { String templateString = "missingDir/test-rofos-yyyy_mm_dd.log"; - String excMessageActual = null; - Throwable error = null; + Throwable error; try (RolloverFileOutputStream rofos = new RolloverFileOutputStream(templateString)) { throw new IllegalStateException(); @@ -198,11 +197,11 @@ public class RolloverFileOutputStreamTest catch (Throwable t) { error = t; - excMessageActual = t.getMessage(); } assertNotNull(error); assertThat(error, instanceOf(IOException.class)); - assertThat(excMessageActual, containsString("Log directory does not exist.")); + error.getMessage(); + assertThat(error.getMessage(), containsString("Log directory does not exist.")); } @Test