Search in sources :

Example 46 with FlatBufferBuilder

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);
}
Also used : HttpRequest(org.apache.apisix.plugin.runner.HttpRequest) PrintStream(java.io.PrintStream) A6ConfigResponse(org.apache.apisix.plugin.runner.A6ConfigResponse) PluginFilter(org.apache.apisix.plugin.runner.filter.PluginFilter) HttpResponse(org.apache.apisix.plugin.runner.HttpResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) A6ConfigRequest(org.apache.apisix.plugin.runner.A6ConfigRequest) PluginFilterChain(org.apache.apisix.plugin.runner.filter.PluginFilterChain) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) List(java.util.List) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 47 with FlatBufferBuilder

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();
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder)

Example 48 with FlatBufferBuilder

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());
    }
}
Also used : A6ConfigRequest(org.apache.apisix.plugin.runner.A6ConfigRequest) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) TextEntry(io.github.api7.A6.TextEntry) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 49 with FlatBufferBuilder

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));
}
Also used : A6ConfigRequest(org.apache.apisix.plugin.runner.A6ConfigRequest) FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 50 with FlatBufferBuilder

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);
    }
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)51 DisplayName (org.junit.jupiter.api.DisplayName)14 Test (org.junit.jupiter.api.Test)14 BigInteger (java.math.BigInteger)10 A6ConfigRequest (org.apache.apisix.plugin.runner.A6ConfigRequest)9 HttpRequest (org.apache.apisix.plugin.runner.HttpRequest)9 ByteBuffer (java.nio.ByteBuffer)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)7 A6ConfigResponse (org.apache.apisix.plugin.runner.A6ConfigResponse)7 Test (org.junit.Test)7 Req (io.github.api7.A6.PrepareConf.Req)5 A6Conf (org.apache.apisix.plugin.runner.A6Conf)5 ByteStringAccess (com.google.protobuf.ByteStringAccess)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 UncheckedDeephavenException (io.deephaven.UncheckedDeephavenException)3 ChunkType (io.deephaven.chunk.ChunkType)3 Map (java.util.Map)3 lombok.val (lombok.val)3 ExtraInfoRequest (org.apache.apisix.plugin.runner.ExtraInfoRequest)3 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)3