Compare commits

..

No commits in common. "0b2ccbf3e7f095a8206f7b245633ee12643b163e" and "871cf00c0b81ef7f70c0f11f0c0417c2900f48d3" have entirely different histories.

16 changed files with 19 additions and 290 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.4</version> <version>1.0.3</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.4</sdk.version> <sdk.version>1.0.3</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.4</version> <version>1.0.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -1,37 +0,0 @@
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.4</version> <version>1.0.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -7,7 +7,10 @@ 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.*; import group.flyfish.rest.registry.proxy.support.resolvers.RestBodyArgumentResolver;
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;
@ -82,8 +85,6 @@ 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,14 +121,10 @@ 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(part); this.multipartList.add(new Multipart(name, filename, data));
return this; return this;
} }

View File

@ -12,7 +12,6 @@ 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;
@ -53,11 +52,13 @@ 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, resolveType(filename), filename); builder.addBinaryBody(name, (byte[]) data,
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, resolveType(filename), filename); builder.addBinaryBody(name, (File) data,
} else if (data instanceof InputStream) { ContentType.create(MIME_MAP.getOrDefault(DataUtils.getExtension(filename),
builder.addBinaryBody(name, (InputStream) data, resolveType(filename), filename); ContentType.APPLICATION_OCTET_STREAM.getMimeType())), filename);
} else { } else {
throw new IllegalArgumentException("上传时,输入的数据不被支持!"); throw new IllegalArgumentException("上传时,输入的数据不被支持!");
} }
@ -99,15 +100,4 @@ 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,14 +139,6 @@ 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,7 +1,6 @@
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;
@ -33,12 +32,6 @@ 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)) {
@ -81,12 +74,7 @@ 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<>();
@ -94,44 +82,6 @@ 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);
} }
@ -147,15 +97,4 @@ 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 parameter.isAnnotationPresent(RestBody.class); return null != parameter.getAnnotation(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 parameter.isAnnotationPresent(RestHeader.class); return null != parameter.getAnnotation(RestHeader.class);
} }
/** /**

View File

@ -1,76 +0,0 @@
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 parameter.isAnnotationPresent(RestPathParam.class); return null != parameter.getAnnotation(RestPathParam.class);
} }
/** /**

View File

@ -80,16 +80,6 @@ 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,9 +77,6 @@ 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) {
@ -89,9 +86,6 @@ 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

@ -1,60 +0,0 @@
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);
}
}