Skip to content

Commit

Permalink
feat: added functionality to choose between apache and okHttp for sen…
Browse files Browse the repository at this point in the history
…ding HTTP requests via settings
  • Loading branch information
tangcent committed May 11, 2024
1 parent 0f706c1 commit b8d0bda
Show file tree
Hide file tree
Showing 42 changed files with 2,129 additions and 704 deletions.
59 changes: 15 additions & 44 deletions common-api/src/main/kotlin/com/itangcent/http/ApacheHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ open class ApacheHttpClient : HttpClient {

private val httpClient: org.apache.http.client.HttpClient

constructor() {
val basicCookieStore = BasicCookieStore()
this.apacheCookieStore = ApacheCookieStore(basicCookieStore)
this.httpClientContext!!.cookieStore = basicCookieStore
this.httpClient = HttpClients.custom()
constructor() : this(
HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManager().also {
it.maxTotal = 50
it.defaultMaxPerRoute = 20
Expand All @@ -73,10 +70,8 @@ open class ApacheHttpClient : HttpClient {
.setSocketTimeout(30 * 1000)
.setCookieSpec(CookieSpecs.STANDARD).build()
)
.setSSLHostnameVerifier(NOOP_HOST_NAME_VERIFIER)
.setSSLSocketFactory(SSLSF)
.build()
}
)

constructor(httpClient: org.apache.http.client.HttpClient) {
val basicCookieStore = BasicCookieStore()
Expand Down Expand Up @@ -133,7 +128,7 @@ open class ApacheHttpClient : HttpClient {
if (param.type() == "file") {
val filePath = param.value()
if (filePath.isNullOrBlank()) {
continue
throw FileNotFoundException("file not found")
}
val file = File(filePath)
if (!file.exists() || !file.isFile) {
Expand Down Expand Up @@ -187,13 +182,7 @@ open class ApacheHttpClient : HttpClient {
}

@ScriptTypeName("request")
class ApacheHttpRequest : AbstractHttpRequest {

private val apacheHttpClient: ApacheHttpClient

constructor(apacheHttpClient: ApacheHttpClient) : super() {
this.apacheHttpClient = apacheHttpClient
}
class ApacheHttpRequest(private val apacheHttpClient: ApacheHttpClient) : AbstractHttpRequest() {

/**
* Executes HTTP request using the [apacheHttpClient].
Expand All @@ -214,13 +203,7 @@ fun HttpRequest.contentType(contentType: ContentType): HttpRequest {
* The implement of [CookieStore] by [org.apache.http.client.CookieStore].
*/
@ScriptTypeName("cookieStore")
class ApacheCookieStore : CookieStore {

private var cookieStore: org.apache.http.client.CookieStore

constructor(cookieStore: org.apache.http.client.CookieStore) {
this.cookieStore = cookieStore
}
class ApacheCookieStore(private var cookieStore: org.apache.http.client.CookieStore) : CookieStore {

/**
* Adds an [Cookie], replacing any existing equivalent cookies.
Expand Down Expand Up @@ -281,7 +264,7 @@ class ApacheHttpResponse(
*
* @return the status of the response, or {@code null} if not yet set
*/
override fun code(): Int? {
override fun code(): Int {
val statusLine = response.statusLine
return statusLine.statusCode
}
Expand Down Expand Up @@ -314,27 +297,20 @@ class ApacheHttpResponse(
}

/**
* Cache the bytes message of this response.
* the bytes message of this response.
*/
private var bytes: ByteArray? = null
private val bodyBytes: ByteArray by lazy {
response.entity.toByteArray()
}

/**
* Obtains the bytes message of this response.
*
* @return the response bytes, or
* {@code null} if there is none
*/
override fun bytes(): ByteArray? {
if (bytes == null) {
synchronized(this)
{
if (bytes == null) {
val entity = response.entity
bytes = entity.toByteArray()
}
}
}
return bytes!!
override fun bytes(): ByteArray {
return bodyBytes
}

/**
Expand All @@ -353,17 +329,12 @@ class ApacheHttpResponse(
* The implement of [Cookie] by [org.apache.http.cookie.Cookie].
*/
@ScriptTypeName("cookie")
class ApacheCookie : Cookie {
private val cookie: org.apache.http.cookie.Cookie
class ApacheCookie(private val cookie: org.apache.http.cookie.Cookie) : Cookie {

fun getWrapper(): org.apache.http.cookie.Cookie {
return cookie
}

constructor(cookie: org.apache.http.cookie.Cookie) {
this.cookie = cookie
}

override fun getName(): String? {
return cookie.name
}
Expand Down Expand Up @@ -404,7 +375,7 @@ class ApacheCookie : Cookie {
return cookie.isSecure
}

override fun getVersion(): Int? {
override fun getVersion(): Int {
return cookie.version
}

Expand Down
7 changes: 6 additions & 1 deletion common-api/src/main/kotlin/com/itangcent/http/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.itangcent.http
import com.itangcent.annotation.script.ScriptTypeName
import com.itangcent.common.constant.HttpMethod

/**
* Defines an interface for an HTTP client capable of creating various types of HTTP requests.
*/
@ScriptTypeName("httpClient")
interface HttpClient {

/**
* Returns a CookieStore to manage cookies for HTTP transactions.
*/
fun cookieStore(): CookieStore

/**
Expand Down Expand Up @@ -124,6 +130,5 @@ interface HttpClient {
fun head(url: String): HttpRequest {
return request().method(HttpMethod.HEAD).url(url)
}

}

31 changes: 30 additions & 1 deletion common-api/src/main/kotlin/com/itangcent/http/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,20 @@ interface Cookie {
* {@code null} if no such comment has been defined.
* Compatible only.Obsolete.
* @return comment
*
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun getComment(): String?

/**
* If a user agent (web browser) presents this cookie to a user, the
* cookie's purpose will be described by the information at this URL.
* Compatible only.Obsolete.
*
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun getCommentURL(): String?

/**
Expand Down Expand Up @@ -129,7 +135,10 @@ interface Cookie {
/**
* Get the Port attribute. It restricts the ports to which a cookie
* may be returned in a Cookie request header.
*
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun getPorts(): IntArray?

/**
Expand All @@ -146,19 +155,34 @@ interface Cookie {
* Compatible only.Obsolete.
*
* @return the version of the cookie.
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun getVersion(): Int?
}

fun Cookie.isExpired(): Boolean {
val expiryDate = this.getExpiryDate()
return expiryDate != null && expiryDate < System.currentTimeMillis()
}

@ScriptTypeName("cookie")
interface MutableCookie : Cookie {

fun setName(name: String?)

fun setValue(value: String?)

/**
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun setComment(comment: String?)

/**
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun setCommentURL(commentURL: String?)

/**
Expand Down Expand Up @@ -193,7 +217,10 @@ interface MutableCookie : Cookie {
* Sets the Port attribute. It restricts the ports to which a cookie
* may be returned in a Cookie request header.
* Compatible only.Obsolete.
*
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun setPorts(ports: IntArray?)

/**
Expand All @@ -218,7 +245,10 @@ interface MutableCookie : Cookie {
* @param version the version of the cookie.
*
* @see Cookie.getVersion
*
* @deprecated it is only supported by Apache HttpClient
*/
@Deprecated("Obsolete")
fun setVersion(version: Int?)
}

Expand Down Expand Up @@ -1239,5 +1269,4 @@ class BasicHttpParam : HttpParam {
fun setType(type: String?) {
this.type = type
}

}
Loading

0 comments on commit b8d0bda

Please sign in to comment.