|
1 | 1 | package xyz.theprogramsrc.dependencydownloadermodule.objects |
2 | 2 |
|
3 | 3 | import org.json.XML |
4 | | -import xyz.theprogramsrc.simplecoreapi.libs.google.gson.JsonObject |
5 | 4 | import xyz.theprogramsrc.simplecoreapi.libs.google.gson.JsonParser |
6 | 5 | import java.net.URL |
7 | 6 |
|
8 | 7 | /** |
9 | 8 | * Representation of a Repository |
10 | | - * @param host The host of the repository |
11 | | - * @param repository The repository id in the host |
| 9 | + * @param url The url of the repository. Example: https://repo1.maven.org/maven2/ |
12 | 10 | */ |
13 | | -data class Repository(val host: String, val repository: String) { |
| 11 | +data class Repository(val url: String) { |
14 | 12 |
|
15 | | - /** |
16 | | - * Checks if the repository is a Sonatype Nexus Repository |
17 | | - * @param requiredVersion The required version of Nexus |
18 | | - * @return true if the repository is a Sonatype Nexus Repository, false otherwise |
19 | | - */ |
20 | | - fun isNexus(requiredVersion: Int = 3): Boolean = if(requiredVersion == 3) { |
21 | | - try { |
22 | | - (JsonParser.parseString(URL("${if(host.startsWith("http")) host else "https://${host}"}/service/rest/swagger.json").readText()).asJsonObject.get("info").asJsonObject.get("version").asString.substring(0,1).toIntOrNull() ?: 0) >= 3 |
23 | | - }catch (e: Exception) { |
24 | | - false |
25 | | - } |
26 | | - }else if(requiredVersion == 2) { |
27 | | - // TODO: Check if the repository is a Sonatype Nexus Repository v2 |
28 | | - false |
29 | | - } else{ |
30 | | - false // Unknown or older versions |
31 | | - } |
| 13 | + private val mavenUrlFormat = "%s/%s/%s/%s-%s.jar" |
32 | 14 |
|
33 | 15 | /** |
34 | | - * Finds the artifact and returns a JsonObject with the url, version and md5 hash if available |
35 | | - * @return A [JsonObject] with the information, null if not found |
| 16 | + * Finds the artifact url to download |
| 17 | + * @return The artifact url to download |
36 | 18 | */ |
37 | | - fun findArtifact(dependency: Dependency): JsonObject? { |
38 | | - if(isNexus(3)) { |
39 | | - val host = if(this.host.startsWith("http")) this.host else "https://${this.host}" |
40 | | - val version = try { |
41 | | - if (dependency.version.endsWith("-SNAPSHOT")) { |
42 | | - val json = JsonParser.parseString(XML.toJSONObject(URL("$host/repository/$repository/${dependency.group.replace(".", "/")}/${dependency.artifactId}/${dependency.version}/maven-metadata.xml").readText()).toString()) |
| 19 | + fun findArtifact(dependency: Dependency): String? { |
| 20 | + return try { |
| 21 | + val parsedVersion = if(dependency.version.endsWith("-SNAPSHOT")){ |
| 22 | + parseSnapshotVersion(dependency) |
| 23 | + } else if(dependency.version == "LATEST") { |
| 24 | + try { |
| 25 | + val result = JsonParser.parseString(XML.toJSONObject(URL("$url/${rewriteEscaping(dependency.group).replace('.','/')}/${dependency.artifactId}/maven-metadata.xml").readText()).toString()) |
43 | 26 | .asJsonObject |
44 | 27 | .getAsJsonObject("metadata") |
45 | 28 | .getAsJsonObject("versioning") |
46 | | - .getAsJsonObject("snapshot") |
47 | | - dependency.version.replace("-SNAPSHOT", "-${json.get("timestamp").asString}-${json.get("buildNumber").asString}") |
48 | | - } else { |
49 | | - dependency.version |
50 | | - } |
51 | | - }catch (e: Exception) { |
52 | | - e.printStackTrace() |
53 | | - null |
54 | | - } ?: return null |
55 | | - |
56 | | - val items = JsonParser.parseString(URL("$host/service/rest/v1/search?repository=$repository&group=${dependency.group}&format=maven2&maven.artifactId=${dependency.artifactId}&maven.extension=jar&sort=version&version=$version").readText()).asJsonObject.get("items").asJsonArray |
57 | | - val item = items.firstOrNull { it.asJsonObject.get("version").asString == version } ?: return null |
58 | | - val found = item.asJsonObject.get("assets").asJsonArray.firstOrNull { |
59 | | - if(!it.asJsonObject.get("maven2").asJsonObject.get("version").asString.equals(version)) { |
60 | | - return@firstOrNull false // Check that the current version matches |
61 | | - } |
62 | | - |
63 | | - if(!it.asJsonObject.get("maven2").asJsonObject.get("extension").asString.equals("jar")){ |
64 | | - return@firstOrNull false // Check that this is a jar file (not checksums or pom) |
65 | | - } |
66 | | - |
67 | | - if(it.asJsonObject.get("maven2").asJsonObject.has("classifier")){ |
68 | | - if(it.asJsonObject.get("maven2").asJsonObject.get("classifier").asString.equals("sources")){ |
69 | | - return@firstOrNull false // Check that this is not a sources jar file |
70 | | - } |
71 | | - |
72 | | - if(it.asJsonObject.get("maven2").asJsonObject.get("classifier").asString.equals("javadoc")){ |
73 | | - return@firstOrNull false // Check that this is not a javadoc jar file |
| 29 | + .get("latest") |
| 30 | + .asString |
| 31 | + if(result.endsWith("-SNAPSHOT")) { |
| 32 | + parseSnapshotVersion(Dependency(dependency.group, dependency.artifactId, result, dependency.md5Hash)) |
| 33 | + }else { |
| 34 | + result |
74 | 35 | } |
| 36 | + }catch (e: Exception){ |
| 37 | + null |
75 | 38 | } |
| 39 | + }else{ |
| 40 | + dependency.version |
| 41 | + } ?: return null |
76 | 42 |
|
77 | | - if(!it.asJsonObject.get("maven2").asJsonObject.get("artifactId").asString.equals(dependency.artifactId)) { |
78 | | - return@firstOrNull false // Check that this is the correct artifact |
79 | | - } |
80 | | - |
81 | | - if(!it.asJsonObject.get("contentType").asString.equals("application/java-archive")){ |
82 | | - return@firstOrNull false // Check that this is a jar file |
83 | | - } |
84 | | - |
85 | | - return@firstOrNull true |
86 | | - }?.asJsonObject ?: return null |
87 | | - val json = JsonObject() |
88 | | - json.addProperty("url", found.get("downloadUrl").asString) |
89 | | - if(found.has("md5")){ |
90 | | - json.addProperty("md5", found.get("md5").asString) |
91 | | - } |
92 | | - json.addProperty("version", version) |
93 | | - return json |
| 43 | + val result = url + String.format(mavenUrlFormat, |
| 44 | + rewriteEscaping(dependency.group).replace('.', '/'), |
| 45 | + rewriteEscaping(dependency.artifactId), |
| 46 | + dependency.version, |
| 47 | + rewriteEscaping(dependency.artifactId), |
| 48 | + parsedVersion |
| 49 | + ) |
| 50 | + URL(result) |
| 51 | + result |
| 52 | + }catch (e: Exception){ |
| 53 | + null |
94 | 54 | } |
| 55 | + } |
| 56 | + |
| 57 | + private fun rewriteEscaping(data: String) = data.replace("{}", ".") |
95 | 58 |
|
96 | | - return null |
| 59 | + private fun parseSnapshotVersion(dependency: Dependency): String? = try { |
| 60 | + val json = JsonParser.parseString(XML.toJSONObject(URL("$url/${rewriteEscaping(dependency.group).replace('.','/')}/${dependency.artifactId}/${dependency.version}/maven-metadata.xml").readText()).toString()) |
| 61 | + .asJsonObject |
| 62 | + .getAsJsonObject("metadata") |
| 63 | + .getAsJsonObject("versioning") |
| 64 | + .getAsJsonObject("snapshot") |
| 65 | + dependency.version.replace("-SNAPSHOT", "-${json.get("timestamp").asString}-${json.get("buildNumber").asString}") |
| 66 | + }catch (e: Exception){ |
| 67 | + null |
97 | 68 | } |
98 | 69 | } |
0 commit comments