use of org.springframework.cglib.proxy.InvocationHandler in project mlib by myshzzx.
the class SpringExporter method proxySock.
public static <T> T proxySock(String host, int port, Class<T> type, @Nullable String beanName) {
ThreadLocal<Triple<Socket, ObjectInputStream, ObjectOutputStream>> tt = ThreadLocal.withInitial(() -> Triple.of(null, null, null));
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setCallback((InvocationHandler) (o, method, args) -> {
Triple<Socket, ObjectInputStream, ObjectOutputStream> tsoo = tt.get();
Socket socket = tsoo.getLeft();
ObjectOutputStream out = tsoo.getRight();
ObjectInputStream in = tsoo.getMiddle();
if (socket == null || socket.isClosed()) {
socket = new Socket(host, port);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
tt.set(Triple.of(socket, in, out));
}
out.writeObject(new Invoke(type, beanName, method.getDeclaringClass(), method.getName(), method.getParameterTypes(), args));
out.flush();
Result r = (Result) in.readObject();
return r.getResult();
});
return (T) enhancer.create();
}
use of org.springframework.cglib.proxy.InvocationHandler in project mlib by myshzzx.
the class SpringExporter method proxyHttp.
public static <T> T proxyHttp(String url, String lineSepHeaders, Class<T> type, @Nullable String beanName) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setCallback((InvocationHandler) (o, method, args) -> {
Invoke iv = new Invoke(type, beanName, method.getDeclaringClass(), method.getName(), method.getParameterTypes(), args);
HttpUriRequest req = new HttpGet(url);
req.setHeader("invoke", Base64.getEncoder().encodeToString(SERIALIZER.serialize(iv)));
if (lineSepHeaders != null) {
String[] lines = lineSepHeaders.split("[\\r\\n]+");
for (String line : lines) {
line = line.trim();
if (line.length() > 0) {
String[] hs = line.split(":", 2);
req.setHeader(hs[0], hs[1]);
}
}
}
try (CloseableHttpResponse rsp = hc.execute(req)) {
JSONObject rj = JSON.parseObject(rsp.getEntity().getContent(), JSONObject.class);
Result r = SERIALIZER.deserialize(Base64.getDecoder().decode(rj.getString("response")));
return r.getResult();
}
});
return (T) enhancer.create();
}
Aggregations