Search in sources :

Example 1 with InvocationHandler

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();
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) Enhancer(org.springframework.cglib.proxy.Enhancer) Socket(java.net.Socket) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) ObjectInputStream(java.io.ObjectInputStream) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ServerSocket(java.net.ServerSocket) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ObjectOutputStream(java.io.ObjectOutputStream) Serializer(mysh.util.Serializer) Triple(org.apache.commons.lang3.tuple.Triple) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) InvocationHandler(org.springframework.cglib.proxy.InvocationHandler) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) Serializable(java.io.Serializable) Base64(java.util.Base64) JSON(com.alibaba.fastjson.JSON) HttpGet(org.apache.http.client.methods.HttpGet) JSONObject(com.alibaba.fastjson.JSONObject) HttpClients(org.apache.http.impl.client.HttpClients) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Enhancer(org.springframework.cglib.proxy.Enhancer) ObjectOutputStream(java.io.ObjectOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with InvocationHandler

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();
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer) Socket(java.net.Socket) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) ObjectInputStream(java.io.ObjectInputStream) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ServerSocket(java.net.ServerSocket) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ObjectOutputStream(java.io.ObjectOutputStream) Serializer(mysh.util.Serializer) Triple(org.apache.commons.lang3.tuple.Triple) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) InvocationHandler(org.springframework.cglib.proxy.InvocationHandler) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) Serializable(java.io.Serializable) Base64(java.util.Base64) JSON(com.alibaba.fastjson.JSON) HttpGet(org.apache.http.client.methods.HttpGet) JSONObject(com.alibaba.fastjson.JSONObject) HttpClients(org.apache.http.impl.client.HttpClients) ApplicationContextAware(org.springframework.context.ApplicationContextAware) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Enhancer(org.springframework.cglib.proxy.Enhancer) JSONObject(com.alibaba.fastjson.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

JSON (com.alibaba.fastjson.JSON)2 JSONObject (com.alibaba.fastjson.JSONObject)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Serializable (java.io.Serializable)2 Method (java.lang.reflect.Method)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 Arrays (java.util.Arrays)2 Base64 (java.util.Base64)2 Nullable (javax.annotation.Nullable)2 Serializer (mysh.util.Serializer)2 Triple (org.apache.commons.lang3.tuple.Triple)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 HttpClients (org.apache.http.impl.client.HttpClients)2 Logger (org.slf4j.Logger)2