forked from crossoverJie/cim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
431138c
commit 536d7b3
Showing
9 changed files
with
180 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
cim-common/src/main/java/com/crossoverjie/cim/common/core/proxy/ProxyManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.crossoverjie.cim.common.core.proxy; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.crossoverjie.cim.common.exception.CIMException; | ||
import com.crossoverjie.cim.common.util.HttpClient; | ||
import okhttp3.OkHttpClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
import static com.crossoverjie.cim.common.enums.StatusEnum.VALIDATION_FAIL; | ||
|
||
/** | ||
* Function: | ||
* | ||
* @author crossoverJie | ||
* Date: 2020-04-25 00:18 | ||
* @since JDK 1.8 | ||
*/ | ||
public final class ProxyManager<T> { | ||
|
||
private final static Logger LOGGER = LoggerFactory.getLogger(ProxyManager.class); | ||
|
||
private Class<T> clazz; | ||
|
||
private String url; | ||
|
||
private OkHttpClient okHttpClient; | ||
|
||
public ProxyManager(Class<T> clazz, String url, OkHttpClient okHttpClient) { | ||
this.clazz = clazz; | ||
this.url = url; | ||
this.okHttpClient = okHttpClient; | ||
} | ||
|
||
/** | ||
* Get proxy instance of api. | ||
* @return | ||
*/ | ||
public T getInstance() { | ||
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{clazz}, new ProxyInvocation()); | ||
} | ||
|
||
|
||
private class ProxyInvocation implements InvocationHandler { | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
JSONObject jsonObject = new JSONObject(); | ||
|
||
if (args != null && args.length > 1) { | ||
throw new CIMException(VALIDATION_FAIL); | ||
} | ||
|
||
|
||
if (method.getParameterTypes().length > 0){ | ||
Object para = args[0]; | ||
Class<?> parameterType = method.getParameterTypes()[0]; | ||
for (Field field : parameterType.getDeclaredFields()) { | ||
field.setAccessible(true); | ||
jsonObject.put(field.getName(), field.get(para)); | ||
} | ||
} | ||
url = url + "/" + method.getName(); | ||
return HttpClient.call(okHttpClient, jsonObject.toString(), url); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.