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

Merge pull request #174 from dmitry-zhuravlev/proxy-settings-support

+ Proxy support in kobalt-settings: <proxy>      <host>somehost</host>      <port>3128</port>      <type>http</type>      <!--<type>https</type>-->  </proxy>
This commit is contained in:
Cedric Beust 2016-04-24 07:08:51 -08:00
commit 575adc4424
7 changed files with 50 additions and 4 deletions

View file

@ -30,6 +30,8 @@ fun plugins(vararg dependencies : String) {
} }
} }
data class ProxyConfig(var host: String = "", var port: Int = 0, val type: String = "")
data class HostConfig(var url: String = "", var username: String? = null, var password: String? = null) { data class HostConfig(var url: String = "", var username: String? = null, var password: String? = null) {
fun hasAuth() : Boolean { fun hasAuth() : Boolean {
return (! username.isNullOrBlank()) && (! password.isNullOrBlank()) return (! username.isNullOrBlank()) && (! password.isNullOrBlank())

View file

@ -3,8 +3,11 @@ package com.beust.kobalt.api
import com.beust.kobalt.Constants import com.beust.kobalt.Constants
import com.beust.kobalt.HostConfig import com.beust.kobalt.HostConfig
import com.beust.kobalt.Plugins import com.beust.kobalt.Plugins
import com.beust.kobalt.ProxyConfig
import com.google.inject.Injector import com.google.inject.Injector
import org.eclipse.aether.repository.Proxy
import java.io.InputStream import java.io.InputStream
import java.net.InetSocketAddress
import java.time.Duration import java.time.Duration
import java.util.* import java.util.*
@ -14,6 +17,18 @@ class Kobalt {
var context: KobaltContext? = null var context: KobaltContext? = null
val proxyConfig = with(Kobalt.context?.settings?.proxy) {
if (this != null) {
ProxyConfig(host, port.toIntOr(0), type)
} else null
}
fun String.toIntOr(defaultValue: Int) = try { //TODO can be extracted to some global Utils
toInt()
} catch(e: NumberFormatException) {
defaultValue
}
/** /**
* @return the repos calculated from the following places: * @return the repos calculated from the following places:
* - Either repos specified in settings.xml or from Constants.DEFAULT_REPOS * - Either repos specified in settings.xml or from Constants.DEFAULT_REPOS
@ -99,3 +114,10 @@ class Kobalt {
fun findPlugin(name: String) = Plugins.findPlugin(name) fun findPlugin(name: String) = Plugins.findPlugin(name)
} }
} }
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

@ -22,6 +22,9 @@ class KobaltSettingsXml {
@XmlElement(name = "default-repos") @JvmField @XmlElement(name = "default-repos") @JvmField
var defaultRepos: DefaultReposXml? = null var defaultRepos: DefaultReposXml? = null
@XmlElement(name = "proxy") @JvmField
var proxy: ProxyXml? = null
@XmlElement(name = "kobalt-compiler-version") @JvmField @XmlElement(name = "kobalt-compiler-version") @JvmField
var kobaltCompilerVersion: String = "1.0.0" var kobaltCompilerVersion: String = "1.0.0"
@ -29,6 +32,17 @@ class KobaltSettingsXml {
var kobaltCompilerRepo: String? = null var kobaltCompilerRepo: String? = null
} }
class ProxyXml {
@XmlElement @JvmField
var host: String = ""
@XmlElement @JvmField
var port: String = ""
@XmlElement @JvmField
var type: String = ""
}
class DefaultReposXml { class DefaultReposXml {
@XmlElement @JvmField @XmlElement @JvmField
var repo: List<String> = arrayListOf() var repo: List<String> = arrayListOf()
@ -46,6 +60,8 @@ class KobaltSettings @Inject constructor(val xmlFile: KobaltSettingsXml) {
val defaultRepos = xmlFile.defaultRepos?.repo val defaultRepos = xmlFile.defaultRepos?.repo
val proxy = xmlFile.proxy
var kobaltCompilerVersion = xmlFile.kobaltCompilerVersion var kobaltCompilerVersion = xmlFile.kobaltCompilerVersion
var kobaltCompilerRepo = xmlFile.kobaltCompilerRepo var kobaltCompilerRepo = xmlFile.kobaltCompilerRepo

View file

@ -1,6 +1,8 @@
package com.beust.kobalt.maven package com.beust.kobalt.maven
import com.beust.kobalt.KobaltException import com.beust.kobalt.KobaltException
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.toProxy
import com.beust.kobalt.misc.CountingFileRequestBody import com.beust.kobalt.misc.CountingFileRequestBody
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import okhttp3.* import okhttp3.*
@ -19,7 +21,7 @@ class Http {
} }
fun get(user: String?, password: String?, url: String) : Response { fun get(user: String?, password: String?, url: String) : Response {
val client = OkHttpClient() val client = OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build()
val request = Request.Builder().url(url) val request = Request.Builder().url(url)
if (user != null) { if (user != null) {
request.header("Authorization", Credentials.basic(user, password)) request.header("Authorization", Credentials.basic(user, password))
@ -73,7 +75,7 @@ class Http {
.build() .build()
log(2, "Uploading $file to $url") log(2, "Uploading $file to $url")
val response = OkHttpClient().newCall(request).execute() val response = OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build().newCall(request).execute()
if (! response.isSuccessful) { if (! response.isSuccessful) {
error(response) error(response)
} else { } else {

View file

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

View file

@ -54,7 +54,7 @@ class CountingFileRequestBody(val file: File, val contentType: String,
// // .post(requestBody) // // .post(requestBody)
// .build(); // .build();
// //
// val response = OkHttpClient().newCall(request).execute() // val response = OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build().newCall(request).execute()
// if (! response.isSuccessful) { // if (! response.isSuccessful) {
// println("ERROR") // println("ERROR")
// } else { // } else {

View file

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