Skip to content

Commit 83241db

Browse files
committed
feat: added functionality to choose between apache and okHttp for sending HTTP requests via settings
1 parent 0f706c1 commit 83241db

36 files changed

+1877
-680
lines changed

common-api/src/main/kotlin/com/itangcent/http/ApacheHttpClient.kt

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ open class ApacheHttpClient : HttpClient {
5252

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

55-
constructor() {
56-
val basicCookieStore = BasicCookieStore()
57-
this.apacheCookieStore = ApacheCookieStore(basicCookieStore)
58-
this.httpClientContext!!.cookieStore = basicCookieStore
59-
this.httpClient = HttpClients.custom()
55+
constructor() : this(
56+
HttpClients.custom()
6057
.setConnectionManager(PoolingHttpClientConnectionManager().also {
6158
it.maxTotal = 50
6259
it.defaultMaxPerRoute = 20
@@ -73,10 +70,8 @@ open class ApacheHttpClient : HttpClient {
7370
.setSocketTimeout(30 * 1000)
7471
.setCookieSpec(CookieSpecs.STANDARD).build()
7572
)
76-
.setSSLHostnameVerifier(NOOP_HOST_NAME_VERIFIER)
77-
.setSSLSocketFactory(SSLSF)
7873
.build()
79-
}
74+
)
8075

8176
constructor(httpClient: org.apache.http.client.HttpClient) {
8277
val basicCookieStore = BasicCookieStore()
@@ -133,7 +128,7 @@ open class ApacheHttpClient : HttpClient {
133128
if (param.type() == "file") {
134129
val filePath = param.value()
135130
if (filePath.isNullOrBlank()) {
136-
continue
131+
throw FileNotFoundException("file not found")
137132
}
138133
val file = File(filePath)
139134
if (!file.exists() || !file.isFile) {
@@ -187,13 +182,7 @@ open class ApacheHttpClient : HttpClient {
187182
}
188183

189184
@ScriptTypeName("request")
190-
class ApacheHttpRequest : AbstractHttpRequest {
191-
192-
private val apacheHttpClient: ApacheHttpClient
193-
194-
constructor(apacheHttpClient: ApacheHttpClient) : super() {
195-
this.apacheHttpClient = apacheHttpClient
196-
}
185+
class ApacheHttpRequest(private val apacheHttpClient: ApacheHttpClient) : AbstractHttpRequest() {
197186

198187
/**
199188
* Executes HTTP request using the [apacheHttpClient].
@@ -214,13 +203,7 @@ fun HttpRequest.contentType(contentType: ContentType): HttpRequest {
214203
* The implement of [CookieStore] by [org.apache.http.client.CookieStore].
215204
*/
216205
@ScriptTypeName("cookieStore")
217-
class ApacheCookieStore : CookieStore {
218-
219-
private var cookieStore: org.apache.http.client.CookieStore
220-
221-
constructor(cookieStore: org.apache.http.client.CookieStore) {
222-
this.cookieStore = cookieStore
223-
}
206+
class ApacheCookieStore(private var cookieStore: org.apache.http.client.CookieStore) : CookieStore {
224207

225208
/**
226209
* Adds an [Cookie], replacing any existing equivalent cookies.
@@ -281,7 +264,7 @@ class ApacheHttpResponse(
281264
*
282265
* @return the status of the response, or {@code null} if not yet set
283266
*/
284-
override fun code(): Int? {
267+
override fun code(): Int {
285268
val statusLine = response.statusLine
286269
return statusLine.statusCode
287270
}
@@ -314,27 +297,20 @@ class ApacheHttpResponse(
314297
}
315298

316299
/**
317-
* Cache the bytes message of this response.
300+
* the bytes message of this response.
318301
*/
319-
private var bytes: ByteArray? = null
302+
private val bodyBytes: ByteArray by lazy {
303+
response.entity.toByteArray()
304+
}
320305

321306
/**
322307
* Obtains the bytes message of this response.
323308
*
324309
* @return the response bytes, or
325310
* {@code null} if there is none
326311
*/
327-
override fun bytes(): ByteArray? {
328-
if (bytes == null) {
329-
synchronized(this)
330-
{
331-
if (bytes == null) {
332-
val entity = response.entity
333-
bytes = entity.toByteArray()
334-
}
335-
}
336-
}
337-
return bytes!!
312+
override fun bytes(): ByteArray {
313+
return bodyBytes
338314
}
339315

340316
/**
@@ -353,17 +329,12 @@ class ApacheHttpResponse(
353329
* The implement of [Cookie] by [org.apache.http.cookie.Cookie].
354330
*/
355331
@ScriptTypeName("cookie")
356-
class ApacheCookie : Cookie {
357-
private val cookie: org.apache.http.cookie.Cookie
332+
class ApacheCookie(private val cookie: org.apache.http.cookie.Cookie) : Cookie {
358333

359334
fun getWrapper(): org.apache.http.cookie.Cookie {
360335
return cookie
361336
}
362337

363-
constructor(cookie: org.apache.http.cookie.Cookie) {
364-
this.cookie = cookie
365-
}
366-
367338
override fun getName(): String? {
368339
return cookie.name
369340
}
@@ -404,7 +375,7 @@ class ApacheCookie : Cookie {
404375
return cookie.isSecure
405376
}
406377

407-
override fun getVersion(): Int? {
378+
override fun getVersion(): Int {
408379
return cookie.version
409380
}
410381

common-api/src/main/kotlin/com/itangcent/http/HttpRequest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,20 @@ interface Cookie {
8282
* {@code null} if no such comment has been defined.
8383
* Compatible only.Obsolete.
8484
* @return comment
85+
*
86+
* @deprecated it is only supported by Apache HttpClient
8587
*/
88+
@Deprecated("Obsolete")
8689
fun getComment(): String?
8790

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

95101
/**
@@ -129,7 +135,10 @@ interface Cookie {
129135
/**
130136
* Get the Port attribute. It restricts the ports to which a cookie
131137
* may be returned in a Cookie request header.
138+
*
139+
* @deprecated it is only supported by Apache HttpClient
132140
*/
141+
@Deprecated("Obsolete")
133142
fun getPorts(): IntArray?
134143

135144
/**
@@ -146,19 +155,34 @@ interface Cookie {
146155
* Compatible only.Obsolete.
147156
*
148157
* @return the version of the cookie.
158+
* @deprecated it is only supported by Apache HttpClient
149159
*/
160+
@Deprecated("Obsolete")
150161
fun getVersion(): Int?
151162
}
152163

164+
fun Cookie.isExpired(): Boolean {
165+
val expiryDate = this.getExpiryDate()
166+
return expiryDate != null && expiryDate < System.currentTimeMillis()
167+
}
168+
153169
@ScriptTypeName("cookie")
154170
interface MutableCookie : Cookie {
155171

156172
fun setName(name: String?)
157173

158174
fun setValue(value: String?)
159175

176+
/**
177+
* @deprecated it is only supported by Apache HttpClient
178+
*/
179+
@Deprecated("Obsolete")
160180
fun setComment(comment: String?)
161181

182+
/**
183+
* @deprecated it is only supported by Apache HttpClient
184+
*/
185+
@Deprecated("Obsolete")
162186
fun setCommentURL(commentURL: String?)
163187

164188
/**
@@ -193,7 +217,10 @@ interface MutableCookie : Cookie {
193217
* Sets the Port attribute. It restricts the ports to which a cookie
194218
* may be returned in a Cookie request header.
195219
* Compatible only.Obsolete.
220+
*
221+
* @deprecated it is only supported by Apache HttpClient
196222
*/
223+
@Deprecated("Obsolete")
197224
fun setPorts(ports: IntArray?)
198225

199226
/**
@@ -218,7 +245,10 @@ interface MutableCookie : Cookie {
218245
* @param version the version of the cookie.
219246
*
220247
* @see Cookie.getVersion
248+
*
249+
* @deprecated it is only supported by Apache HttpClient
221250
*/
251+
@Deprecated("Obsolete")
222252
fun setVersion(version: Int?)
223253
}
224254

0 commit comments

Comments
 (0)