Search in sources :

Example 1 with TextEntry

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;
}
Also used : TextEntry(io.github.api7.A6.TextEntry)

Example 2 with TextEntry

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;
}
Also used : TextEntry(io.github.api7.A6.TextEntry)

Example 3 with TextEntry

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());
    }
}
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 4 with TextEntry

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);
}
Also used : A6Response(org.apache.apisix.plugin.runner.A6Response) A6ConfigRequest(org.apache.apisix.plugin.runner.A6ConfigRequest) A6ConfigResponse(org.apache.apisix.plugin.runner.A6ConfigResponse) PluginFilterChain(org.apache.apisix.plugin.runner.filter.PluginFilterChain) HashMap(java.util.HashMap) TextEntry(io.github.api7.A6.TextEntry) A6Conf(org.apache.apisix.plugin.runner.A6Conf) Req(io.github.api7.A6.PrepareConf.Req)

Example 5 with TextEntry

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);
}
Also used : PluginFilter(org.apache.apisix.plugin.runner.filter.PluginFilter) PluginFilterChain(org.apache.apisix.plugin.runner.filter.PluginFilterChain) ArrayList(java.util.ArrayList) TextEntry(io.github.api7.A6.TextEntry)

Aggregations

TextEntry (io.github.api7.A6.TextEntry)5 A6ConfigRequest (org.apache.apisix.plugin.runner.A6ConfigRequest)2 PluginFilterChain (org.apache.apisix.plugin.runner.filter.PluginFilterChain)2 FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)1 Req (io.github.api7.A6.PrepareConf.Req)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 A6Conf (org.apache.apisix.plugin.runner.A6Conf)1 A6ConfigResponse (org.apache.apisix.plugin.runner.A6ConfigResponse)1 A6Response (org.apache.apisix.plugin.runner.A6Response)1 PluginFilter (org.apache.apisix.plugin.runner.filter.PluginFilter)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1