Skip to content
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 @@ -160,6 +160,10 @@ public Type actualReflectType() {
return actualType;
}

public void setActualType(Type actualType) {
this.actualType = actualType;
}

@Override
public String toString() {
return "ArgInfo{" + "index="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.ParameterTypesComparator;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.RestMethodMetadata;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.metadata.rest.media.MediaType;
Expand Down Expand Up @@ -109,11 +110,10 @@ protected Result doInvoke(Invocation invocation) {
Method reflectMethod = restMethodMetadata.getReflectMethod();
mediaType =
MediaTypeUtil.convertMediaType(reflectMethod.getReturnType(), r.getContentType());
Object value = HttpMessageCodecManager.httpMessageDecode(
r.getBody(),
reflectMethod.getReturnType(),
reflectMethod.getGenericReturnType(),
mediaType);
ArgInfo argInfo = new ArgInfo();
argInfo.setParamType(reflectMethod.getReturnType());
argInfo.setActualType(reflectMethod.getGenericReturnType());
Object value = HttpMessageCodecManager.httpMessageDecode(r.getBody(), argInfo, mediaType);
appResponse.setValue(value);
// resolve response attribute & attachment
HttpHeaderUtil.parseResponseHeader(appResponse, r);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected void doParse(ProviderParseContext parseContext, ArgInfo argInfo) {
try {
String contentType = parseContext.getRequestFacade().getHeader(RestHeaderEnum.CONTENT_TYPE.getHeader());
MediaType mediaType = MediaTypeUtil.convertMediaType(argInfo.getParamType(), contentType);
Object param = HttpMessageCodecManager.httpMessageDecode(
request.getInputStream(), argInfo.getParamType(), argInfo.actualReflectType(), mediaType);
Object param = HttpMessageCodecManager.httpMessageDecode(request.getInputStream(), argInfo, mediaType);
parseContext.setValueByIndex(argInfo.getIndex(), param);
} catch (Throwable e) {
throw new ParamParseException("dubbo rest protocol provider body param parser error: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ protected void doParse(ProviderParseContext parseContext, ArgInfo argInfo) {
String param = request.getParameter(argInfo.getAnnotationNameAttribute());

Object paramValue = paramTypeConvert(argInfo.getParamType(), param);
parseContext.setValueByIndex(argInfo.getIndex(), paramValue);
if (paramValue != null) {
parseContext.setValueByIndex(argInfo.getIndex(), paramValue);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@
package org.apache.dubbo.rpc.protocol.rest.message;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.rest.exception.UnSupportContentTypeException;
import org.apache.dubbo.rpc.protocol.rest.pair.MessageCodecResultPair;

import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Set;

public class HttpMessageCodecManager {
private static final Set<HttpMessageCodec> httpMessageCodecs = FrameworkModel.defaultModel()
.getExtensionLoader(HttpMessageCodec.class)
.getSupportedExtensionInstances();

public static Object httpMessageDecode(byte[] body, Class<?> type, Type actualType, MediaType mediaType)
throws Exception {
public static Object httpMessageDecode(byte[] body, ArgInfo argInfo, MediaType mediaType) throws Exception {
Class<?> type = argInfo.getParamType();
if (body == null || body.length == 0) {
return null;
}

for (HttpMessageCodec httpMessageCodec : httpMessageCodecs) {
if (httpMessageCodec.contentTypeSupport(mediaType, type) || typeJudge(mediaType, type, httpMessageCodec)) {
return httpMessageCodec.decode(body, type, actualType);
return httpMessageCodec.decode(body, argInfo);
}
}
throw new UnSupportContentTypeException("UnSupport content-type :" + mediaType.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.dubbo.rpc.protocol.rest.message;

import java.lang.reflect.Type;
import org.apache.dubbo.metadata.rest.ArgInfo;

public interface HttpMessageDecode<InputStream> {

Object decode(InputStream body, Class<?> targetType, Type actualTYpe) throws Exception;
Object decode(InputStream body, ArgInfo argInfo) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;

import java.io.OutputStream;
import java.lang.reflect.Type;

/**
* body type is byte array
Expand All @@ -31,7 +31,7 @@
public class ByteArrayCodec implements HttpMessageCodec<byte[], OutputStream> {

@Override
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
return body;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;

import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -46,8 +46,8 @@ public static void addUnSupportClass(Class<?> unSupportClass) {
}

@Override
public Object decode(byte[] body, Class<?> targetType, Type actualType) throws Exception {
return DataParseUtils.jsonConvert(actualType, body);
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
return DataParseUtils.jsonConvert(argInfo.actualReflectType(), body);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* body is form
Expand All @@ -39,17 +37,15 @@
public class MultiValueCodec implements HttpMessageCodec<byte[], OutputStream> {

@Override
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
Class<?> targetType = argInfo.getParamType();
Object map = DataParseUtils.multipartFormConvert(body, targetType);
Map valuesMap = (Map) map;
if (Map.class.isAssignableFrom(targetType)) {
return map;
} else if (DataParseUtils.isTextType(targetType)) {

// only fetch first
Set set = valuesMap.keySet();
ArrayList arrayList = new ArrayList<>(set);
Object key = arrayList.get(0);
String key = argInfo.getParamName();
Object value = valuesMap.get(key);
if (value == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;

import java.io.OutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

@Activate(onClass = "javax.ws.rs.core.Response")
Expand Down Expand Up @@ -58,7 +58,7 @@ public MediaType contentType() {
}

@Override
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
if (null == body || body.length == 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;

import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

/**
Expand All @@ -32,7 +32,7 @@
public class StringCodec implements HttpMessageCodec<byte[], OutputStream> {

@Override
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
if (body == null || body.length == 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;

import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

/**
Expand All @@ -34,8 +34,8 @@
public class TextCodec implements HttpMessageCodec<byte[], OutputStream> {

@Override
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
return DataParseUtils.stringTypeConvert(targetType, new String(body, StandardCharsets.UTF_8));
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
return DataParseUtils.stringTypeConvert(argInfo.getParamType(), new String(body, StandardCharsets.UTF_8));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
Expand All @@ -31,7 +32,6 @@

import java.io.OutputStream;
import java.io.StringReader;
import java.lang.reflect.Type;

import org.xml.sax.InputSource;

Expand All @@ -42,7 +42,7 @@
public class XMLCodec implements HttpMessageCodec<byte[], OutputStream> {

@Override
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
Expand All @@ -53,7 +53,7 @@ public Object decode(byte[] body, Class<?> targetType, Type type) throws Excepti
Source xmlSource =
new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(new StringReader(new String(body))));

JAXBContext context = JAXBContext.newInstance(targetType);
JAXBContext context = JAXBContext.newInstance(argInfo.getParamType());
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(xmlSource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.protocol.rest;

import org.apache.dubbo.metadata.rest.ArgInfo;
import org.apache.dubbo.metadata.rest.media.MediaType;
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodecManager;
import org.apache.dubbo.rpc.protocol.rest.message.codec.XMLCodec;
Expand All @@ -38,11 +39,11 @@ void testCodec() throws Exception {
HttpMessageCodecManager.httpMessageEncode(
byteArrayOutputStream, registrationResult, null, MediaType.TEXT_XML, null);

ArgInfo argInfo = new ArgInfo();
argInfo.setActualType(RegistrationResult.class);
argInfo.setParamType(RegistrationResult.class);
Object o = HttpMessageCodecManager.httpMessageDecode(
byteArrayOutputStream.toByteArray(),
RegistrationResult.class,
RegistrationResult.class,
MediaType.TEXT_XML);
byteArrayOutputStream.toByteArray(), argInfo, MediaType.TEXT_XML);

Assertions.assertEquals(registrationResult, o);

Expand Down