Compare commits

..

2 Commits

16 changed files with 290 additions and 19 deletions

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId> <artifactId>rest-proxy</artifactId>
<version>1.0.3</version> <version>1.0.4</version>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version> <commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version> <commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.0.3</sdk.version> <sdk.version>1.0.4</sdk.version>
</properties> </properties>
<packaging>pom</packaging> <packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId> <artifactId>rest-proxy</artifactId>
<version>1.0.3</version> <version>1.0.4</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -0,0 +1,37 @@
package group.flyfish.rest.annotation;
import java.lang.annotation.*;
/**
* rest请求体标记
*
* @author wangyu
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RestPart {
/**
* 请求部分名称
*
* @return 传入multipart中key
*/
String value() default "file";
/**
* 绑定文件名称
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface Filename {
/**
* 请求部分名称
*
* @return 传入multipart中key
*/
String value() default "file";
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>group.flyfish</groupId> <groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId> <artifactId>rest-proxy</artifactId>
<version>1.0.3</version> <version>1.0.4</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -7,10 +7,7 @@ import group.flyfish.rest.mapping.RestResultMapping;
import group.flyfish.rest.registry.RestApiRegistry; import group.flyfish.rest.registry.RestApiRegistry;
import group.flyfish.rest.registry.proxy.support.RestArgumentResolver; import group.flyfish.rest.registry.proxy.support.RestArgumentResolver;
import group.flyfish.rest.registry.proxy.support.RestArgumentResolverComposite; import group.flyfish.rest.registry.proxy.support.RestArgumentResolverComposite;
import group.flyfish.rest.registry.proxy.support.resolvers.RestBodyArgumentResolver; import group.flyfish.rest.registry.proxy.support.resolvers.*;
import group.flyfish.rest.registry.proxy.support.resolvers.RestHeaderArgumentResolver;
import group.flyfish.rest.registry.proxy.support.resolvers.RestParamArgumentResolver;
import group.flyfish.rest.registry.proxy.support.resolvers.RestPathParamArgumentResolver;
import group.flyfish.rest.utils.DataUtils; import group.flyfish.rest.utils.DataUtils;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -85,6 +82,8 @@ public class RestClientConfiguration {
public RestArgumentResolverComposite restArgumentResolverComposite() { public RestArgumentResolverComposite restArgumentResolverComposite() {
List<RestArgumentResolver> resolvers = Arrays.asList( List<RestArgumentResolver> resolvers = Arrays.asList(
new RestPathParamArgumentResolver(), new RestPathParamArgumentResolver(),
new RestPartArgumentResolver(),
new RestPartArgumentResolver.FilenameResolver(),
new RestBodyArgumentResolver(), new RestBodyArgumentResolver(),
new RestHeaderArgumentResolver(), new RestHeaderArgumentResolver(),
new RestParamArgumentResolver() new RestParamArgumentResolver()

View File

@ -121,10 +121,14 @@ public class RestClientBuilder {
} }
public RestClientBuilder addMultipartBody(String name, String filename, Object data) { public RestClientBuilder addMultipartBody(String name, String filename, Object data) {
return addMultipartBody(new Multipart(name, filename, data));
}
public RestClientBuilder addMultipartBody(Multipart part) {
if (null == this.multipartList) { if (null == this.multipartList) {
this.multipartList = new ArrayList<>(); this.multipartList = new ArrayList<>();
} }
this.multipartList.add(new Multipart(name, filename, data)); this.multipartList.add(part);
return this; return this;
} }

View File

@ -12,6 +12,7 @@ import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -52,13 +53,11 @@ public abstract class AbstractBodyResolver {
String name = multipart.getName(); String name = multipart.getName();
String filename = multipart.getFilename(); String filename = multipart.getFilename();
if (data instanceof byte[]) { if (data instanceof byte[]) {
builder.addBinaryBody(name, (byte[]) data, builder.addBinaryBody(name, (byte[]) data, resolveType(filename), filename);
ContentType.create(MIME_MAP.getOrDefault(DataUtils.getExtension(filename),
ContentType.APPLICATION_OCTET_STREAM.getMimeType())), filename);
} else if (data instanceof File) { } else if (data instanceof File) {
builder.addBinaryBody(name, (File) data, builder.addBinaryBody(name, (File) data, resolveType(filename), filename);
ContentType.create(MIME_MAP.getOrDefault(DataUtils.getExtension(filename), } else if (data instanceof InputStream) {
ContentType.APPLICATION_OCTET_STREAM.getMimeType())), filename); builder.addBinaryBody(name, (InputStream) data, resolveType(filename), filename);
} else { } else {
throw new IllegalArgumentException("上传时,输入的数据不被支持!"); throw new IllegalArgumentException("上传时,输入的数据不被支持!");
} }
@ -100,4 +99,15 @@ public abstract class AbstractBodyResolver {
} }
return null; return null;
} }
/**
* 解析内容类型
*
* @param filename 文件名
* @return 结果
*/
private ContentType resolveType(String filename) {
return ContentType.create(MIME_MAP.getOrDefault(DataUtils.getExtension(filename),
ContentType.APPLICATION_OCTET_STREAM.getMimeType()));
}
} }

View File

@ -139,6 +139,14 @@ public class RestProxyInvoker<T> implements InvocationHandler, PropertiesConfigu
if (context.hasBody()) { if (context.hasBody()) {
builder.body(context.getBody()); builder.body(context.getBody());
} }
// 赋值文件体
if (context.hasMultipart()) {
builder.multipart();
context.getFiles().forEach((key, value) -> {
value.setFilename(context.getFilename(key, value.getFilename()));
builder.addMultipartBody(value);
});
}
// 赋值参数们 // 赋值参数们
if (context.hasParams()) { if (context.hasParams()) {
builder.queryParams(context.getParam()); builder.queryParams(context.getParam());

View File

@ -1,6 +1,7 @@
package group.flyfish.rest.registry.proxy.support; package group.flyfish.rest.registry.proxy.support;
import group.flyfish.rest.annotation.RestApi; import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.core.entity.Multipart;
import group.flyfish.rest.utils.DataUtils; import group.flyfish.rest.utils.DataUtils;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@ -32,6 +33,12 @@ public class ArgumentResolveContext {
// 请求体 // 请求体
private Object body; private Object body;
// 文件列表
private Map<String, Multipart> files;
// 文件名列表
private Map<String, String> filenames;
// 设置参数 // 设置参数
public void setParam(String key, Object value) { public void setParam(String key, Object value) {
if (DataUtils.isEmpty(param)) { if (DataUtils.isEmpty(param)) {
@ -74,7 +81,12 @@ public class ArgumentResolveContext {
this.headers.put(name, value); this.headers.put(name, value);
} }
// 设置路径参数 /**
* 设置路径参数
*
* @param key 名称
* @param value
*/
public void setPathParam(String key, Object value) { public void setPathParam(String key, Object value) {
if (DataUtils.isEmpty(pathParams)) { if (DataUtils.isEmpty(pathParams)) {
pathParams = new HashMap<>(); pathParams = new HashMap<>();
@ -82,6 +94,44 @@ public class ArgumentResolveContext {
pathParams.put(key, value); pathParams.put(key, value);
} }
/**
* 设置文件
*
* @param name 文件key
* @param filename 文件名
* @param file 文件数据
*/
public void setMultipart(String name, String filename, Object file) {
setMultipart(new Multipart(name, filename, file));
}
/**
* 添加文件名
*
* @param part 文件部分key
* @param filename 文件名
*/
public void addFilename(String part, String filename) {
if (DataUtils.isEmpty(filenames)) {
filenames = new HashMap<>();
}
filenames.put(part, filename);
}
/**
* 设置文件
*
* @param multipart 文件
*/
public void setMultipart(Multipart multipart) {
if (DataUtils.isEmpty(files)) {
files = new HashMap<>();
}
if (null != multipart && null != multipart.getData()) {
files.put(multipart.getName(), multipart);
}
}
public boolean hasPathParams() { public boolean hasPathParams() {
return DataUtils.isNotEmpty(pathParams); return DataUtils.isNotEmpty(pathParams);
} }
@ -97,4 +147,15 @@ public class ArgumentResolveContext {
public boolean hasParams() { public boolean hasParams() {
return DataUtils.isNotEmpty(param); return DataUtils.isNotEmpty(param);
} }
public boolean hasMultipart() {
return DataUtils.isNotEmpty(files);
}
public String getFilename(String part, String initial) {
if (null == filenames || !filenames.containsKey(part)) {
return initial;
}
return filenames.get(part);
}
} }

View File

@ -21,7 +21,7 @@ public class RestBodyArgumentResolver implements RestArgumentResolver {
*/ */
@Override @Override
public boolean support(Parameter parameter) { public boolean support(Parameter parameter) {
return null != parameter.getAnnotation(RestBody.class); return parameter.isAnnotationPresent(RestBody.class);
} }
/** /**

View File

@ -23,7 +23,7 @@ public class RestHeaderArgumentResolver implements RestArgumentResolver {
*/ */
@Override @Override
public boolean support(Parameter parameter) { public boolean support(Parameter parameter) {
return null != parameter.getAnnotation(RestHeader.class); return parameter.isAnnotationPresent(RestHeader.class);
} }
/** /**

View File

@ -0,0 +1,76 @@
package group.flyfish.rest.registry.proxy.support.resolvers;
import group.flyfish.rest.annotation.RestPart;
import group.flyfish.rest.core.entity.Multipart;
import group.flyfish.rest.registry.proxy.support.ArgumentResolveContext;
import group.flyfish.rest.registry.proxy.support.RestArgumentResolver;
import java.lang.reflect.Parameter;
/**
* 文件上传参数处理器
*
* @author wangyu
*/
public class RestPartArgumentResolver implements RestArgumentResolver {
/**
* 是否支持
*
* @param parameter 参数
* @return 结果
*/
@Override
public boolean support(Parameter parameter) {
return parameter.isAnnotationPresent(RestPart.class);
}
/**
* 解析
*
* @param context 上下文赋值
* @param parameter 参数
* @param value
*/
@Override
public void resolve(ArgumentResolveContext context, Parameter parameter, Object value) {
RestPart part = parameter.getAnnotation(RestPart.class);
if (value instanceof Multipart) {
context.setMultipart((Multipart) value);
} else if (null != value) {
context.setMultipart(part.value(), null, value);
}
}
/**
* 解析和处理文件名
*/
public static class FilenameResolver implements RestArgumentResolver {
/**
* 是否支持
*
* @param parameter 参数
* @return 结果
*/
@Override
public boolean support(Parameter parameter) {
return parameter.isAnnotationPresent(RestPart.Filename.class);
}
/**
* 解析
*
* @param context 上下文赋值
* @param parameter 参数
* @param value
*/
@Override
public void resolve(ArgumentResolveContext context, Parameter parameter, Object value) {
if (value instanceof String) {
RestPart.Filename filename = parameter.getAnnotation(RestPart.Filename.class);
context.addFilename(filename.value(), (String) value);
}
}
}
}

View File

@ -22,7 +22,7 @@ public class RestPathParamArgumentResolver implements RestArgumentResolver {
*/ */
@Override @Override
public boolean support(Parameter parameter) { public boolean support(Parameter parameter) {
return null != parameter.getAnnotation(RestPathParam.class); return parameter.isAnnotationPresent(RestPathParam.class);
} }
/** /**

View File

@ -80,6 +80,16 @@ public final class DataUtils {
return CollectionUtils.isEmpty(map); return CollectionUtils.isEmpty(map);
} }
/**
* 判断集合是否为空
*
* @param collection 集合
* @return 结果
*/
public static boolean isEmpty(Collection<?> collection) {
return CollectionUtils.isEmpty(collection);
}
/** /**
* 是否为空 * 是否为空
* *

View File

@ -77,6 +77,9 @@ public final class JacksonUtil {
if (null == json || "".equals(json)) { if (null == json || "".equals(json)) {
return null; return null;
} }
if (String.class.isAssignableFrom(clazz)) {
return DataUtils.cast(json);
}
try { try {
return mapper.readValue(json, clazz); return mapper.readValue(json, clazz);
} catch (IOException e) { } catch (IOException e) {
@ -86,6 +89,9 @@ public final class JacksonUtil {
} }
public static <T> T fromJson(final String json, JavaType type) { public static <T> T fromJson(final String json, JavaType type) {
if (type.isTypeOrSubTypeOf(String.class)) {
return DataUtils.cast(json);
}
if (null == json || "".equals(json)) { if (null == json || "".equals(json)) {
return null; return null;
} }

View File

@ -0,0 +1,60 @@
package group.flyfish.rest;
import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.annotation.RestPart;
import group.flyfish.rest.annotation.RestService;
import group.flyfish.rest.container.RestTestContainer;
import group.flyfish.rest.core.entity.Multipart;
import group.flyfish.rest.enums.HttpMethod;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = RestTestContainer.class)
@ComponentScan("sys.test")
@Slf4j
@Component
public class MultipartTest {
@Resource
private TestRestService testRestService;
/**
* 测试入口
*/
@Test
public void test() {
File file = new File("/Users/wangyu/Desktop/2022年终述职报告.docx");
String filename = testRestService.uploadPart(new Multipart("file", file.getName(), file));
System.out.println(filename);
try (InputStream in = Files.newInputStream(file.toPath())) {
filename = testRestService.uploadAnno(in, file.getName());
System.out.println(filename);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@RestService(baseUrl = "http://localhost:8999", timeout = 500)
public interface TestRestService {
@RestApi(uri = "/files", method = HttpMethod.POST)
String uploadPart(@RestPart Multipart file);
@RestApi(uri = "/files", method = HttpMethod.POST)
String uploadAnno(@RestPart("fbl") InputStream file, @RestPart.Filename("fbl") String name);
}
}