TODO: method 方法不填时为所有函数的使用权
This commit is contained in:
parent
ee69aa0a6d
commit
dc759f8489
|
@ -19,6 +19,7 @@ import org.springframework.cloud.gateway.support.DefaultServerRequest;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.codec.HttpMessageReader;
|
import org.springframework.http.codec.HttpMessageReader;
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
@ -66,63 +67,58 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
|
||||||
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
||||||
GenericService gs = gsPool.get(uri);
|
GenericService gs = gsPool.get(uri);
|
||||||
|
|
||||||
|
// ServerRequest serverRequest = new DefaultServerRequest(exchange);
|
||||||
ServerRequest serverRequest = new DefaultServerRequest(exchange);
|
// Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).map(s -> {
|
||||||
Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).map(s -> {
|
// log.info("original text:{}", s);
|
||||||
log.info("original text:{}", s);
|
// /**
|
||||||
/**
|
// * do your decrypt() here and convert to String.class or you can change the
|
||||||
* do your decrypt() here and convert to String.class
|
// * output class to change format
|
||||||
* or you can change the output class to change format
|
// */
|
||||||
*/
|
// return "12 " + s;
|
||||||
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);
|
|
||||||
// });
|
// });
|
||||||
|
|
||||||
List<String> methodString = queryParams.get("method");
|
// modifiedBody.subscribe(databuferr -> {
|
||||||
List<String> params = queryParams.get("params");
|
// 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) {
|
if(methodString.size() != 0) {
|
||||||
// special
|
// special
|
||||||
List<String> paramTypes = mehtods.get(methodString.get(0));
|
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(paramTypes != null) {
|
||||||
if (result != null) {
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
int paramsSize = 0;
|
||||||
return response.writeWith(Mono
|
if(params != null) {
|
||||||
.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
|
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 {
|
} else {
|
||||||
log.info("queryParams.get(\"method\") is null");
|
log.info("queryParams.get(\"method\") is null");
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,7 +89,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
// log.error(e.toString());
|
// 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");
|
Map<String, Object> dubboYaml = (Map<String, Object>) configYaml.get("dubbo");
|
||||||
|
|
||||||
List<RouteDefinition> routeList = new ArrayList<RouteDefinition>();
|
List<RouteDefinition> routeList = new ArrayList<RouteDefinition>();
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package cn.ecpark.service.usergw.config;
|
package cn.ecpark.service.usergw.config;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
@ -50,7 +52,9 @@ public class ConfigSpecialFunction {
|
||||||
log.warn("method {} is duplicate", name);
|
log.warn("method {} is duplicate", name);
|
||||||
}
|
}
|
||||||
List<String> params = (List<String>)cfg.get("param-types");
|
List<String> params = (List<String>)cfg.get("param-types");
|
||||||
|
if(params == null) {
|
||||||
|
params = new LinkedList<>(); // 不用ArrayList 减少内存的使用.
|
||||||
|
}
|
||||||
DubboGatewayFilterFactory.mehtods.put(name, params);
|
DubboGatewayFilterFactory.mehtods.put(name, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ restful:
|
||||||
default-filters:
|
default-filters:
|
||||||
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
|
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
|
||||||
routes:
|
routes:
|
||||||
|
|
||||||
- id: path_route
|
- id: path_route
|
||||||
uri: http://httpbin.org:80/*
|
uri: http://httpbin.org:80/*
|
||||||
order: 9
|
order: 9
|
||||||
|
@ -32,16 +31,15 @@ restful:
|
||||||
dubbo:
|
dubbo:
|
||||||
routes:
|
routes:
|
||||||
- id: test
|
- id: test
|
||||||
order: 10
|
order: 0
|
||||||
application: dubbo-exchange
|
application: dubbo-exchange
|
||||||
methods: # 如果没填就从 request拿 意味着所有接口都可以使用
|
methods: # 如果没填就从 request拿 意味着所有接口都可以使用
|
||||||
|
|
||||||
- name: Say
|
- name: Say
|
||||||
param-types:
|
param-types:
|
||||||
- java.lang.String
|
- java.lang.String
|
||||||
|
|
||||||
- name: Hello
|
- name: Hello
|
||||||
|
|
||||||
connections: 4
|
connections: 4
|
||||||
group: test
|
group: test
|
||||||
# registry: zookeeper://127.0.0.1:2181
|
# registry: zookeeper://127.0.0.1:2181
|
||||||
|
|
|
@ -38,16 +38,6 @@ public class TestExtract {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestUriQueryList() {
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user