com.xqbase.util.http.HttpPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xqbase-util-jdk17 Show documentation
Show all versions of xqbase-util-jdk17 Show documentation
Reusable Java components for www.xqbase.com
package com.xqbase.util.http;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;
import com.xqbase.util.ByteArrayQueue;
import com.xqbase.util.SocketPool;
import com.xqbase.util.function.SupplierEx;
public class HttpPool extends SocketPool {
private String path_, host, proxyAuth;
private static SupplierEx
getSocketSupplier(final HttpParam param, final int timeout) {
return new SupplierEx() {
@Override
public Socket get() throws IOException {
Socket socket = SocketPool.createSocket(param.socketHost,
param.socketPort, param.connect ? false : param.secure, timeout);
return param.connect ? HttpUtil.connect(socket,
param.host, param.proxyAuth, param.secure) : socket;
}
};
}
private HttpPool(HttpParam param, int soTimeout, int kaTimeout) {
super(getSocketSupplier(param, soTimeout), kaTimeout);
path_ = param.path;
host = param.host;
proxyAuth = param.proxyAuth;
}
public HttpPool(String url, int timeout) {
this(url, timeout, timeout);
}
public HttpPool(String url, int soTimeout, int kaTimeout) {
this(null, url, soTimeout, kaTimeout);
}
public HttpPool(HttpProxy httpProxy, String url, int timeout) {
this(httpProxy, url, timeout, timeout);
}
public HttpPool(HttpProxy httpProxy, String url, int soTimeout, int kaTimeout) {
this(new HttpParam(httpProxy, url), soTimeout, kaTimeout);
}
private int request(String path, ByteArrayQueue requestBody,
Map> requestHeaders, boolean head, ByteArrayQueue responseBody,
Map> responseHeaders) throws IOException {
while (true) {
try (Entry entry = borrow()) {
try {
boolean[] connectionClose = {false};
int status = HttpUtil.request(entry.getObject(), path_ + path,
host, proxyAuth, requestBody, requestHeaders, head,
responseBody, responseHeaders, connectionClose);
entry.setValid(!connectionClose[0]);
return status;
} catch (IOException e) {
if (e instanceof SocketTimeoutException || entry.getBorrows() == 0) {
throw e;
}
continue;
}
}
}
}
public int head(String path, Map> requestHeaders,
Map> responseHeaders) throws IOException {
return request(path, null, requestHeaders, true, null, responseHeaders);
}
public int get(String path,
Map> requestHeaders, ByteArrayQueue responseBody,
Map> responseHeaders) throws IOException {
return request(path, null, requestHeaders, false, responseBody, responseHeaders);
}
public int post(String path, ByteArrayQueue requestBody,
Map> requestHeaders, ByteArrayQueue responseBody,
Map> responseHeaders) throws IOException {
return request(path, requestBody, requestHeaders, false, responseBody, responseHeaders);
}
public void pipeline(List requests,
List responses) throws IOException {
try (Entry entry = borrow()) {
boolean[] connectionClose = {false};
Pipeline.pipeline(entry.getObject(), host, requests, responses, connectionClose);
entry.setValid(!connectionClose[0]);
}
}
}