Fixed URLConnection not adding headers to the request.

* In this update, we've replaced the `URLConnection` with the OkHttp library, and now headers are being correctly passed in the request. As a result, the Zim file is successfully downloading.
* To integrate OkHttp into our Gradle project, we've moved the OkHttp dependency to our `buildSrc` folder.
This commit is contained in:
MohitMali 2023-10-10 14:11:30 +05:30 committed by Kelson
parent 5e810391f0
commit 11c5b3caa2
4 changed files with 41 additions and 27 deletions

View File

@ -23,6 +23,7 @@ dependencies {
}
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0")
implementation("com.googlecode.json-simple:json-simple:1.1")
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation(gradleApi())
implementation(localGroovy())

View File

@ -79,11 +79,6 @@ object Libs {
const val mockwebserver: String = "com.squareup.okhttp3:mockwebserver:" +
Versions.com_squareup_okhttp3
/**
* https://square.github.io/okhttp/
*/
const val okhttp: String = "com.squareup.okhttp3:okhttp:" + Versions.com_squareup_okhttp3
/**
* https://kotlinlang.org/
*/

View File

@ -185,7 +185,6 @@ class AllProjectConfigurer {
implementation(Libs.navigation_fragment_ktx)
implementation(Libs.navigation_ui_ktx)
androidTestImplementation(Libs.navigation_testing)
implementation(Libs.okhttp)
implementation(Libs.logging_interceptor)
implementation(Libs.retrofit)
implementation(Libs.adapter_rxjava2)

View File

@ -6,10 +6,13 @@ import custom.createPublisher
import custom.transactionWithCommit
import plugin.KiwixConfigurationPlugin
import java.net.URI
import java.net.URL
import java.net.URLDecoder
import java.util.Locale
import java.util.Base64
import java.io.FileOutputStream
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.ResponseBody
plugins {
android
@ -51,36 +54,52 @@ fun ProductFlavor.createDownloadTask(file: File): Task {
doLast {
if (!file.exists()) {
file.createNewFile()
URL(fetchUrl()).openStream().use {
it.copyTo(file.outputStream())
OkHttpClient().newCall(fetchRequest()).execute().use { response ->
if (response.isSuccessful) {
response.body?.let { responseBody ->
writeZimFileData(responseBody, file)
}
} else {
throw RuntimeException(
"Download Failed. Error: ${response.message}\n" +
" Status Code: ${response.code}"
)
}
}
}
}
}
}
fun ProductFlavor.fetchUrl(): String {
fun ProductFlavor.fetchRequest(): Request {
val urlString = buildConfigFields["ZIM_URL"]!!.value.replace("\"", "")
var secretKey = ""
val url = if (urlString.isAuthenticationUrl) {
secretKey = urlString.secretKey
URI.create(urlString.removeAuthenticationFromUrl).toURL()
} else {
URI.create(urlString).toURL()
}
return url
.openConnection()
.apply {
if (urlString.isAuthenticationUrl) {
setRequestProperty(
return if (urlString.isAuthenticationUrl) {
Request.Builder()
.url(URI.create(urlString.removeAuthenticationFromUrl).toURL())
.header(
"Authorization",
"Basic ${Base64.getEncoder().encodeToString(System.getenv(secretKey).toByteArray())}"
"Basic " +
Base64.getEncoder().encodeToString(System.getenv(urlString.secretKey).toByteArray())
)
.build()
} else {
Request.Builder()
.url(URI.create(urlString).toURL())
.build()
}
}
fun writeZimFileData(responseBody: ResponseBody, file: File) {
FileOutputStream(file).use { outputStream ->
responseBody.byteStream().use { inputStream ->
val buffer = ByteArray(4096)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
outputStream.write(buffer, 0, bytesRead)
}
outputStream.flush()
}
connect()
getInputStream()
}.let {
it.getHeaderField("Location")?.replace("https", "http") ?: it.url.toString()
}
}