Skip to content

Commit ac7405f

Browse files
committed
try to fix internet problem
1 parent d021558 commit ac7405f

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/main/kotlin/org/tabooproject/intellij/Template.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package org.tabooproject.intellij
33
import freemarker.cache.StringTemplateLoader
44
import freemarker.template.Configuration
55
import freemarker.template.Template
6-
import okhttp3.OkHttpClient
76
import org.tabooproject.intellij.step.ConfigurationPropertiesStep
87
import org.tabooproject.intellij.step.TEMPLATE_DOWNLOAD_MIRROR
98
import java.io.IOException
109
import java.io.StringReader
1110
import java.io.StringWriter
1211
import java.nio.charset.StandardCharsets
1312
import java.nio.file.Files
13+
import java.util.concurrent.TimeUnit
1414
import java.util.zip.ZipInputStream
1515

1616
data class TemplateFile(val node: String)
@@ -34,7 +34,12 @@ object Template {
3434
}
3535

3636
fun downloadAndUnzipFile(baseDir: String, url: String = getTemplateDownloadUrl()) {
37-
val response = OkHttpClient()
37+
val client = createOkHttpClientWithSystemProxy {
38+
connectTimeout(10, TimeUnit.SECONDS)
39+
readTimeout(10, TimeUnit.SECONDS)
40+
}
41+
42+
val response = client
3843
.newCall(getRequest(url))
3944
.execute()
4045
.takeIf { it.isSuccessful } ?: throw IOException("Failed to download file")

src/main/kotlin/org/tabooproject/intellij/Utils.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.tabooproject.intellij
22

3+
import okhttp3.OkHttpClient
34
import okhttp3.Request
5+
import java.net.InetSocketAddress
6+
import java.net.Proxy
47
import java.nio.file.Files
58
import java.nio.file.Path
69
import java.nio.file.Paths
@@ -19,6 +22,25 @@ fun createFileWithDirectories(baseDir: String, relativePath: String): Path? {
1922
return null
2023
}
2124

25+
fun createOkHttpClientWithSystemProxy(block: OkHttpClient.Builder.() -> Unit = {}): OkHttpClient {
26+
val proxyHost = System.getProperty("http.proxyHost")
27+
val proxyPort = System.getProperty("http.proxyPort")
28+
29+
val clientBuilder = OkHttpClient.Builder().apply {
30+
block(this)
31+
}
32+
33+
if (!proxyHost.isNullOrEmpty() && !proxyPort.isNullOrEmpty()) {
34+
val proxy = Proxy(
35+
Proxy.Type.HTTP,
36+
InetSocketAddress(proxyHost, proxyPort.toInt())
37+
)
38+
clientBuilder.proxy(proxy)
39+
}
40+
41+
return clientBuilder.build()
42+
}
43+
2244
fun getRequest(url: String): Request {
2345
return Request.Builder().url(url).build()
2446
}

src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package org.tabooproject.intellij.step
22

33
import com.intellij.ide.util.projectWizard.ModuleWizardStep
4+
import com.intellij.ui.dsl.builder.columns
45
import com.intellij.ui.dsl.builder.panel
5-
import okhttp3.OkHttpClient
66
import org.tabooproject.intellij.component.AddDeleteModuleListPanel
7+
import org.tabooproject.intellij.createOkHttpClientWithSystemProxy
78
import org.tabooproject.intellij.getRequest
9+
import org.tabooproject.intellij.util.LOCAL_MODULES
810
import java.io.IOException
911
import java.util.concurrent.TimeUnit
1012
import javax.swing.JComponent
1113

1214
private fun fetchAndParseModules(
1315
url: String = "https://raw.githubusercontent.com/TabooLib/taboolib-gradle-plugin/master/src/main/kotlin/io/izzel/taboolib/gradle/Standards.kt",
1416
): List<String>? {
15-
val client = OkHttpClient.Builder()
16-
// TODO: detect user's proxy and make request through it
17-
.connectTimeout(5, TimeUnit.SECONDS)
18-
.readTimeout(5, TimeUnit.SECONDS)
19-
.build()
17+
val client = createOkHttpClientWithSystemProxy {
18+
connectTimeout(5, TimeUnit.SECONDS)
19+
readTimeout(5, TimeUnit.SECONDS)
20+
}
2021
val request = getRequest(url)
2122

2223
return try {
@@ -53,7 +54,7 @@ data class ConfigurationProperty(
5354
var name: String = "untitled",
5455
var mainClass: String = "org.example.untitled.UntitledPlugin",
5556
var version: String = "1.0-SNAPSHOT",
56-
var mirrorIndex: String = "GitHub",
57+
var mirrorIndex: String = "github.com",
5758
val modules: MutableList<String> = mutableListOf<String>().apply {
5859
add("UNIVERSAL")
5960
add("BUKKIT_ALL")
@@ -109,6 +110,7 @@ class ConfigurationPropertiesStep : ModuleWizardStep() {
109110
comboBox(TEMPLATE_DOWNLOAD_MIRROR.keys)
110111
.apply {
111112
component.selectedIndex = 0
113+
component.columns(20)
112114
}.onChanged {
113115
property.mirrorIndex = it.selectedItem as String
114116
}

0 commit comments

Comments
 (0)