|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
package cokr.xit.foundation.web;
|
|
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.net.Authenticator;
|
|
|
|
|
import java.net.ProxySelector;
|
|
|
|
|
import java.net.URI;
|
|
|
|
@ -74,44 +76,67 @@ public class WebClient {
|
|
|
|
|
return builder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void get(Consumer<Config> configurer) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> HttpResponse<T> get(Consumer<Config> configurer) {
|
|
|
|
|
Config conf = new Config();
|
|
|
|
|
configurer.accept(conf);
|
|
|
|
|
HttpRequest hreq = conf.get();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!conf.async) {
|
|
|
|
|
HttpResponse<String> hresp = client().send(hreq, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
conf.respHandler.accept(hresp);
|
|
|
|
|
} else {
|
|
|
|
|
CompletableFuture<HttpResponse<String>> future = client()
|
|
|
|
|
.sendAsync(hreq, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
HttpResponse<String> hresp = future.get();
|
|
|
|
|
conf.respHandler.accept(hresp);
|
|
|
|
|
}
|
|
|
|
|
if (conf.download)
|
|
|
|
|
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler);
|
|
|
|
|
else
|
|
|
|
|
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofString(), conf.textHandler);
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
conf.errorHandler.accept(Assert.rootCause(e));
|
|
|
|
|
if (conf.async) {
|
|
|
|
|
conf.errorHandler.accept(Assert.rootCause(e));
|
|
|
|
|
return null;
|
|
|
|
|
} else
|
|
|
|
|
throw Assert.runtimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private <T> HttpResponse<T> handleRequest(boolean async, HttpRequest hreq, HttpResponse.BodyHandler<T> bodyHandler, Consumer<HttpResponse<T>> respHandler) throws Exception {
|
|
|
|
|
if (!async) {
|
|
|
|
|
return client().send(hreq, bodyHandler);
|
|
|
|
|
} else {
|
|
|
|
|
CompletableFuture<HttpResponse<T>> future = client().sendAsync(hreq, bodyHandler);
|
|
|
|
|
future.thenApply(resp -> {
|
|
|
|
|
respHandler.accept(resp);
|
|
|
|
|
return resp;
|
|
|
|
|
});
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void post(Consumer<Config> configurer) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> HttpResponse<T> post(Consumer<Config> configurer) {
|
|
|
|
|
Config conf = new Config();
|
|
|
|
|
configurer.accept(conf);
|
|
|
|
|
HttpRequest hreq = conf.post();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!conf.async) {
|
|
|
|
|
HttpResponse<String> hresp = client().send(hreq, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
conf.respHandler.accept(hresp);
|
|
|
|
|
} else {
|
|
|
|
|
CompletableFuture<HttpResponse<String>> future =
|
|
|
|
|
client().sendAsync(hreq, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
HttpResponse<String> hresp = future.get();
|
|
|
|
|
conf.respHandler.accept(hresp);
|
|
|
|
|
}
|
|
|
|
|
if (conf.download)
|
|
|
|
|
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofInputStream(), conf.downloadHandler);
|
|
|
|
|
else
|
|
|
|
|
return (HttpResponse<T>)handleRequest(conf.async, hreq, HttpResponse.BodyHandlers.ofString(), conf.textHandler);
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
conf.errorHandler.accept(Assert.rootCause(e));
|
|
|
|
|
if (conf.async) {
|
|
|
|
|
conf.errorHandler.accept(Assert.rootCause(e));
|
|
|
|
|
return null;
|
|
|
|
|
} else
|
|
|
|
|
throw Assert.runtimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write(InputStream inputStream, String path) {
|
|
|
|
|
try (
|
|
|
|
|
InputStream in = inputStream;
|
|
|
|
|
FileOutputStream out = new FileOutputStream(path);
|
|
|
|
|
) {
|
|
|
|
|
inputStream.transferTo(out);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw Assert.runtimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -119,16 +144,18 @@ public class WebClient {
|
|
|
|
|
private String uri;
|
|
|
|
|
private boolean
|
|
|
|
|
async,
|
|
|
|
|
json;
|
|
|
|
|
json,
|
|
|
|
|
download;
|
|
|
|
|
private Charset charset = StandardCharsets.UTF_8;
|
|
|
|
|
private LinkedHashMap<String, String> headers;
|
|
|
|
|
private LinkedHashMap<String, Object> data;
|
|
|
|
|
private Consumer<HttpResponse<String>> respHandler = hresp -> {
|
|
|
|
|
private Consumer<HttpResponse<String>> textHandler = hresp -> {
|
|
|
|
|
HttpHeaders headers = hresp.headers();
|
|
|
|
|
headers.map().forEach((k, v) -> System.out.println(k + " = " + v));
|
|
|
|
|
System.out.println("status: " + hresp.statusCode());
|
|
|
|
|
System.out.println(hresp.body());
|
|
|
|
|
};
|
|
|
|
|
private Consumer<HttpResponse<InputStream>> downloadHandler = hresp -> {};
|
|
|
|
|
private Consumer<Throwable> errorHandler = t -> {
|
|
|
|
|
t.printStackTrace(System.err);
|
|
|
|
|
};
|
|
|
|
@ -160,6 +187,11 @@ public class WebClient {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Config download(boolean download) {
|
|
|
|
|
this.download = download;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Config data(String key, Object value) {
|
|
|
|
|
if (data == null)
|
|
|
|
|
data = new LinkedHashMap<>();
|
|
|
|
@ -168,7 +200,12 @@ public class WebClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Config onResponse(Consumer<HttpResponse<String>> handler) {
|
|
|
|
|
respHandler = handler;
|
|
|
|
|
textHandler = handler;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Config onDownload(Consumer<HttpResponse<InputStream>> handler) {
|
|
|
|
|
downloadHandler = handler;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|