Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changs for Bug: https://github.com/apache/apisix-java-plugin-runner/i… #256

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,22 @@

package org.apache.apisix.plugin.runner.handler;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;

import com.google.common.cache.Cache;
import io.github.api7.A6.Err.Code;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.apache.apisix.plugin.runner.A6Conf;
import org.apache.apisix.plugin.runner.A6ErrRequest;
import org.apache.apisix.plugin.runner.A6ErrResponse;
import org.apache.apisix.plugin.runner.A6Request;
import org.apache.apisix.plugin.runner.ExtraInfoRequest;
import org.apache.apisix.plugin.runner.ExtraInfoResponse;
import org.apache.apisix.plugin.runner.HttpRequest;
import org.apache.apisix.plugin.runner.HttpResponse;
import org.apache.apisix.plugin.runner.PostRequest;
import org.apache.apisix.plugin.runner.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import lombok.RequiredArgsConstructor;

import org.apache.apisix.plugin.runner.*;
import org.apache.apisix.plugin.runner.constants.Constants;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.*;
abhi0476 marked this conversation as resolved.
Show resolved Hide resolved

@RequiredArgsConstructor
public class RpcCallHandler extends SimpleChannelInboundHandler<A6Request> {
Expand Down Expand Up @@ -168,7 +151,7 @@ private void handleHttpRespCall(ChannelHandlerContext ctx, PostRequest request)

// save HttpCallRequest
postReq = request;
postResp = new PostResponse(postReq.getRequestId());
postResp = new PostResponse(postReq.getRequestId(), request.getUpstreamHeaders());

confToken = postReq.getConfToken();
A6Conf conf = cache.getIfPresent(confToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void postFilter(PostRequest request, PostResponse response, PluginFilterC
System.out.println("do post filter: UpStreamFilter, order: " + chain.getIndex());
System.out.println("do post filter: UpStreamFilter, conf: " + request.getConfig(this));
System.out.println("do post filter: UpStreamFilter, upstreamStatusCode: " + request.getUpstreamStatusCode());
for (Map.Entry<String, String> header : request.getUpstreamHeaders().entrySet()) {
for (Map.Entry<String, List<String>> header : request.getUpstreamHeaders().entrySet()) {
System.out.println("do post filter: UpStreamFilter, upstreamHeader key: " + header.getKey());
System.out.println("do post filter: UpStreamFilter, upstreamHeader value: " + header.getValue());
}
Expand Down Expand Up @@ -150,6 +150,6 @@ void doPostFilter() {
Assertions.assertTrue(bytes.toString().contains("do post filter: UpStreamFilter, conf: {\"conf_key1\":\"conf_value1\",\"conf_key2\":2}"));
Assertions.assertTrue(bytes.toString().contains("do post filter: UpStreamFilter, upstreamStatusCode: 418"));
Assertions.assertTrue(bytes.toString().contains("do post filter: UpStreamFilter, upstreamHeader key: headerKey"));
Assertions.assertTrue(bytes.toString().contains("do post filter: UpStreamFilter, upstreamHeader value: headerValue"));
Assertions.assertTrue(bytes.toString().contains("do post filter: UpStreamFilter, upstreamHeader value: [headerValue]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ArrayList;

public class PostRequest implements A6Request {
private final Req req;
Expand All @@ -35,7 +37,7 @@ public class PostRequest implements A6Request {

private Map<String, String> config;

private Map<String, String> headers;
private Map<String, List<String>> headers;

private Integer status;

Expand Down Expand Up @@ -76,12 +78,13 @@ public String getConfig(PluginFilter filter) {
return config.getOrDefault(filter.name(), null);
}

public Map<String, String> getUpstreamHeaders() {
public Map<String, List<String>> getUpstreamHeaders() {
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());
headers.putIfAbsent(header.name(), new ArrayList<>());
headers.get(header.name()).add(header.value());
}
}
return headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ArrayList;

public class PostResponse implements A6Response {

Expand All @@ -42,12 +44,13 @@ public class PostResponse implements A6Response {

private Integer statusCode;

private Map<String, String> headers;
private Map<String, List<String>> headers;

private Charset charset;

public PostResponse(long requestId) {
public PostResponse(long requestId, Map<String, List<String>> headers) {
this.requestId = requestId;
this.headers = headers != null ? new HashMap<>(headers) : new HashMap<>();
this.charset = StandardCharsets.UTF_8;
}

Expand All @@ -63,16 +66,27 @@ public ByteBuffer encode() {

int headerIndex = -1;
if (!CollectionUtils.isEmpty(headers)) {
int[] headerTexts = new int[headers.size()];
int hsize = 0;
for (String hkey: headers.keySet()) {
List<String> headerValues = headers.get(hkey);
hsize += CollectionUtils.isEmpty(headerValues) ? 0 : headerValues.size();
}

int[] headerTexts = new int[hsize];
int i = -1;
for (Map.Entry<String, String> header : headers.entrySet()) {
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
int key = builder.createString(header.getKey());
int value = 0;
if (!Objects.isNull(header.getValue())) {
value = builder.createString(header.getValue());
List<String> headerValues = header.getValue();
if (!CollectionUtils.isEmpty(headerValues)) {
for (String hv: headerValues) {
int value = 0;
if (!Objects.isNull(hv)) {
value = builder.createString(hv);
}
int text = TextEntry.createTextEntry(builder, key, value);
headerTexts[++i] = text;
}
}
int text = TextEntry.createTextEntry(builder, key, value);
headerTexts[++i] = text;
}
headerIndex = Resp.createHeadersVector(builder, headerTexts);
}
Expand Down Expand Up @@ -116,7 +130,27 @@ public void setHeader(String headerKey, String headerValue) {
if (Objects.isNull(headers)) {
headers = new HashMap<>();
}
headers.put(headerKey, headerValue);

headers.put(headerKey, new ArrayList<>());
headers.get(headerKey).add(headerValue);
}

public void addHeader(String headerKey, String headerValue) {
abhi0476 marked this conversation as resolved.
Show resolved Hide resolved
if (headerKey == null) {
logger.warn("headerKey is null, ignore it");
return;
}

if (Objects.isNull(headers)) {
headers = new HashMap<>();
}

headers.putIfAbsent(headerKey, new ArrayList<>());
headers.get(headerKey).add(headerValue);
}

public Map<String, List<String>> headers() {
abhi0476 marked this conversation as resolved.
Show resolved Hide resolved
return headers;
}

public void setBody(String body) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -37,8 +39,9 @@ void testEncodeWithSetCharset() {
long requestId = 1L;
String body = "dummy body";
Charset charset = StandardCharsets.UTF_16;
Map<String, List<String>> headers = new HashMap<>();

PostResponse postResponse = new PostResponse(requestId);
PostResponse postResponse = new PostResponse(requestId, headers);
postResponse.setBody(body);
postResponse.setCharset(charset);

Expand All @@ -53,8 +56,9 @@ void testEncodeWithoutSetCharset() {
long requestId = 1L;
String body = "dummy body";
Charset charset = StandardCharsets.UTF_8;
Map<String, List<String>> headers = new HashMap<>();

PostResponse postResponse = new PostResponse(requestId);
PostResponse postResponse = new PostResponse(requestId, headers);
postResponse.setBody(body);

ByteBuffer encoded = postResponse.encode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.apache.apisix.plugin.runner.PostRequest;
import org.apache.apisix.plugin.runner.PostResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
Expand All @@ -39,8 +41,8 @@ public void postFilter(PostRequest request, PostResponse response, PluginFilterC
Map<String, Object> conf = new HashMap<>();
conf = gson.fromJson(configStr, conf.getClass());

Map<String, String> headers = request.getUpstreamHeaders();
String contentType = headers.get("Content-Type");
Map<String, List<String>> headers = request.getUpstreamHeaders();
String contentType = CollectionUtils.isEmpty(headers.get("Content-Type")) ? null : headers.get("Content-Type").get(0);
Integer upstreamStatusCode = request.getUpstreamStatusCode();

response.setStatusCode(Double.valueOf(conf.get("response_code").toString()).intValue());
Expand Down