com.jianggujin.http.support.JApiInvocationHandler Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2018 jianggujin (www.jianggujin.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jianggujin.http.support;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.jianggujin.http.core.JRequestBodyResolver;
import com.jianggujin.http.request.JRequestBodyResolverFactory;
import com.jianggujin.http.support.annotation.JApiRequestBodyResolver;
import com.jianggujin.http.support.annotation.JApiResponseCreator;
import com.jianggujin.http.support.annotation.JBaseUrl;
import com.jianggujin.http.support.annotation.JCookie;
import com.jianggujin.http.support.annotation.JHeader;
import com.jianggujin.http.support.annotation.JRequestParam;
import com.jianggujin.http.util.JDataUtils;
/**
* 动态代理API接口
*
* @author jianggujin
*
*/
public class JApiInvocationHandler implements InvocationHandler {
private String baseUrl;
private JApiRequestBodyResolver requestBodyResolver;
private JApiResponseCreator responseCreator;
private Map cookies;
private Map headers;
private Map requestParams;
private volatile Map methods;
public JApiInvocationHandler(Class> clazz) throws Exception {
baseUrl = getUrl(clazz);
requestBodyResolver = clazz.getAnnotation(JApiRequestBodyResolver.class);
responseCreator = clazz.getAnnotation(JApiResponseCreator.class);
cookies = getCookies(clazz.getAnnotation(JCookie.class));
headers = getHeaders(clazz.getAnnotation(JHeader.class));
requestParams = getRequestParams(clazz.getAnnotation(JRequestParam.class));
}
@Override
public Object invoke(Object proxyObj, Method method, Object[] args) throws Throwable {
if (methods == null) {
synchronized (this) {
if (methods == null) {
methods = new ConcurrentHashMap();
}
}
}
synchronized (method.getName()) {
JMethodHolder holder = this.methods.get(method);
if (holder == null) {
holder = new JMethodHolder(baseUrl, requestBodyResolver, responseCreator, cookies, headers,
requestParams, method);
}
return holder.invoke(args);
}
}
/**
* 获得基础路径
*
* @param declaringClass
* @return
*/
private static String getUrl(Class> declaringClass) {
JBaseUrl baseUrl = declaringClass.getAnnotation(JBaseUrl.class);
if (baseUrl != null) {
return baseUrl.value();
}
return null;
}
/**
* 获得请求体解析器
*
* @param requestBodyResolver
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
protected static JRequestBodyResolver getRequestBodyResolver(JApiRequestBodyResolver requestBodyResolver)
throws InstantiationException, IllegalAccessException {
if (requestBodyResolver != null) {
if (!requestBodyResolver.cache()) {
return requestBodyResolver.value().newInstance();
}
JRequestBodyResolver resolver = JRequestBodyResolverFactory.findResolver(requestBodyResolver.value());
if (resolver == null) {
resolver = requestBodyResolver.value().newInstance();
JRequestBodyResolverFactory.register(resolver);
}
return resolver;
}
return null;
}
/**
* 响应创建器
*
* @param responseCreator
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
protected static JResponseCreator getResponseCreator(JApiResponseCreator responseCreator)
throws InstantiationException, IllegalAccessException {
if (responseCreator != null) {
if (!responseCreator.cache()) {
return responseCreator.value().newInstance();
}
JResponseCreator creator = JResponseCreatorCache.getCreator(responseCreator.value());
if (creator == null) {
creator = responseCreator.value().newInstance();
JResponseCreatorCache.register(creator);
}
return creator;
}
return null;
}
/***
* 获得类或方法上的{@link JCookie}注解
*
* @param cookie
* @return
*/
protected static Map getCookies(JCookie cookie) {
// 设置Cookie
if (cookie != null) {
return JDataUtils.string2Map(cookie.value(), cookie.itemDelimiter(), cookie.kvDelimiter(), false, false,
null, null);
}
return null;
}
/***
* 获得类或方法上的{@link JHeader}注解
*
* @param header
*/
protected static Map getHeaders(JHeader header) {
if (header != null) {
return JDataUtils.string2Map(header.value(), header.itemDelimiter(), header.kvDelimiter(), false, false,
null, null);
}
return null;
}
/***
* 获得类或方法上的{@link JRequestParam}注解
*
* @param requestParam
*/
protected static Map getRequestParams(JRequestParam requestParam) {
if (requestParam != null) {
return JDataUtils.string2Map(requestParam.value(), requestParam.itemDelimiter(), requestParam.kvDelimiter(),
false, false, null, null);
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy