Here's the converted version using Apache HttpComponents (HttpClient). I'll use the latest version compatible with Java 8:
package com.i3.net
import org.apache.http.HttpEntity
import org.apache.http.client.methods.*
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
class Http3Service {
static final int MAX_CONCURRENT_CALLS = 200
static CloseableHttpClient client = createClient()
private static CloseableHttpClient createClient() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager()
cm.setMaxTotal(MAX_CONCURRENT_CALLS)
cm.setDefaultMaxPerRoute(MAX_CONCURRENT_CALLS)
return HttpClients.custom()
.setConnectionManager(cm)
.setConnectionTimeToLive(1, TimeUnit.MINUTES)
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
.build()
}
static Map postForm(String url, Map<String, String> formData, Map<String, Map> fileData = [:]) {
retryHttpRequest({ ->
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
formData.each { key, value ->
builder.addTextBody(key, value, ContentType.TEXT_PLAIN)
}
fileData.each { key, fileValue ->
builder.addBinaryBody(
key,
fileValue.inputStream,
ContentType.APPLICATION_OCTET_STREAM,
fileValue.filename
)
}
HttpPost httpPost = new HttpPost(url)
httpPost.setEntity(builder.build())
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(HttpUriRequest 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
}
}
}
}
To use this class, you'll need these Maven dependencies:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
Key changes made:
- Replaced OkHttp client with Apache HttpClient
- Used PoolingHttpClientConnectionManager for connection pooling
- Replaced OkHttp's MultipartBody with Apache's MultipartEntityBuilder
- Used Apache's HttpPost and StringEntity for requests
- Maintained the same interface and return type structure
- Kept the retry mechanism intact
- Used Apache's built-in retry handler in the client configuration
The functionality remains the same, but the implementation now uses Apache HttpComponents instead of OkHttp. The class still:
- Handles multipart form data uploads
- Supports JSON POST requests
- Implements retry logic
- Maintains connection pooling
- Returns the same response format (Map with 'response' and 'code')
Would you like me to explain any specific part in more detail?