TODO: method 方法不填时为所有函数的使用权

This commit is contained in:
huangsimin 2019-07-09 11:09:41 +08:00
parent ee69aa0a6d
commit dc759f8489
5 changed files with 56 additions and 68 deletions

View File

@ -19,6 +19,7 @@ import org.springframework.cloud.gateway.support.DefaultServerRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.server.reactive.ServerHttpRequest;
@ -66,63 +67,58 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
GenericService gs = gsPool.get(uri);
ServerRequest serverRequest = new DefaultServerRequest(exchange);
Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).map(s -> {
log.info("original text:{}", s);
/**
* do your decrypt() here and convert to String.class
* or you can change the output class to change format
*/
return "foooooooo " + s;
});
modifiedBody.subscribe( databuferr -> {
log.info(databuferr);
});
ServerHttpRequest req = exchange.getRequest();
Flux<DataBuffer> body = req.getBody();
body.subscribe( databuferr -> {
byte[] bytes = new byte[databuferr.readableByteCount()];
databuferr.read(bytes);
log.warn(bytes.toString());
});
body.flatMap( databuferr->{
byte[] bytes = new byte[databuferr.readableByteCount()];
databuferr.read(bytes);
log.warn(bytes.toString());
return Flux.empty();
} );
MultiValueMap<String, String> queryParams = req.getQueryParams();
// body.map( buffer -> {
// byte[] bytes = new byte[buffer.readableByteCount()];
// buffer.read(bytes);
// DataBufferUtils.release(buffer);
// try {
// String bodyString = new String(bytes, "utf-8");
// log.error("test body");
// } catch (Exception e) {
// e.printStackTrace();
// }
// return Flux.just(bytes);
// ServerRequest serverRequest = new DefaultServerRequest(exchange);
// Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).map(s -> {
// log.info("original text:{}", s);
// /**
// * do your decrypt() here and convert to String.class or you can change the
// * output class to change format
// */
// return "12 " + s;
// });
List<String> methodString = queryParams.get("method");
List<String> params = queryParams.get("params");
// modifiedBody.subscribe(databuferr -> {
// log.info(databuferr);
// });
ServerHttpRequest req = exchange.getRequest();
HttpHeaders headers = req.getHeaders();
List<String> methodString = headers.get("method");
List<String> params = headers.get("params");
if(methodString.size() != 0) {
// special
List<String> paramTypes = mehtods.get(methodString.get(0));
Object result = gs.$invoke(methodString.get(0), Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class), params.toArray());
if (result != null) {
ServerHttpResponse response = exchange.getResponse();
return response.writeWith(Mono
.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
if(paramTypes != null) {
int paramsSize = 0;
if(params != null) {
paramsSize = params.size();
}
if(paramTypes.size() == paramsSize) {
Object result = null;
if(paramsSize == 0) {
result = gs.$invoke(methodString.get(0), new String[]{}, new Object[]{});
} else {
result = gs.$invoke(methodString.get(0), Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class), params.toArray());
}
ServerHttpResponse response = exchange.getResponse();
if (result != null) {
return response.writeWith(Mono
.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
}
return response.setComplete();
}else {
log.warn("paramTypes.size {} is not equals to params size {}", paramTypes.size(), params.size());
}
} else {
log.warn("mehtod: {} is not exist or not allowed", methodString.get(0));
}
} else {
log.info("queryParams.get(\"method\") is null");
}

View File

@ -89,7 +89,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
// log.error(e.toString());
// }
Map<String, Object> defaultYaml = (Map<String, Object>) configYaml.get("default");
Map<String, Object> defaultYaml = (Map<String, Object>) configYaml.get("restful");
Map<String, Object> dubboYaml = (Map<String, Object>) configYaml.get("dubbo");
List<RouteDefinition> routeList = new ArrayList<RouteDefinition>();

View File

@ -1,7 +1,9 @@
package cn.ecpark.service.usergw.config;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
@ -50,7 +52,9 @@ public class ConfigSpecialFunction {
log.warn("method {} is duplicate", name);
}
List<String> params = (List<String>)cfg.get("param-types");
if(params == null) {
params = new LinkedList<>(); // 不用ArrayList 减少内存的使用.
}
DubboGatewayFilterFactory.mehtods.put(name, params);
}

View File

@ -2,7 +2,6 @@ restful:
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
routes:
- id: path_route
uri: http://httpbin.org:80/*
order: 9
@ -32,16 +31,15 @@ restful:
dubbo:
routes:
- id: test
order: 10
order: 0
application: dubbo-exchange
methods: # 如果没填就从 request拿 意味着所有接口都可以使用
- name: Say
param-types:
- java.lang.String
- name: Hello
connections: 4
group: test
# registry: zookeeper://127.0.0.1:2181

View File

@ -38,16 +38,6 @@ public class TestExtract {
@Test
public void TestUriQueryList() {
// String uriString = "dubbo://qu/ha?method=213&method=312";
// QueryStringDecoder decode = new QueryStringDecoder(uriString);
// URI uri = URI.create(uriString);
// Map<String, List<String>> parameters = decode.parameters();
// Assert.assertNotNull(uri);
String[] _words = {"Hello", "World", "Hello"};
Object owords = Arrays.asList(_words);
List<String> words = (List<String>) owords;
List<String[]> a = words.stream().map(word -> ((String) word).split("e")).distinct().collect(Collectors.toList());
System.out.println(a);
}
}