I'll replace the OkHttp3 implementation with Apache HttpClient (from Apache HttpComponents). Here's the converted class using Apache's HTTP client instead:
groovy Copy code
package com.i3.net
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.StringEntity
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.util.EntityUtils
import java.util.concurrent.TimeUnit
class Http3Service {
static final int MAX_CONCURRENT_CALLS = 200
static final int TIMEOUT_MINUTES = 10
static CloseableHttpClient client = createClient()
private static CloseableHttpClient createClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager()
connectionManager.setMaxTotal(MAX_CONCURRENT_CALLS)
connectionManager.setDefaultMaxPerRoute(MAX_CONCURRENT_CALLS)
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout((int)TimeUnit.MINUTES.toMillis(2))
.setSocketTimeout((int)TimeUnit.MINUTES.toMillis(TIMEOUT_MINUTES))
.setConnectionRequestTimeout((int)TimeUnit.MINUTES.toMillis(TIMEOUT_MINUTES))
.build()
return HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.setConnectionManagerShared(true)
.build()
}
static Map postForm(String url, Map<String, String> formData, Map<String, Map> fileData = [:]) {
retryHttpRequest({ ->
HttpPost httpPost = new HttpPost(url)
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
// Add form fields
formData.each { key, value ->
builder.addTextBody(key, value)
}
// Add file data
fileData.each { key, fileValue ->
builder.addBinaryBody(
key,
fileValue.inputStream,
ContentType.APPLICATION_OCTET_STREAM,
fileValue.filename
)
}
HttpEntity multipartEntity = builder.build()
httpPost.setEntity(multipartEntity)
executeRequest(httpPost)
})
}
static Map postJson(String url, String json) {
retryHttpRequest({ ->
HttpPost httpPost = new HttpPost(url)
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON)
httpPost.setEntity(entity)
executeRequest(httpPost)
})
}
private static Map executeRequest(HttpPost request) {
CloseableHttpResponse response = null
try {
response = client.execute(request)
HttpEntity entity = response.getEntity()
String body = entity ? EntityUtils.toString(entity) : null
int code = response.getStatusLine().getStatusCode()
return [response: body, code: code]
} catch (IOException e) {
println "HTTP call failed: ${e.getClass().simpleName} - ${e.getMessage()}"
throw new RuntimeException("HTTP request failed", e)
} finally {
response?.close()
}
}
// Overloaded method to simulate default parameters
private static Map retryHttpRequest(Closure<Map> requestClosure) {
retryHttpRequest(requestClosure, 3, 5000)
}
private static Map retryHttpRequest(Closure<Map> requestClosure, int maxRetries, long initialDelayMillis) {
int attempt = 0
long delay = initialDelayMillis
while (true) {
try {
return requestClosure.call()
} catch (IOException e) {
attempt++
if (attempt > maxRetries) {
println "HTTP request failed after ${attempt} attempts: ${e.class.simpleName} - ${e.message}"
throw new RuntimeException("HTTP request failed after retries", e)
}
println "HTTP request failed: ${e.class.simpleName} - ${e.message}. Retrying in ${delay}ms... (attempt ${attempt}/${maxRetries})"
try {
Thread.sleep(delay)
} catch (InterruptedException ie) {
Thread.currentThread().interrupt()
throw new RuntimeException("Retry interrupted", ie)
}
delay *= 2 // Exponential backoff
}
}
}
}
Would you like me to explain the key differences between the OkHttp implementation and this Apache HttpClient implementation?