Skip to content

Commit

Permalink
Support more content types for Triple protocol (apache#13387)
Browse files Browse the repository at this point in the history
* Add more content type support for triple

* Add more content type support for triple

* Add tests & Bug fix

* Code style fix

* Code style fix

* Code style fix

* Code style fix

* Code style fix

* Set codec related dependencies to provided

* Change CodecUtil to bean

* Support Triple response encode by Accept header

* Support Triple response encode by Accept header

* Fix npe

* Refactor response encode

* Bug fix

* Style fix

* Bug fix & Add log

* Style fix

* Bug fix

* Update ExceptionUtilsTest.java

* Refactor

* Fix code style

* Refactor

* Refactor

* Code style fix

* Refactor

* Refactor

* Code style optimize

* Code style optimize

* Refactor MultipartCodec

* Add tests & Remove some dep

* Remove commons_fileupload from bom

* Simplify depedencies & clean up

* Fix jaxb version

* Enhance reliability for MultipartCodec & Add tests

* Enhance reliability for MultipartCodec

* Add test cases

* Add test cases

* Add test cases

* Add test cases

* Add test for xml safety

* Add test for xml safety

* Refactor CodecFactory

* Refactor CodecFactory

* Refactor CodecFactory

* Add codec cache

* Fix npe

* Fix npe

---------

Co-authored-by: nameless <x1544669126@gmail.com>
  • Loading branch information
namelessssssssssss and nameless authored Dec 16, 2023
1 parent 2f4a78e commit 6118aa5
Show file tree
Hide file tree
Showing 52 changed files with 1,838 additions and 254 deletions.
12 changes: 12 additions & 0 deletions dubbo-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@
<palantirJavaFormat.version>2.38.0</palantirJavaFormat.version>
<revision>3.3.0-beta.2-SNAPSHOT</revision>
<open_feign_version>3.1.5</open_feign_version>
<jakarta.xml.bind-api.version>4.0.1</jakarta.xml.bind-api.version>
<jaxb-runtime.version>2.3.3-b02</jaxb-runtime.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -1073,6 +1075,16 @@
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>${open_feign_version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${jakarta.xml.bind-api.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>${jaxb-runtime.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
4 changes: 2 additions & 2 deletions dubbo-distribution/dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,10 @@
<resource>META-INF/dubbo/internal/org.apache.dubbo.remoting.http.HttpBinder</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageCodec</resource>
<resource>META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageEncoderFactory</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageCodecFactory</resource>
<resource>META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageDecoderFactory</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.h2.Http2ServerTransportListenerFactory</resource>
Expand Down
9 changes: 9 additions & 0 deletions dubbo-remoting/dubbo-remoting-http12/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,14 @@
<artifactId>netty-codec-http2</artifactId>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.apache.dubbo.remoting.http12.exception.EncodeException;
import org.apache.dubbo.remoting.http12.exception.HttpStatusException;
import org.apache.dubbo.remoting.http12.message.HttpMessageCodec;
import org.apache.dubbo.remoting.http12.message.HttpMessageEncoder;

public abstract class AbstractServerHttpChannelObserver implements CustomizableHttpChannelObserver<Object> {

Expand All @@ -32,18 +32,18 @@ public abstract class AbstractServerHttpChannelObserver implements CustomizableH

private boolean headerSent;

private HttpMessageCodec httpMessageCodec;
private HttpMessageEncoder responseEncoder;

public AbstractServerHttpChannelObserver(HttpChannel httpChannel) {
this.httpChannel = httpChannel;
}

public void setHttpMessageCodec(HttpMessageCodec httpMessageCodec) {
this.httpMessageCodec = httpMessageCodec;
public void setResponseEncoder(HttpMessageEncoder responseEncoder) {
this.responseEncoder = responseEncoder;
}

protected HttpMessageCodec getHttpMessageCodec() {
return httpMessageCodec;
public HttpMessageEncoder getResponseEncoder() {
return responseEncoder;
}

@Override
Expand Down Expand Up @@ -77,7 +77,7 @@ public void onNext(Object data) {
}
HttpOutputMessage outputMessage = encodeHttpOutputMessage(data);
preOutputMessage(outputMessage);
this.httpMessageCodec.encode(outputMessage.getBody(), data);
this.responseEncoder.encode(outputMessage.getBody(), data);
getHttpChannel().writeMessage(outputMessage);
postOutputMessage(outputMessage);
} catch (Throwable e) {
Expand Down Expand Up @@ -114,7 +114,7 @@ public void onError(Throwable throwable) {
errorResponse.setMessage(throwable.getMessage());
this.errorResponseCustomizer.accept(errorResponse, throwable);
HttpOutputMessage httpOutputMessage = encodeHttpOutputMessage(errorResponse);
this.httpMessageCodec.encode(httpOutputMessage.getBody(), errorResponse);
this.responseEncoder.encode(httpOutputMessage.getBody(), errorResponse);
getHttpChannel().writeMessage(httpOutputMessage);
} catch (Throwable ex) {
throwable = new EncodeException(ex);
Expand All @@ -140,7 +140,7 @@ private void doSendHeaders(String statusCode) {
.headers()
.set(
HttpHeaderNames.CONTENT_TYPE.getName(),
httpMessageCodec.contentType().getName());
responseEncoder.mediaType().getName());
this.headersCustomizer.accept(httpMetadata.headers());
getHttpChannel().writeHeader(httpMetadata);
this.headerSent = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public enum HttpHeaderNames {

TRANSFER_ENCODING("transfer-encoding"),

TE("te");
TE("te"),

ACCEPT("accept");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public Map<String, String> toSingleValueMap() {
return result;
}

public String getContentType() {
return getFirst(HttpHeaderNames.CONTENT_TYPE.getName());
}

public String getAccept() {
return getFirst(HttpHeaderNames.ACCEPT.getName());
}

@Override
public boolean equals(Object other) {
return (this == other || this.targetMap.equals(other));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting.http12.message;

public interface CodecMediaType {

MediaType mediaType();

default boolean supports(String mediaType) {
return mediaType.startsWith(mediaType().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

public class DefaultListeningDecoder implements ListeningDecoder {

private final HttpMessageCodec httpMessageCodec;
private final HttpMessageDecoder httpMessageDecoder;

private final Class<?>[] targetTypes;

private Listener listener;

public DefaultListeningDecoder(HttpMessageCodec httpMessageCodec, Class<?>[] targetTypes) {
this.httpMessageCodec = httpMessageCodec;
public DefaultListeningDecoder(HttpMessageDecoder httpMessageDecoder, Class<?>[] targetTypes) {
this.httpMessageDecoder = httpMessageDecoder;
this.targetTypes = targetTypes;
}

Expand All @@ -38,7 +38,7 @@ public void setListener(Listener listener) {

@Override
public void decode(InputStream inputStream) {
Object[] decode = this.httpMessageCodec.decode(inputStream, targetTypes);
Object[] decode = this.httpMessageDecoder.decode(inputStream, targetTypes);
this.listener.onMessage(decode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,7 @@
*/
package org.apache.dubbo.remoting.http12.message;

import org.apache.dubbo.common.extension.ExtensionScope;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.remoting.http12.exception.DecodeException;
import org.apache.dubbo.remoting.http12.exception.EncodeException;

import java.io.InputStream;
import java.io.OutputStream;

/**
* for http body codec
*/
@SPI(scope = ExtensionScope.FRAMEWORK)
public interface HttpMessageCodec {

void encode(OutputStream outputStream, Object data) throws EncodeException;

default void encode(OutputStream outputStream, Object[] data) throws EncodeException {
// default encode first data
this.encode(outputStream, data == null || data.length == 0 ? null : data[0]);
}

Object decode(InputStream inputStream, Class<?> targetType) throws DecodeException;

default Object[] decode(InputStream inputStream, Class<?>[] targetTypes) throws DecodeException {
// default decode first target type
return new Object[] {
this.decode(inputStream, targetTypes == null || targetTypes.length == 0 ? null : targetTypes[0])
};
}

MediaType contentType();

default boolean support(String contentType) {
MediaType mediaType = this.contentType();
return mediaType.getName().startsWith(contentType);
}
}
public interface HttpMessageCodec extends HttpMessageEncoder, HttpMessageDecoder {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting.http12.message;

import org.apache.dubbo.remoting.http12.exception.DecodeException;

import java.io.InputStream;

public interface HttpMessageDecoder extends CodecMediaType {

Object decode(InputStream inputStream, Class<?> targetType) throws DecodeException;

default Object[] decode(InputStream inputStream, Class<?>[] targetTypes) throws DecodeException {
// default decode first target type
return new Object[] {
this.decode(inputStream, targetTypes == null || targetTypes.length == 0 ? null : targetTypes[0])
};
}

MediaType mediaType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.rpc.model.FrameworkModel;

/**
* for http body codec
*/
@SPI(scope = ExtensionScope.FRAMEWORK)
public interface HttpMessageCodecFactory {

HttpMessageCodec createCodec(URL url, FrameworkModel frameworkModel);

MediaType contentType();
public interface HttpMessageDecoderFactory extends CodecMediaType {

default boolean support(String contentType) {
MediaType mediaType = this.contentType();
return mediaType.getName().startsWith(contentType);
}
HttpMessageDecoder createCodec(URL url, FrameworkModel frameworkModel, String mediaType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.remoting.http12.message;

import org.apache.dubbo.remoting.http12.exception.EncodeException;

import java.io.OutputStream;

public interface HttpMessageEncoder extends CodecMediaType {

void encode(OutputStream outputStream, Object data) throws EncodeException;

default void encode(OutputStream outputStream, Object[] data) throws EncodeException {
// default encode first data
this.encode(outputStream, data == null || data.length == 0 ? null : data[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@
package org.apache.dubbo.remoting.http12.message;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.extension.ExtensionScope;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.rpc.model.FrameworkModel;

@Activate
public class JsonCodecFactory implements HttpMessageCodecFactory {
@SPI(scope = ExtensionScope.FRAMEWORK)
public interface HttpMessageEncoderFactory extends CodecMediaType {

public static final String NAME = "json";

@Override
public HttpMessageCodec createCodec(URL url, FrameworkModel frameworkModel) {
return new JsonCodec();
}

@Override
public MediaType contentType() {
return MediaType.APPLICATION_JSON_VALUE;
}
HttpMessageEncoder createCodec(URL url, FrameworkModel frameworkModel, String mediaType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public class MediaType {

public static final MediaType TEXT_EVENT_STREAM_VALUE = new MediaType("text", "event-stream");

public static final MediaType MULTIPART_FORM_DATA = new MediaType("multipart", "form-data");

public static final MediaType APPLICATION_X_WWW_FROM_URLENCODED =
new MediaType("application", "x-www-form-urlencoded");

public static final MediaType APPLICATION_XML = new MediaType("application", "xml");

public static final MediaType TEXT_PLAIN = new MediaType("text", "plain");

private final String name;

private final String type;
Expand Down
Loading

0 comments on commit 6118aa5

Please sign in to comment.