@@ -10,9 +10,84 @@ import org.apache.http.client.methods.HttpPost
1010import org.apache.http.entity.StringEntity
1111import org.apache.http.impl.client.HttpClients
1212import org.apache.http.util.EntityUtils
13+ import xyz.bluspring.unitytranslate.UnityTranslate
14+ import java.io.BufferedInputStream
15+ import java.io.BufferedOutputStream
16+ import java.net.HttpURLConnection
17+ import java.net.URL
1318
1419object HttpHelper {
20+ private var hasApache: Boolean? = null
21+
1522 fun post (uri : String , body : JsonObject , headers : Map <String , String > = mapOf()): JsonElement {
23+ if (hasApache == null ) {
24+ try {
25+ // This is literally only here because of Forge.
26+ Class .forName(" org.apache.http.impl.client.HttpClients" )
27+ Class .forName(" org.apache.http.HttpHeaders" )
28+ Class .forName(" org.apache.http.util.EntityUtils" )
29+ Class .forName(" org.apache.commons.logging.LogFactory" )
30+ hasApache = true
31+ } catch (e: Throwable ) {
32+ UnityTranslate .logger.error(" Detected that Apache HTTP support is unavailable! Translations may fail after a certain period of time." )
33+ UnityTranslate .logger.error(" For more information, view: https://github.com/BluSpring/UnityTranslate/issues/4" )
34+ e.printStackTrace()
35+ hasApache = false
36+ }
37+ }
38+
39+ return if (hasApache == true ) {
40+ postApache(uri, body, headers)
41+ } else {
42+ postJava(uri, body, headers)
43+ }
44+ }
45+
46+ fun postJava (uri : String , body : JsonObject , headers : Map <String , String > = mapOf()): JsonElement {
47+ val url = URL (uri)
48+ var connection: HttpURLConnection ? = null
49+
50+ val data = body.toString().toByteArray(Charsets .UTF_8 )
51+
52+ try {
53+ connection = url.openConnection() as HttpURLConnection
54+ connection.requestMethod = " POST"
55+ connection.connectTimeout = 60_000
56+ connection.readTimeout = 60_000
57+
58+ connection.setRequestProperty(" Accept" , " application/json" )
59+ connection.setRequestProperty(" Content-Type" , " application/json" )
60+ headers.forEach { (key, value) ->
61+ connection.setRequestProperty(key, value)
62+ }
63+
64+ connection.doOutput = true
65+ connection.setFixedLengthStreamingMode(data.size)
66+
67+ val outputStream = BufferedOutputStream (connection.outputStream)
68+
69+ outputStream.write(data)
70+ outputStream.flush()
71+ outputStream.close()
72+
73+ if (connection.responseCode / 100 != 2 ) {
74+ throw Exception (" Failed to load $uri (code: ${connection.responseCode} )" )
75+ } else {
76+ val inputStream = BufferedInputStream (connection.inputStream)
77+ val reader = inputStream.bufferedReader(Charsets .UTF_8 )
78+ val result = JsonParser .parseReader(reader)
79+
80+ reader.close()
81+ inputStream.close()
82+
83+ return result
84+ }
85+ } finally {
86+ connection?.disconnect()
87+ }
88+ }
89+
90+ fun postApache (uri : String , body : JsonObject , headers : Map <String , String > = mapOf()): JsonElement {
1691 HttpClients .createDefault().use { httpClient ->
1792 val request = HttpPost (uri)
1893 request.config = RequestConfig .custom()
0 commit comments