Skip to content

Commit

Permalink
prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jun 15, 2024
1 parent cb8f3e8 commit 5dcefde
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 149 deletions.
4 changes: 2 additions & 2 deletions site/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ java {
}

application {
mainClass = "xyz.wagyourtail.jvmdg.maven.MainKt"
mainClass = "xyz.wagyourtail.jvmdg.site.MainKt"
}

tasks.getByName("run", JavaExec::class) {
Expand Down Expand Up @@ -89,7 +89,7 @@ val myShadowJar by tasks.creating(ShadowJar::class) {
attributes.putAll(
mapOf(
"Implementation-Version" to project.version,
"Main-Class" to "xyz.wagyourtail.jvmdg.maven.MainKt"
"Main-Class" to "xyz.wagyourtail.jvmdg.site.MainKt"
)
)
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.wagyourtail.jvmdg.maven
package xyz.wagyourtail.jvmdg.site

import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.http.*
Expand All @@ -12,13 +12,21 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.html.*
import xyz.wagyourtail.jvmdg.maven.html.About
import xyz.wagyourtail.jvmdg.maven.html.Maven
import xyz.wagyourtail.jvmdg.maven.html.FolderContents
import xyz.wagyourtail.jvmdg.maven.transform.JarTransformer
import xyz.wagyourtail.jvmdg.maven.transform.ModuleTransformer
import xyz.wagyourtail.jvmdg.maven.transform.PomTransformer
import xyz.wagyourtail.jvmdg.site.maven.Cache
import xyz.wagyourtail.jvmdg.site.maven.MavenClient
import xyz.wagyourtail.jvmdg.site.maven.Settings
import xyz.wagyourtail.jvmdg.site.html.About
import xyz.wagyourtail.jvmdg.site.maven.html.Maven
import xyz.wagyourtail.jvmdg.site.maven.html.FolderContents
import xyz.wagyourtail.jvmdg.site.maven.transform.JarTransformer
import xyz.wagyourtail.jvmdg.site.maven.transform.MetadataTransformer
import xyz.wagyourtail.jvmdg.site.maven.transform.ModuleTransformer
import xyz.wagyourtail.jvmdg.site.maven.transform.PomTransformer
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream
import kotlin.time.Duration.Companion.days

private val LOGGER = KotlinLogging.logger { }
Expand Down Expand Up @@ -53,9 +61,9 @@ fun main() {
call.respondHtmlTemplate(Maven()) {}
}
// list existing mirrored content
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>(?:.+/)?)")) {
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>(?:.+/)?)")) {
val major = call.parameters["major"]!!.toInt()
val mirrorPath = call.parameters["mirrorPath"]!!
val mirrorPath = call.parameters["mirrorPath"]!!.replace("/jvmdg-${major}.", "/")
if (Settings.blacklist.any { mirrorPath.startsWith(it) }) {
call.respond(HttpStatusCode.NotFound)
} else {
Expand All @@ -69,9 +77,9 @@ fun main() {
}

// maven pom transformer
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>.+\\.pom)")) {
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+\\.pom)")) {
val major = call.parameters["major"]!!.toInt()
val mirrorPath = call.parameters["mirrorPath"]!!
val mirrorPath = call.parameters["mirrorPath"]!!.replace("/jvmdg-${major}.", "/")
if (Settings.blacklist.any { mirrorPath.startsWith(it) }) {
call.respond(HttpStatusCode.NotFound)
} else {
Expand All @@ -97,9 +105,9 @@ fun main() {
}

// gradle module transformer
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>.+\\.module)")) {
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+\\.module)")) {
val major = call.parameters["major"]!!.toInt()
val mirrorPath = call.parameters["mirrorPath"]!!
val mirrorPath = call.parameters["mirrorPath"]!!.replace("/jvmdg-${major}.", "/")
if (Settings.blacklist.any { mirrorPath.startsWith(it) }) {
call.respond(HttpStatusCode.NotFound)
} else {
Expand All @@ -125,32 +133,39 @@ fun main() {
}

// maven metadata, don't cache
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>.+\\.xml)")) {
val mirrorPath = call.parameters["mirrorPath"]!!
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+/maven-metadata\\.xml)")) {
val major = call.parameters["major"]!!.toInt()
val mirrorPath = call.parameters["mirrorPath"]!!.replace("/jvmdg-${major}.", "/")
if (Settings.blacklist.any { mirrorPath.startsWith(it) }) {
call.respond(HttpStatusCode.NotFound)
} else {
val content = MavenClient.get(mirrorPath)
Cache.recordPath(content.toPath())
call.respondOutputStream {
content.inputStream().use { it.copyTo(this) }
val content = MavenClient.resolveMetadata(mirrorPath)
if (content == null) {
call.respond(HttpStatusCode.NotFound)
} else {
call.respondOutputStream {
MetadataTransformer.transform(content, major)
// write document to output stream
val transformer = TransformerFactory.newInstance().newTransformer()
transformer.transform(DOMSource(content), StreamResult(this))
}
}
}
}

// don't include signature or checksums
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>.+\\.(?:md5|sha1|sha256|sha512))")) {
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+\\.(?:md5|sha1|sha256|sha512))")) {
call.respond(HttpStatusCode.NotFound)
}
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>.+\\.asc)")) {
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+\\.asc)")) {
call.respond(HttpStatusCode.NotFound)
}

// jar transformer
get(Regex("maven/(?<major>\\d+)/(?<mirrorPath>.+\\.jar)")) {
get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+\\.jar)")) {
try {
val major = call.parameters["major"]!!.toInt()
val mirrorPath = call.parameters["mirrorPath"]!!
val mirrorPath = call.parameters["mirrorPath"]!!.replace("/jvmdg-${major}.", "/")
if (Settings.blacklist.any { mirrorPath.startsWith(it) }) {
call.respond(HttpStatusCode.NotFound)
} else {
Expand Down Expand Up @@ -184,7 +199,30 @@ fun main() {
}
}
}

get(Regex("maven/jvmdg-(?<major>\\d+)/(?<mirrorPath>.+)")) {
val major = call.parameters["major"]!!.toInt()
val mirrorPath = call.parameters["mirrorPath"]!!.replace("/jvmdg-${major}.", "/")
if (Settings.blacklist.any { mirrorPath.startsWith(it) }) {
call.respond(HttpStatusCode.NotFound)
} else {
val content = Cache.getOrPut("${major}/${mirrorPath}") { path ->
MavenClient.get(mirrorPath).let {
Cache.recordPath(it.toPath())
it.inputStream().use { it.copyTo(path.outputStream()) }
}
}
if (content != null) {
call.respondOutputStream {
content.inputStream().use { it.copyTo(this) }
headers {
cacheControl(CacheControl.MaxAge(7.days.inWholeSeconds.toInt()))
}
}
} else {
call.respond(HttpStatusCode.NotFound)
}
}
}
}
}.start(wait = true)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.wagyourtail.jvmdg.maven.html
package xyz.wagyourtail.jvmdg.site.html

import io.ktor.server.html.*
import kotlinx.html.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package xyz.wagyourtail.jvmdg.maven
package xyz.wagyourtail.jvmdg.site.maven

import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import xyz.wagyourtail.jvmdg.maven.lock.StringKeyLock
import xyz.wagyourtail.jvmdg.site.maven.lock.StringKeyLock
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.wagyourtail.jvmdg.maven
package xyz.wagyourtail.jvmdg.site.maven

import org.apache.maven.repository.internal.MavenRepositorySystemUtils
import org.eclipse.aether.DefaultRepositorySystemSession
Expand All @@ -21,10 +21,18 @@ import org.eclipse.aether.spi.connector.transport.TransporterFactory
import org.eclipse.aether.transport.file.FileTransporterFactory
import org.eclipse.aether.transport.http.HttpTransporterFactory
import org.eclipse.aether.util.repository.AuthenticationBuilder
import xyz.wagyourtail.jvmdg.maven.impl.ConsoleRepositoryListener
import xyz.wagyourtail.jvmdg.maven.impl.ConsoleTransferListener
import org.w3c.dom.Document
import xyz.wagyourtail.jvmdg.site.maven.impl.ConsoleRepositoryListener
import xyz.wagyourtail.jvmdg.site.maven.impl.ConsoleTransferListener
import java.io.File
import java.io.InputStream
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import javax.xml.parsers.DocumentBuilderFactory
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration

object MavenClient {

Expand Down Expand Up @@ -90,6 +98,25 @@ object MavenClient {
return repo.resolveArtifact(session, request).artifact.file
}

val metadataClient = HttpClient.newBuilder()
.connectTimeout(5.seconds.toJavaDuration())
.build()

fun resolveMetadata(path: String): Document? {
for (repo in repositories) {
var url = repo.url
if (!url.endsWith("/")) {
url += "/"
}
url += path
val request = metadataClient.send(HttpRequest.newBuilder(URI.create(url)).build(), HttpResponse.BodyHandlers.ofInputStream())
if (request.statusCode() == 200) {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(request.body())
}
}
return null
}

fun resolveDeps(artifact: DefaultArtifact): List<File> {
val depRequest = DependencyRequest()
depRequest.collectRequest = CollectRequest(Dependency(artifact, "compile"), repositories)
Expand All @@ -102,7 +129,7 @@ object MavenClient {
val folder = path.substringBeforeLast("/")
val artifactFolder = folder.substringBeforeLast("/")
val versionNumber = folder.substringAfterLast("/")
val artifactName = artifactFolder.substringAfterLast("/")
var artifactName = artifactFolder.substringAfterLast("/")
val group = artifactFolder.substringBeforeLast("/")
val classifier = path.substringAfterLast("/").let {
if (!it.startsWith("$artifactName-$versionNumber-")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.wagyourtail.jvmdg.maven
package xyz.wagyourtail.jvmdg.site.maven

import java.util.*
import kotlin.time.Duration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package xyz.wagyourtail.jvmdg.site.maven.html

import io.ktor.server.html.*
import kotlinx.html.*

class FolderContents(
val majorVersion: Int,
val folder: String,
val files: List<String>
) : Template<HTML> {

override fun HTML.apply() {
head {
title("JvmDowngrader Maven Mirror: jvmdg-${majorVersion}/$folder")
}
body {
h1 {
+folder
}
for (file in files) {
if (file.startsWith(folder.removeSuffix("/").replace("/", "."))) {
p {
a(href = "/maven/jvmdg-$majorVersion/${folder}jvmdg-$majorVersion.$file") {
+"jvmdg-$majorVersion.$file"
}
}
}
p {
a(href = "/maven/jvmdg-$majorVersion/$folder$file") {
+file
}
}
}
}
}

}
Loading

0 comments on commit 5dcefde

Please sign in to comment.