-
-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#450 ported OkHttp module to Kotlin and upgraded to OkHttp 4.0.1
- Loading branch information
Showing
10 changed files
with
154 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpBodyWriter.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpBodyWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package net.gotev.uploadservice.okhttp | ||
|
||
import net.gotev.uploadservice.http.BodyWriter | ||
import okio.BufferedSink | ||
import java.io.IOException | ||
|
||
/** | ||
* @author Aleksandar Gotev | ||
*/ | ||
|
||
class OkHttpBodyWriter(private val sink: BufferedSink) : BodyWriter() { | ||
@Throws(IOException::class) | ||
override fun write(bytes: ByteArray) { | ||
sink.write(bytes) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun write(bytes: ByteArray, lengthToWriteFromStart: Int) { | ||
sink.write(bytes, 0, lengthToWriteFromStart) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun flush() { | ||
sink.flush() | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.gotev.uploadservice.okhttp | ||
|
||
import net.gotev.uploadservice.ServerResponse | ||
import net.gotev.uploadservice.http.HttpConnection | ||
import okhttp3.MediaType | ||
import okhttp3.RequestBody | ||
import okhttp3.Response | ||
import okio.BufferedSink | ||
|
||
/** | ||
* @author Aleksandar Gotev | ||
*/ | ||
private fun String.requiresRequestBody() = this == "POST" || this == "PUT" || this == "PATCH" || this == "PROPPATCH" || this == "REPORT" | ||
|
||
private fun String.permitsRequestBody() = !(this == "GET" || this == "HEAD") | ||
|
||
internal fun body(httpMethod: String, bodyLength: Long, contentType: MediaType?, delegate: HttpConnection.RequestBodyDelegate): RequestBody? { | ||
val method = httpMethod.trim().toUpperCase() | ||
|
||
if (!method.permitsRequestBody() && !method.requiresRequestBody()) return null | ||
|
||
return object : RequestBody() { | ||
override fun contentLength() = bodyLength | ||
|
||
override fun contentType() = contentType | ||
|
||
override fun writeTo(sink: BufferedSink) { | ||
OkHttpBodyWriter(sink).apply { | ||
delegate.onBodyReady(this) | ||
flush() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun Response.headersHashMap() = LinkedHashMap(headers.toMap()) | ||
|
||
private fun Response.bodyBytes() = body?.bytes() ?: ByteArray(0) | ||
|
||
internal fun Response.asServerResponse() = ServerResponse(code, bodyBytes(), headersHashMap()) | ||
|
39 changes: 0 additions & 39 deletions
39
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpStack.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpStack.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.gotev.uploadservice.okhttp | ||
|
||
import net.gotev.uploadservice.http.HttpConnection | ||
import net.gotev.uploadservice.http.HttpStack | ||
import okhttp3.OkHttpClient | ||
import java.io.IOException | ||
|
||
/** | ||
* Implementation of the OkHttp Stack. | ||
* @author Aleksandar Gotev | ||
*/ | ||
class OkHttpStack(private val client: OkHttpClient = OkHttpClient()) : HttpStack { | ||
@Throws(IOException::class) | ||
override fun createNewConnection(method: String, url: String): HttpConnection { | ||
return OkHttpStackConnection(client, method, url) | ||
} | ||
} |
136 changes: 0 additions & 136 deletions
136
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpStackConnection.java
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
uploadservice-okhttp/src/main/java/net/gotev/uploadservice/okhttp/OkHttpStackConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package net.gotev.uploadservice.okhttp | ||
|
||
import net.gotev.uploadservice.Logger | ||
import net.gotev.uploadservice.NameValue | ||
import net.gotev.uploadservice.http.HttpConnection | ||
import okhttp3.MediaType | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import java.io.IOException | ||
import java.net.URL | ||
import java.util.* | ||
|
||
/** | ||
* [HttpConnection] implementation using OkHttpClient. | ||
* | ||
* @author Aleksandar Gotev | ||
*/ | ||
class OkHttpStackConnection(private val httpClient: OkHttpClient, private val httpMethod: String, url: String) : HttpConnection { | ||
|
||
private val requestBuilder = Request.Builder().url(URL(url)) | ||
private var bodyLength = 0L | ||
private var contentType: MediaType? = null | ||
private val uuid = UUID.randomUUID().toString() | ||
|
||
init { | ||
Logger.debug(javaClass.simpleName, "creating new OkHttp connection (uuid: $uuid)") | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun setHeaders(requestHeaders: List<NameValue>): HttpConnection { | ||
for (param in requestHeaders) { | ||
if ("content-type" == param.name.trim().toLowerCase()) | ||
contentType = param.value.trim().toMediaTypeOrNull() | ||
|
||
requestBuilder.header(param.name.trim(), param.value.trim()) | ||
} | ||
|
||
return this | ||
} | ||
|
||
override fun setTotalBodyBytes(totalBodyBytes: Long, isFixedLengthStreamingMode: Boolean): HttpConnection { | ||
// http://stackoverflow.com/questions/33921894/how-do-i-enable-disable-chunked-transfer-encoding-for-a-multi-part-post-that-inc#comment55679982_33921894 | ||
bodyLength = if (isFixedLengthStreamingMode) totalBodyBytes else -1 | ||
|
||
return this | ||
} | ||
|
||
private fun request(delegate: HttpConnection.RequestBodyDelegate) = | ||
requestBuilder.method( | ||
method = httpMethod, | ||
body = body(httpMethod, bodyLength, contentType, delegate) | ||
).build() | ||
|
||
@Throws(IOException::class) | ||
override fun getResponse(delegate: HttpConnection.RequestBodyDelegate) = httpClient | ||
.newCall(request(delegate)) | ||
.execute() | ||
.use { it.asServerResponse() } | ||
|
||
// Resources are automatically freed after usage. Log only. | ||
override fun close() { | ||
Logger.debug(javaClass.simpleName, "closing OkHttp connection (uuid: $uuid)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters