Skip to content

Commit

Permalink
[ISSUE alibaba#8934] add header back after convert request to jsonBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorHe1 committed Aug 10, 2022
1 parent 3586ad6 commit ab02997
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.google.protobuf.UnsafeByteOperations;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

/**
* gRPC utils, use to parse request and response.
Expand Down Expand Up @@ -59,8 +61,7 @@ public static Payload convert(Request request, RequestMeta meta) {
payloadBuilder.setMetadata(metaBuilder.build());

// request body .
request.clearHeaders();
byte[] jsonBytes = JacksonUtils.toJsonBytes(request);
byte[] jsonBytes = convertRequestToByte(request);
return payloadBuilder
.setBody(Any.newBuilder().setValue(UnsafeByteOperations.unsafeWrap(jsonBytes)))
.build();
Expand All @@ -77,11 +78,11 @@ public static Payload convert(Request request) {

Metadata newMeta = Metadata.newBuilder().setType(request.getClass().getSimpleName())
.setClientIp(NetUtils.localIP()).putAllHeaders(request.getHeaders()).build();
request.clearHeaders();
byte[] jsonBytes = JacksonUtils.toJsonBytes(request);

byte[] jsonBytes = convertRequestToByte(request);

Payload.Builder builder = Payload.newBuilder();

return builder
.setBody(Any.newBuilder().setValue(UnsafeByteOperations.unsafeWrap(jsonBytes)))
.setMetadata(newMeta).build();
Expand All @@ -103,6 +104,14 @@ public static Payload convert(Response response) {
.setMetadata(metaBuilder.build()).build();
}

private static byte[] convertRequestToByte(Request request) {
Map<String, String> requestHeaders = new HashMap<>(request.getHeaders());
request.clearHeaders();
byte[] jsonBytes = JacksonUtils.toJsonBytes(request);
request.putAllHeader(requestHeaders);
return jsonBytes;
}

/**
* parse payload to request/response model.
*
Expand Down

0 comments on commit ab02997

Please sign in to comment.