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

A couple of rest bugfix #14822

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ protected Object resolveCollectionValue(NamedValueMeta meta, HttpRequest request

@Override
protected Object resolveMapValue(NamedValueMeta meta, HttpRequest request, HttpResponse response) {
return request.headers();
return request.headers().asMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ protected Object resolveCollectionValue(NamedValueMeta meta, HttpRequest request

@Override
protected Object resolveMapValue(NamedValueMeta meta, HttpRequest request, HttpResponse response) {
return request.headers();
return request.headers().asMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.protocol.tri.TripleConstants;

@Activate(group = CommonConstants.PROVIDER, order = 29000)
@Activate(group = CommonConstants.PROVIDER, order = 10)
public class HttpContextCallbackFilter implements Filter, BaseFilter.Listener {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.dubbo.remoting.http12.message.HttpMessageEncoder;
import org.apache.dubbo.remoting.http12.message.MediaType;
import org.apache.dubbo.rpc.protocol.tri.rest.argument.ArgumentResolver;
import org.apache.dubbo.rpc.protocol.tri.rest.argument.TypeConverter;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.ParameterMeta;

import java.io.ByteArrayInputStream;
Expand All @@ -34,6 +35,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Optional;

public final class RestHttpMessageCodec implements HttpMessageDecoder, HttpMessageEncoder {

Expand All @@ -43,6 +45,7 @@ public final class RestHttpMessageCodec implements HttpMessageDecoder, HttpMessa
private final HttpResponse response;
private final ParameterMeta[] parameters;
private final ArgumentResolver argumentResolver;
private final TypeConverter typeConverter;
private final HttpMessageEncoder messageEncoder;
private final Charset charset;

Expand All @@ -51,11 +54,13 @@ public RestHttpMessageCodec(
HttpResponse response,
ParameterMeta[] parameters,
ArgumentResolver argumentResolver,
TypeConverter typeConverter,
HttpMessageEncoder messageEncoder) {
this.request = request;
this.response = response;
this.parameters = parameters;
this.argumentResolver = argumentResolver;
this.typeConverter = typeConverter;
this.messageEncoder = messageEncoder;
charset = request.charsetOrDefault();
}
Expand Down Expand Up @@ -106,6 +111,10 @@ private InputStream decodeInputStream(InputStream is) {
public void encode(OutputStream os, Object data) throws EncodeException {
if (data != null) {
Class<?> type = data.getClass();
if (type == Optional.class) {
encode(os, ((Optional<?>) data).orElse(null));
return;
}
try {
if (type == byte[].class) {
os.write((byte[]) data);
Expand All @@ -121,6 +130,9 @@ public void encode(OutputStream os, Object data) throws EncodeException {
}
return;
}
if (messageEncoder.mediaType().isPureText() && type != String.class) {
data = typeConverter.convert(data, String.class);
}
} catch (HttpStatusException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.apache.dubbo.rpc.protocol.tri.rest.RestHttpMessageCodec;
import org.apache.dubbo.rpc.protocol.tri.rest.argument.ArgumentResolver;
import org.apache.dubbo.rpc.protocol.tri.rest.argument.CompositeArgumentResolver;
import org.apache.dubbo.rpc.protocol.tri.rest.argument.GeneralTypeConverter;
import org.apache.dubbo.rpc.protocol.tri.rest.argument.TypeConverter;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.condition.MethodsCondition;
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.HandlerMeta;
import org.apache.dubbo.rpc.protocol.tri.rest.util.RequestUtils;
Expand All @@ -51,13 +53,15 @@ public final class RestRequestHandlerMapping implements RequestHandlerMapping {

private final RequestMappingRegistry requestMappingRegistry;
private final ArgumentResolver argumentResolver;
private final TypeConverter typeConverter;
private final ContentNegotiator contentNegotiator;
private final CodecUtils codecUtils;

public RestRequestHandlerMapping(FrameworkModel frameworkModel) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
requestMappingRegistry = beanFactory.getOrRegisterBean(DefaultRequestMappingRegistry.class);
argumentResolver = beanFactory.getOrRegisterBean(CompositeArgumentResolver.class);
typeConverter = beanFactory.getOrRegisterBean(GeneralTypeConverter.class);
contentNegotiator = beanFactory.getOrRegisterBean(ContentNegotiator.class);
codecUtils = beanFactory.getOrRegisterBean(CodecUtils.class);
}
Expand Down Expand Up @@ -98,6 +102,7 @@ public RequestHandler getRequestHandler(URL url, HttpRequest request, HttpRespon
response,
meta.getParameters(),
argumentResolver,
typeConverter,
codecUtils.determineHttpMessageEncoder(url, responseMediaType));

if (HttpMethods.supportBody(method) && !RequestUtils.isFormOrMultiPart(request)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected Object resolveMapValue(NamedValueMeta meta, HttpRequest request, HttpR
case Param:
return RequestUtils.getParametersMap(request);
case Header:
return request.headers();
return request.headers().asMap();
case Cookie:
Collection<HttpCookie> cookies = request.cookies();
if (cookies.isEmpty()) {
Expand Down
Loading