use of io.github.api7.A6.TextEntry in project apisix-java-plugin-runner by apache.
the class HttpRequest method getArgs.
/**
* Gets all args.
*
* @return the args
*/
public Map<String, String> getArgs() {
if (Objects.isNull(args)) {
args = new HashMap<>();
for (int i = 0; i < req.argsLength(); i++) {
TextEntry arg = req.args(i);
args.put(arg.name(), arg.value());
}
}
return args;
}
use of io.github.api7.A6.TextEntry in project apisix-java-plugin-runner by apache.
the class HttpRequest method getHeader.
/**
* Gets all headers.
* <p>Examples:</p>
*
* <pre>
* {@code
* request.getHeaders()
* }
* </pre>
*
* @return the all headers
*/
public Map<String, String> getHeader() {
if (Objects.isNull(headers)) {
headers = new HashMap<>();
for (int i = 0; i < req.headersLength(); i++) {
TextEntry header = req.headers(i);
headers.put(header.name(), header.value());
}
}
return headers;
}
use of io.github.api7.A6.TextEntry in project apisix-java-plugin-runner by apache.
the class PayloadDecoderTest method testGetBody.
@Test
@DisplayName("test get body")
void testGetBody() {
// mock client assembly data
FlatBufferBuilder builder = new FlatBufferBuilder();
int foo = builder.createString("foo");
int bar = builder.createString("bar");
int confIndex = TextEntry.createTextEntry(builder, foo, bar);
int vector = io.github.api7.A6.PrepareConf.Req.createConfVector(builder, new int[] { confIndex });
io.github.api7.A6.PrepareConf.Req.startReq(builder);
io.github.api7.A6.PrepareConf.Req.addConf(builder, vector);
builder.finish(io.github.api7.A6.PrepareConf.Req.endReq(builder));
byte[] data = new byte[builder.dataBuffer().remaining()];
builder.dataBuffer().get(data, 0, data.length);
// use the correct data length
byte[] header = new byte[] { 1, 0, 0, (byte) data.length };
byte[] bytes = new byte[header.length + data.length];
// assembly data format
System.arraycopy(header, 0, bytes, 0, header.length);
System.arraycopy(data, 0, bytes, header.length, data.length);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
A6ConfigRequest configReq = (A6ConfigRequest) payloadDecoder.decode(buffer);
for (int i = 0; i < configReq.getReq().confLength(); i++) {
TextEntry conf = configReq.getReq().conf(i);
Assertions.assertEquals("foo", conf.name());
Assertions.assertEquals("bar", conf.value());
}
}
use of io.github.api7.A6.TextEntry in project apisix-java-plugin-runner by apache.
the class PrepareConfHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, A6Request request) {
if (request.getType() != Constants.RPC_PREPARE_CONF) {
ctx.fireChannelRead(request);
return;
}
Req req = ((A6ConfigRequest) request).getReq();
long confToken = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
A6Response response = new A6ConfigResponse(confToken);
long token = ((A6ConfigResponse) response).getConfToken();
PluginFilterChain chain = createFilterChain(req);
/*
* to reset vtable_start and vtable_size of req,
* so that req can be reused after being got from the cache.
* {@link org.apache.apisix.plugin.runner.handler.A6HttpCallHandler#handle cache.getIfPresent()}
* @see <a href="Issues63"> https://github.com/apache/apisix-java-plugin-runner/issues/63</a>
* */
Map<String, String> config = new HashMap<>();
for (int i = 0; i < req.confLength(); i++) {
TextEntry conf = req.conf(i);
config.put(conf.name(), conf.value());
}
A6Conf a6Conf = new A6Conf(config, chain);
cache.put(token, a6Conf);
ctx.write(response);
ctx.writeAndFlush(response);
}
use of io.github.api7.A6.TextEntry in project apisix-java-plugin-runner by apache.
the class PrepareConfHandler method createFilterChain.
private PluginFilterChain createFilterChain(Req req) {
List<PluginFilter> chainFilters = new ArrayList<>();
for (int i = 0; i < req.confLength(); i++) {
TextEntry conf = req.conf(i);
PluginFilter filter = filters.get(conf.name());
if (Objects.isNull(filter)) {
logger.warn("receive undefined filter: {}, skip it", conf.name());
continue;
}
if (chainFilters.contains(filter)) {
logger.warn("skip the same filter: {}", conf.name());
continue;
}
chainFilters.add(filter);
}
return new PluginFilterChain(chainFilters);
}
Aggregations