1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Refactor some proxy code.

/cc @dmitry-zhuravlev
This commit is contained in:
Cedric Beust 2016-04-26 03:06:17 -08:00
parent 80066df24e
commit 9ecc603c24
5 changed files with 18 additions and 28 deletions

View file

@ -5,7 +5,9 @@ import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.dependency.FileDependency
import org.eclipse.aether.repository.Proxy
import java.io.File
import java.net.InetSocketAddress
@Directive
fun homeDir(vararg dirs: String) : String = SystemProperties.homeDir +
@ -30,7 +32,11 @@ fun plugins(vararg dependencies : String) {
}
}
data class ProxyConfig(var host: String = "", var port: Int = 0, val type: String = "")
data class ProxyConfig(var host: String = "", var port: Int = 0, val type: String = "") {
fun toProxy() = java.net.Proxy(java.net.Proxy.Type.HTTP, InetSocketAddress(host, port))
fun toAetherProxy() = Proxy(type, host, port) // TODO make support for proxy auth
}
data class HostConfig(var url: String = "", var username: String? = null, var password: String? = null) {
fun hasAuth() : Boolean {

View file

@ -6,10 +6,8 @@ import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log
import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.aether.repository.Proxy
import java.io.File
import java.io.FileInputStream
import java.net.InetSocketAddress
import javax.xml.bind.JAXBContext
import javax.xml.bind.annotation.XmlElement
import javax.xml.bind.annotation.XmlRootElement
@ -63,22 +61,17 @@ class KobaltSettings @Inject constructor(val xmlFile: KobaltSettingsXml) {
val defaultRepos = xmlFile.defaultRepos?.repo
val proxy = xmlFile.proxy
val proxyConfig = with(proxy) {
if (this != null) {
com.beust.kobalt.ProxyConfig(host, port.toIntOr(0), type)
} else null
}
fun String.toIntOr(defaultValue: Int) = try { //TODO can be extracted to some global Utils
toInt()
val proxyConfig = with(xmlFile.proxy) {
fun toIntOr(s: String, defaultValue: Int) = try { //TODO can be extracted to some global Utils
s.toInt()
} catch(e: NumberFormatException) {
defaultValue
}
if (this != null) {
ProxyConfig(host, toIntOr(port, 0), type)
} else null
}
var kobaltCompilerVersion = xmlFile.kobaltCompilerVersion
var kobaltCompilerRepo = xmlFile.kobaltCompilerRepo
@ -104,9 +97,3 @@ class KobaltSettings @Inject constructor(val xmlFile: KobaltSettingsXml) {
}
}
fun ProxyConfig?.toProxy() = if (this != null) {
java.net.Proxy(java.net.Proxy.Type.HTTP, InetSocketAddress(host, port))
} else null
fun ProxyConfig?.toAetherProxy() = if (this != null) Proxy(type, host, port) else null //TODO make support for proxy auth

View file

@ -2,7 +2,6 @@ package com.beust.kobalt.maven
import com.beust.kobalt.KobaltException
import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.internal.toProxy
import com.beust.kobalt.misc.CountingFileRequestBody
import com.beust.kobalt.misc.log
import com.google.inject.Inject
@ -22,7 +21,7 @@ class Http @Inject constructor(val settings:KobaltSettings) {
}
fun get(user: String?, password: String?, url: String) : Response {
val client = OkHttpClient.Builder().proxy(settings.proxyConfig.toProxy()).build()
val client = OkHttpClient.Builder().proxy(settings.proxyConfig?.toProxy()).build()
val request = Request.Builder().url(url)
if (user != null) {
request.header("Authorization", Credentials.basic(user, password))
@ -76,7 +75,7 @@ class Http @Inject constructor(val settings:KobaltSettings) {
.build()
log(2, "Uploading $file to $url")
val response = OkHttpClient.Builder().proxy(settings.proxyConfig.toProxy()).build().newCall(request).execute()
val response = OkHttpClient.Builder().proxy(settings.proxyConfig?.toProxy()).build().newCall(request).execute()
if (! response.isSuccessful) {
error(response)
} else {

View file

@ -6,7 +6,6 @@ import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.homeDir
import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.internal.KobaltSettingsXml
import com.beust.kobalt.internal.toAetherProxy
import com.beust.kobalt.maven.CompletedFuture
import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.misc.KobaltLogger
@ -81,7 +80,7 @@ class Aether(val localRepo: File, val settings: KobaltSettings) {
private val kobaltRepositories : List<RemoteRepository>
get() = Kobalt.repos.map {
RemoteRepository.Builder("maven", "default", it.url)
.setProxy(settings.proxyConfig.toAetherProxy())
.setProxy(settings.proxyConfig?.toAetherProxy())
// .setSnapshotPolicy(RepositoryPolicy(false, null, null))
.build()
}

View file

@ -3,7 +3,6 @@ package com.beust.kobalt.misc
import com.beust.kobalt.KobaltException
import com.beust.kobalt.internal.DocUrl
import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.internal.toProxy
import com.beust.kobalt.maven.Http
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
@ -60,7 +59,7 @@ class GithubApi2 @Inject constructor(
// Read only Api
//
private val service = Retrofit.Builder()
.client(OkHttpClient.Builder().proxy(settings.proxyConfig.toProxy()).build())
.client(OkHttpClient.Builder().proxy(settings.proxyConfig?.toProxy()).build())
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.build()