|
|
|
@ -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
|
|
|
|
|
// create UUID subdirectory
|
|
|
|
|
// should only be one level, so not mkdirs
|
|
|
|
|
tempDirJars.mkdir();
|
|
|
|
|
|
|
|
|
|
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 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|