sync, async 처리 수정

master
mjkhan21 1 year ago
parent 183e982ce1
commit 6994a8b205

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

Loading…
Cancel
Save