From 26ec3848ddec8d196ba21509cd5865671837b816 Mon Sep 17 00:00:00 2001 From: damage Date: Mon, 23 Dec 2024 11:44:38 +0100 Subject: [PATCH] implemented some checks and parallel use of website :D --- src/main/java/de/devloop/mavor/Artifact.java | 40 ++++++++ .../devloop/mavor/servlet/DownloadJars.java | 97 +++++++++++++------ .../de/devloop/mavor/servlet/DownloadZip.java | 2 +- .../java/de/devloop/mavor/servlet/Main.java | 6 +- src/main/resources/pomstub.xml | 4 +- src/main/webapp/main.jsp | 2 +- 6 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 src/main/java/de/devloop/mavor/Artifact.java diff --git a/src/main/java/de/devloop/mavor/Artifact.java b/src/main/java/de/devloop/mavor/Artifact.java new file mode 100644 index 0000000..86bb8d7 --- /dev/null +++ b/src/main/java/de/devloop/mavor/Artifact.java @@ -0,0 +1,40 @@ +package de.devloop.mavor; + +public class Artifact { + private String groupId; + private String artifactId; + private String version; + private String repository; + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getArtifactId() { + return artifactId; + } + + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getRepository() { + return repository; + } + + public void setRepository(String repository) { + this.repository = repository; + } +} \ No newline at end of file diff --git a/src/main/java/de/devloop/mavor/servlet/DownloadJars.java b/src/main/java/de/devloop/mavor/servlet/DownloadJars.java index b8211e4..fda2e1c 100644 --- a/src/main/java/de/devloop/mavor/servlet/DownloadJars.java +++ b/src/main/java/de/devloop/mavor/servlet/DownloadJars.java @@ -10,11 +10,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.util.Objects; import java.util.UUID; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import de.devloop.mavor.Artifact; import de.devloop.mavor.AuthenticatedServlet; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; @@ -25,7 +27,7 @@ import jakarta.servlet.http.HttpServletResponse; @WebServlet("/download/jars") public class DownloadJars extends AuthenticatedServlet { - private static final String PARAMETER_SITE = "site"; + private static final String PARAMETER_REPOSITORY = "repository"; private static final String PARAMETER_GROUP_ID = "groupId"; private static final String PARAMETER_ARTIFACT_ID = "artifactId"; private static final String PARAMETER_VERSION = "version"; @@ -36,7 +38,9 @@ public class DownloadJars extends AuthenticatedServlet { private static final String MAVEN_CMD = "/usr/bin/mvn"; private static final String TEMP_DIR = "/home/damage/Temp/mavor"; - private File tempDir = new File(TEMP_DIR); + private final UUID randomUUID = UUID.randomUUID(); + private final File tempDir = new File(TEMP_DIR); + private final File tempDirJars = new File(tempDir, randomUUID.toString()); @Override protected void doAuthenticatedPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -45,23 +49,14 @@ public class DownloadJars extends AuthenticatedServlet { @Override protected void doAuthenticatedGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // TODO: implement a lot of checks: - // NPE, required parameters not given - - String type = req.getParameter(PARAMETER_TYPE); - File pomXmlFile; - if (type.equals("artifact")) { - String site = req.getParameter(PARAMETER_SITE); - String groupId = req.getParameter(PARAMETER_GROUP_ID); - String artifactId = req.getParameter(PARAMETER_ARTIFACT_ID); - String version = req.getParameter(PARAMETER_VERSION); - pomXmlFile = generatePomXmlByArtifact(site, groupId, artifactId, version); - } else if (type.equals("pom")) { - pomXmlFile = writePomXml(req.getParameter(PARAMETER_POM)); - } else { - throw new ServletException("Unknown download type: " + type); - } + // create UUID subdirectory + // should only be one level, so not mkdirs + tempDirJars.mkdir(); + + // create pom.xml + File pomXmlFile = setupPomFile(req); + // run maven command ExecutionResult executionResult; try { executionResult = executeMaven(); @@ -71,6 +66,7 @@ public class DownloadJars extends AuthenticatedServlet { pomXmlFile.delete(); } + // and off we go req.setAttribute("stdout", executionResult.stdout); req.setAttribute("stderr", executionResult.stderr); req.setAttribute("exitcode", executionResult.exitCode); @@ -82,8 +78,23 @@ public class DownloadJars extends AuthenticatedServlet { view.forward(req, resp); } + private Artifact getArtifact(HttpServletRequest req) { + String repository = Objects.requireNonNull(req.getParameter(PARAMETER_REPOSITORY)); + String groupId = Objects.requireNonNull(req.getParameter(PARAMETER_GROUP_ID)); + String artifactId = Objects.requireNonNull(req.getParameter(PARAMETER_ARTIFACT_ID)); + String version = Objects.requireNonNull(req.getParameter(PARAMETER_VERSION)); + + Artifact artifact = new Artifact(); + artifact.setGroupId(groupId); + artifact.setArtifactId(artifactId); + artifact.setVersion(version); + artifact.setRepository(repository); + + return artifact; + } + private ExecutionResult executeMaven() throws IOException, InterruptedException { - Process mvnProcess = Runtime.getRuntime().exec(MAVEN_CMD + " dependency:copy-dependencies -DoutputDirectory=" + TEMP_DIR, null, new File(TEMP_DIR)); + Process mvnProcess = Runtime.getRuntime().exec(MAVEN_CMD + " dependency:copy-dependencies -DoutputDirectory=" + tempDirJars.getCanonicalPath(), null, tempDirJars.getCanonicalFile()); String stdout = new String(mvnProcess.getInputStream().readAllBytes(), StandardCharsets.UTF_8); String stderr = new String(mvnProcess.getErrorStream().readAllBytes(), StandardCharsets.UTF_8); @@ -95,19 +106,38 @@ public class DownloadJars extends AuthenticatedServlet { return executionResult; } - private File generatePomXmlByArtifact(String site, String groupId, String artifactId, String version) throws IOException { + private File setupPomFile(HttpServletRequest req) 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); + } else if (type.equals("pom")) { + String pomXml = Objects.requireNonNull(req.getParameter(PARAMETER_POM)); + pomXmlFile = writePomXml(pomXml); + } else { + throw new ServletException("Unknown download type: " + type); + } + } catch (NullPointerException npe) { + throw new ServletException("Missing required parameter", npe); + } + + return pomXmlFile; + } + + private File generatePomXmlByArtifact(Artifact artifact) throws IOException { String pomXml = getResourceFileAsString("pomstub.xml"); - pomXml = pomXml.replace("{site-id}", "foo"); - pomXml = pomXml.replace("{site-url}", site); - pomXml = pomXml.replace("{group-id}", groupId); - pomXml = pomXml.replace("{artifact-id}", artifactId); - pomXml = pomXml.replace("{version}", version); + 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); } private File writePomXml(String pomXml) throws IOException { - File pomXmlFile = new File(tempDir, "pom.xml"); - pomXmlFile.delete(); + File pomXmlFile = new File(tempDirJars, "pom.xml"); FileWriter fileWriter = new FileWriter(pomXmlFile); fileWriter.write(pomXml); fileWriter.close(); @@ -118,13 +148,14 @@ public class DownloadJars extends AuthenticatedServlet { private String zipDependencies() throws IOException { File zipFile = new File(tempDir, "mavor_" + UUID.randomUUID() + ".zip"); ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipFile)); - // TODO: this goes BOOM on parallel use - File[] jarFiles = tempDir.listFiles(new FilenameFilter() { + + // get all jar files, make sure it is a jar and in correct directory + File[] jarFiles = tempDirJars.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { try { - return dir.getCanonicalPath().equals(tempDir.getCanonicalPath()) && name.endsWith(".jar"); + return dir.getCanonicalPath().equals(tempDirJars.getCanonicalPath()) && name.endsWith(".jar"); } catch (IOException e) { return false; } @@ -132,6 +163,7 @@ public class DownloadJars extends AuthenticatedServlet { }); + // zip the jars for (File jarFile : jarFiles) { FileInputStream jarFileStream = new FileInputStream(jarFile); ZipEntry zipEntry = new ZipEntry(jarFile.getName()); @@ -141,8 +173,11 @@ public class DownloadJars extends AuthenticatedServlet { jarFileStream.close(); jarFile.delete(); } - zipStream.close(); + + // remove temp directory, zip is in another directory + tempDirJars.delete(); + return zipFile.getName(); } diff --git a/src/main/java/de/devloop/mavor/servlet/DownloadZip.java b/src/main/java/de/devloop/mavor/servlet/DownloadZip.java index caa2c18..35aa7dd 100644 --- a/src/main/java/de/devloop/mavor/servlet/DownloadZip.java +++ b/src/main/java/de/devloop/mavor/servlet/DownloadZip.java @@ -37,7 +37,7 @@ public class DownloadZip extends AuthenticatedServlet { throw new ServletException("-.-"); } } else { - resp.sendRedirect("/mavor"); + Main.redirectToMe(resp); } } } diff --git a/src/main/java/de/devloop/mavor/servlet/Main.java b/src/main/java/de/devloop/mavor/servlet/Main.java index 07aedf1..9728087 100644 --- a/src/main/java/de/devloop/mavor/servlet/Main.java +++ b/src/main/java/de/devloop/mavor/servlet/Main.java @@ -18,5 +18,9 @@ public class Main extends AuthenticatedServlet { RequestDispatcher view = req.getRequestDispatcher("/main.jsp"); view.forward(req, resp); - } + } + + public static void redirectToMe(HttpServletResponse resp) throws IOException { + resp.sendRedirect("/mavor"); + } } diff --git a/src/main/resources/pomstub.xml b/src/main/resources/pomstub.xml index 1729ef7..d592f85 100644 --- a/src/main/resources/pomstub.xml +++ b/src/main/resources/pomstub.xml @@ -8,8 +8,8 @@ - {site-id} - {site-url} + {repository-id} + {repository-url} diff --git a/src/main/webapp/main.jsp b/src/main/webapp/main.jsp index 0a066b8..e42977c 100644 --- a/src/main/webapp/main.jsp +++ b/src/main/webapp/main.jsp @@ -5,7 +5,7 @@

Hello ${username}

- Site:
+ Repository:
Group ID:
Artifact ID:
Version: