From 69434775ca618d3db780e78d34d5fc653eaa1b10 Mon Sep 17 00:00:00 2001 From: damage Date: Mon, 23 Dec 2024 12:19:09 +0100 Subject: [PATCH] Servlet wasnt thread safe --- .../java/de/devloop/mavor/Configuration.java | 20 ++++++++++ .../devloop/mavor/servlet/DownloadJars.java | 38 +++++++++---------- .../de/devloop/mavor/servlet/DownloadZip.java | 8 ++-- 3 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 src/main/java/de/devloop/mavor/Configuration.java diff --git a/src/main/java/de/devloop/mavor/Configuration.java b/src/main/java/de/devloop/mavor/Configuration.java new file mode 100644 index 0000000..03e6e26 --- /dev/null +++ b/src/main/java/de/devloop/mavor/Configuration.java @@ -0,0 +1,20 @@ +package de.devloop.mavor; + +import java.util.Objects; + +public class Configuration { + + private static final String ENV_MAVEN_EXECUTABLE = "MAVOR_MAVEN_EXECUTABLE"; + private static final String ENV_TEMP_DIR = "MAVOR_TEMP_DIR"; + + private final String mavenExecutable = Objects.requireNonNull(System.getProperty(ENV_MAVEN_EXECUTABLE)); + private final String tempDir = Objects.requireNonNull(System.getProperty(ENV_TEMP_DIR)); + + public String getMavenExecutable() { + return mavenExecutable; + } + + public String getTempDir() { + return tempDir; + } +} diff --git a/src/main/java/de/devloop/mavor/servlet/DownloadJars.java b/src/main/java/de/devloop/mavor/servlet/DownloadJars.java index fda2e1c..2a87ded 100644 --- a/src/main/java/de/devloop/mavor/servlet/DownloadJars.java +++ b/src/main/java/de/devloop/mavor/servlet/DownloadJars.java @@ -18,6 +18,7 @@ import java.util.zip.ZipOutputStream; import de.devloop.mavor.Artifact; import de.devloop.mavor.AuthenticatedServlet; +import de.devloop.mavor.Configuration; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; @@ -34,13 +35,7 @@ public class DownloadJars extends AuthenticatedServlet { private static final String PARAMETER_POM = "pom"; private static final String PARAMETER_TYPE = "type"; - // TODO: set as property - private static final String MAVEN_CMD = "/usr/bin/mvn"; - private static final String TEMP_DIR = "/home/damage/Temp/mavor"; - - private final UUID randomUUID = UUID.randomUUID(); - private final File tempDir = new File(TEMP_DIR); - private final File tempDirJars = new File(tempDir, randomUUID.toString()); + private final Configuration configuration = new Configuration(); @Override protected void doAuthenticatedPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -49,17 +44,21 @@ public class DownloadJars extends AuthenticatedServlet { @Override protected void doAuthenticatedGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + final UUID randomUUID = UUID.randomUUID(); + final File tempDir = new File(configuration.getTempDir()); + final File tempDirJars = new File(tempDir, randomUUID.toString()); + // create UUID subdirectory // should only be one level, so not mkdirs tempDirJars.mkdir(); // create pom.xml - File pomXmlFile = setupPomFile(req); + File pomXmlFile = setupPomFile(req, tempDirJars); // run maven command ExecutionResult executionResult; try { - executionResult = executeMaven(); + executionResult = executeMaven(tempDirJars); } catch (InterruptedException e) { throw new ServletException("Running maven failed", e); } finally { @@ -71,7 +70,7 @@ public class DownloadJars extends AuthenticatedServlet { req.setAttribute("stderr", executionResult.stderr); req.setAttribute("exitcode", executionResult.exitCode); - String zipFilename = zipDependencies(); + String zipFilename = zipDependencies(tempDir, tempDirJars); req.setAttribute("zipFilename", zipFilename); RequestDispatcher view = req.getRequestDispatcher("/download.jsp"); @@ -93,8 +92,9 @@ public class DownloadJars extends AuthenticatedServlet { return artifact; } - private ExecutionResult executeMaven() throws IOException, InterruptedException { - Process mvnProcess = Runtime.getRuntime().exec(MAVEN_CMD + " dependency:copy-dependencies -DoutputDirectory=" + tempDirJars.getCanonicalPath(), null, tempDirJars.getCanonicalFile()); + private ExecutionResult executeMaven(File tempDirJars) throws IOException, InterruptedException { + String mavenCmd = String.format("%s dependency:copy-dependencies -DoutputDirectory=\"%s\"", configuration.getMavenExecutable(), tempDirJars.getCanonicalPath()); + Process mvnProcess = Runtime.getRuntime().exec(mavenCmd, null, tempDirJars.getCanonicalFile()); String stdout = new String(mvnProcess.getInputStream().readAllBytes(), StandardCharsets.UTF_8); String stderr = new String(mvnProcess.getErrorStream().readAllBytes(), StandardCharsets.UTF_8); @@ -106,16 +106,16 @@ public class DownloadJars extends AuthenticatedServlet { return executionResult; } - private File setupPomFile(HttpServletRequest req) throws ServletException, IOException { + private File setupPomFile(HttpServletRequest req, File tempDirJars) throws ServletException, IOException { File pomXmlFile; try { String type = Objects.requireNonNull(req.getParameter(PARAMETER_TYPE)); if (type.equals("artifact")) { Artifact artifact = getArtifact(req); - pomXmlFile = generatePomXmlByArtifact(artifact); + pomXmlFile = generatePomXmlByArtifact(artifact, tempDirJars); } else if (type.equals("pom")) { String pomXml = Objects.requireNonNull(req.getParameter(PARAMETER_POM)); - pomXmlFile = writePomXml(pomXml); + pomXmlFile = writePomXml(pomXml, tempDirJars); } else { throw new ServletException("Unknown download type: " + type); } @@ -126,17 +126,17 @@ public class DownloadJars extends AuthenticatedServlet { return pomXmlFile; } - private File generatePomXmlByArtifact(Artifact artifact) throws IOException { + private File generatePomXmlByArtifact(Artifact artifact, File tempDirJars) throws IOException { String pomXml = getResourceFileAsString("pomstub.xml"); pomXml = pomXml.replace("{repository-id}", "foo"); pomXml = pomXml.replace("{repository-url}", artifact.getRepository()); pomXml = pomXml.replace("{group-id}", artifact.getGroupId()); pomXml = pomXml.replace("{artifact-id}", artifact.getArtifactId()); pomXml = pomXml.replace("{version}", artifact.getVersion()); - return writePomXml(pomXml); + return writePomXml(pomXml, tempDirJars); } - private File writePomXml(String pomXml) throws IOException { + private File writePomXml(String pomXml, File tempDirJars) throws IOException { File pomXmlFile = new File(tempDirJars, "pom.xml"); FileWriter fileWriter = new FileWriter(pomXmlFile); fileWriter.write(pomXml); @@ -145,7 +145,7 @@ public class DownloadJars extends AuthenticatedServlet { return pomXmlFile; } - private String zipDependencies() throws IOException { + private String zipDependencies(File tempDir, File tempDirJars) throws IOException { File zipFile = new File(tempDir, "mavor_" + UUID.randomUUID() + ".zip"); ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipFile)); diff --git a/src/main/java/de/devloop/mavor/servlet/DownloadZip.java b/src/main/java/de/devloop/mavor/servlet/DownloadZip.java index 35aa7dd..c8c83e7 100644 --- a/src/main/java/de/devloop/mavor/servlet/DownloadZip.java +++ b/src/main/java/de/devloop/mavor/servlet/DownloadZip.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.IOException; import de.devloop.mavor.AuthenticatedServlet; +import de.devloop.mavor.Configuration; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServletRequest; @@ -13,14 +14,13 @@ import jakarta.servlet.http.HttpServletResponse; @WebServlet("/download/zip") public class DownloadZip extends AuthenticatedServlet { - // TODO: set as property - private static final String TEMP_DIR = "/home/damage/Temp/mavor"; - private static final String PARAMETER_FILENAME = "file"; - private File tempDir = new File(TEMP_DIR); + + private final Configuration configuration = new Configuration(); @Override protected void doAuthenticatedGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + File tempDir = new File(configuration.getTempDir()); String zipFilename = req.getParameter(PARAMETER_FILENAME); File zipFile = new File(tempDir, zipFilename);