use of com.google.flatbuffers.FlatBufferBuilder in project apisix-java-plugin-runner by apache.
the class ExtraInfoTest method setUp.
@BeforeEach
void setUp() {
bytes = new ByteArrayOutputStream();
console = System.out;
System.setOut(new PrintStream(bytes));
filters = new HashMap<>();
filters.put("FooFilter", new PluginFilter() {
@Override
public String name() {
return "FooFilter";
}
@Override
@SuppressWarnings("unchecked")
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
String remote_addr = request.getVars("remote_addr");
String server_port = request.getVars("server_port");
System.out.println("remote_addr: " + remote_addr);
System.out.println("server_port: " + server_port);
chain.filter(request, response);
}
@Override
public List<String> requiredVars() {
return List.of("remote_addr", "server_port");
}
@Override
public Boolean requiredBody() {
return false;
}
});
filters.put("CatFilter", new PluginFilter() {
@Override
public String name() {
return "CatFilter";
}
@Override
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
String body = request.getBody();
String content_type = request.getVars("content_type");
System.out.println("content_type: " + content_type);
System.out.println("body: " + body);
chain.filter(request, response);
}
@Override
public List<String> requiredVars() {
return List.of("content_type");
}
@Override
public Boolean requiredBody() {
return true;
}
});
cache = CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000).build();
FlatBufferBuilder builder = new FlatBufferBuilder();
int foo = builder.createString("FooFilter");
int bar = builder.createString("{\"conf_key1\":\"conf_value1\",\"conf_key2\":2}");
int filter1 = TextEntry.createTextEntry(builder, foo, bar);
int cat = builder.createString("CatFilter");
int dog = builder.createString("Dog");
int filter2 = TextEntry.createTextEntry(builder, cat, dog);
int confVector = io.github.api7.A6.PrepareConf.Req.createConfVector(builder, new int[] { filter1, filter2 });
io.github.api7.A6.PrepareConf.Req.startReq(builder);
io.github.api7.A6.PrepareConf.Req.addConf(builder, confVector);
builder.finish(io.github.api7.A6.PrepareConf.Req.endReq(builder));
io.github.api7.A6.PrepareConf.Req req = io.github.api7.A6.PrepareConf.Req.getRootAsReq(builder.dataBuffer());
A6ConfigRequest request = new A6ConfigRequest(req);
prepareConfHandler = new PrepareConfHandler(cache, filters);
channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler);
channel.writeInbound(request);
channel.finish();
A6ConfigResponse response = channel.readOutbound();
confToken = response.getConfToken();
prepareConfHandler = new PrepareConfHandler(cache, filters);
httpReqCallHandler = new HTTPReqCallHandler(cache);
channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler, httpReqCallHandler);
}
use of com.google.flatbuffers.FlatBufferBuilder in project apisix-java-plugin-runner by apache.
the class ExtraInfoRequest method encode.
@Override
public ByteBuffer encode() {
FlatBufferBuilder builder = new FlatBufferBuilder();
if (var != null) {
int nameOffset = builder.createString(var);
io.github.api7.A6.ExtraInfo.Var.startVar(builder);
io.github.api7.A6.ExtraInfo.Var.addName(builder, nameOffset);
int endVar = io.github.api7.A6.ExtraInfo.Var.endVar(builder);
buildExtraInfo(endVar, Info.Var, builder);
}
if (this.reqBody != null && this.reqBody) {
io.github.api7.A6.ExtraInfo.ReqBody.startReqBody(builder);
int reqBodyReq = ReqBody.endReqBody(builder);
buildExtraInfo(reqBodyReq, Info.ReqBody, builder);
}
builder.finish(Req.endReq(builder));
return builder.dataBuffer();
}
use of com.google.flatbuffers.FlatBufferBuilder 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 com.google.flatbuffers.FlatBufferBuilder in project apisix-java-plugin-runner by apache.
the class PayloadDecoderTest method testGetBody2.
@Test
@DisplayName("test get body with error data length")
void testGetBody2() {
// {"name":"foo", "value":"bar"}
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);
// se the error data length
byte errDateLength = (byte) (data.length / 2);
byte[] header = new byte[] { 1, 0, 0, errDateLength };
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);
assertThrows(IndexOutOfBoundsException.class, () -> configReq.getReq().conf(0));
}
use of com.google.flatbuffers.FlatBufferBuilder in project SeriesGuide by UweTrottmann.
the class FlatbufferTest method createAndReadBuffer.
@Test
public void createAndReadBuffer() {
FlatBufferBuilder builder = new FlatBufferBuilder(0);
int[] episodeInfos = new int[42];
for (int i = 0; i < 21; i++) {
episodeInfos[i] = EpisodeInfo.createEpisodeInfo(builder, 1, i + 1, i);
}
for (int i = 21; i < 42; i++) {
episodeInfos[i] = EpisodeInfo.createEpisodeInfo(builder, 2, i + 1, i);
}
int episodes = SgJobInfo.createEpisodesVector(builder, episodeInfos);
int jobInfo = SgJobInfo.createSgJobInfo(builder, 1, episodes, 0, 0, 42);
builder.finish(jobInfo);
byte[] bytes = builder.sizedByteArray();
ByteBuffer bufferReloaded = ByteBuffer.wrap(bytes);
SgJobInfo jobInfoReloaded = SgJobInfo.getRootAsSgJobInfo(bufferReloaded);
assertThat(jobInfoReloaded.showId()).isEqualTo(42);
assertThat(jobInfoReloaded.flagValue()).isEqualTo(1);
assertThat(jobInfoReloaded.episodesLength()).isEqualTo(42);
for (int i = 0; i < 21; i++) {
EpisodeInfo episodeInfo = jobInfoReloaded.episodes(i);
assertThat(episodeInfo.season()).isEqualTo(1);
assertThat(episodeInfo.number()).isEqualTo(i + 1);
assertThat(episodeInfo.plays()).isEqualTo(i);
}
for (int i = 21; i < 42; i++) {
EpisodeInfo episodeInfo = jobInfoReloaded.episodes(i);
assertThat(episodeInfo.season()).isEqualTo(2);
assertThat(episodeInfo.number()).isEqualTo(i + 1);
assertThat(episodeInfo.plays()).isEqualTo(i);
}
}
Aggregations