com.jdroid.java.http.BasicHttpResponseValidator.kt Maven / Gradle / Ivy
The newest version!
package com.jdroid.java.http
import com.jdroid.java.http.exception.ConnectionException
import com.jdroid.java.http.exception.HttpResponseException
open class BasicHttpResponseValidator : HttpServiceProcessor {
override fun onInit(httpService: HttpService) {
// Do Nothing
}
override fun beforeExecute(httpService: HttpService) {
// Do Nothing
}
/**
* Validate the response generated by the server.
*/
override fun afterExecute(httpService: HttpService, httpResponse: HttpResponseWrapper) {
val message = httpResponse.logStatusCode()
if (httpResponse.isSuccess()) {
onSuccess(httpResponse, message)
} else if (httpResponse.isClientError()) {
onClientError(httpResponse, message)
} else if (httpResponse.isServerError()) {
onServerError(httpResponse, message)
} else {
throw HttpResponseException(message)
}
}
protected open fun onSuccess(httpResponse: HttpResponseWrapper, message: String) {
// Do Nothing
}
protected open fun onClientError(httpResponse: HttpResponseWrapper, message: String) {
throw HttpResponseException(message)
}
protected open fun onServerError(httpResponse: HttpResponseWrapper, message: String) {
// 504 - Gateway Timeout
if (httpResponse.getStatusCode() == 504) {
throw ConnectionException(message)
} else {
throw HttpResponseException(message)
}
}
}