implemented some checks and parallel use of website :D

This commit is contained in:
damage 2024-12-23 11:44:38 +01:00
parent 53aece5e85
commit 26ec3848dd
6 changed files with 115 additions and 36 deletions

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -37,7 +37,7 @@ public class DownloadZip extends AuthenticatedServlet {
throw new ServletException("-.-");
}
} else {
resp.sendRedirect("/mavor");
Main.redirectToMe(resp);
}
}
}

View File

@ -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");
}
}

View File

@ -8,8 +8,8 @@
<repositories>
<repository>
<id>{site-id}</id>
<url>{site-url}</url>
<id>{repository-id}</id>
<url>{repository-url}</url>
</repository>
</repositories>

View File

@ -5,7 +5,7 @@
<h2>Hello ${username}</h2>
<form method="get" action="/mavor/download/jars">
<input type="hidden" name="type" value="artifact"/>
Site: <input type="text" name="site" value="https://source.devloop.de/api/packages/damage/maven/" /><br/>
Repository: <input type="text" name="repository" value="https://source.devloop.de/api/packages/damage/maven/" /><br/>
Group ID: <input type="text" name="groupId" value="org.apache.activemq"/><br/>
Artifact ID: <input type="text" name="artifactId" value="artemis-core-client"/><br/>
Version: <input type="text" name="version" value="2.39.0"/><br/>