/
MavenCentral.scala
74 lines (58 loc) · 2.54 KB
/
MavenCentral.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package utils
import java.io.{File, InputStream}
import java.net.{URLEncoder, URL}
import java.nio.file.Files
import java.util.jar.JarInputStream
import org.webjars.WebJarAssetLocator
import play.api.Play
import play.api.libs.ws.WSResponse
import scala.util.{Success, Try}
object MavenCentral {
lazy val tempDir: File = Files.createTempDirectory("webjars").toFile
val primaryBaseJarUrl = Play.current.configuration.getString("webjars.jarUrl.primary").get
val fallbackBaseJarUrl = Play.current.configuration.getString("webjars.jarUrl.fallback").get
def getFile(groupId: String, artifactId: String, version: String): Try[(JarInputStream, InputStream)] = {
val tmpFile = new File(tempDir, s"$groupId-$artifactId-$version.jar")
if (tmpFile.exists()) {
val fileInputStream = Files.newInputStream(tmpFile.toPath)
Success((new JarInputStream(fileInputStream), fileInputStream))
}
else {
val fileInputStreamFuture = getFileInputStream(primaryBaseJarUrl, groupId, artifactId, version).recoverWith {
case _ =>
getFileInputStream(fallbackBaseJarUrl, groupId, artifactId, version)
}
fileInputStreamFuture.map { fileInputStream =>
// todo: not thread safe!
// write to the fs
Files.copy(fileInputStream, tmpFile.toPath)
fileInputStream.close()
val tmpFileInputStream = Files.newInputStream(tmpFile.toPath)
// read it from the fs since we've drained the http response
(new JarInputStream(tmpFileInputStream), tmpFileInputStream)
}
}
}
def getFileInputStream(baseJarUrl: String, groupId: String, artifactId: String, version: String): Try[InputStream] = {
Try {
val url = new URL(baseJarUrl.format(groupId.replace(".", "/"), artifactId, URLEncoder.encode(version, "UTF-8"), artifactId, URLEncoder.encode(version, "UTF-8")))
url.openConnection().getInputStream
}
}
def fetchFileList(groupId: String, artifactId: String, version: String): Try[List[String]] = {
getFile(groupId, artifactId, version).map { case (jarInputStream, inputStream) =>
val webJarFiles = Stream.continually(jarInputStream.getNextJarEntry).
takeWhile(_ != null).
filterNot(_.isDirectory).
map(_.getName).
filter(_.startsWith(WebJarAssetLocator.WEBJARS_PATH_PREFIX)).
toList
jarInputStream.close()
inputStream.close()
webJarFiles
}
}
case class UnexpectedResponseException(response: WSResponse) extends RuntimeException {
override def getMessage: String = response.statusText
}
}