Skip to content

Commit

Permalink
Support configuration of disallowed content-types
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Sep 25, 2024
1 parent bf51a34 commit d7c11c7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public class RestConfig implements Serializable {
*/
private String jsonFramework;

/**
* The disallowed content-types.
*/
private String[] disallowedContentTypes;

/**
* The cors configuration.
*/
Expand Down Expand Up @@ -133,6 +138,14 @@ public void setJsonFramework(String jsonFramework) {
this.jsonFramework = jsonFramework;
}

public String[] getDisallowedContentTypes() {
return disallowedContentTypes;
}

public void setDisallowedContentTypes(String[] disallowedContentTypes) {
this.disallowedContentTypes = disallowedContentTypes;
}

public CorsConfig getCors() {
return cors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@
package org.apache.dubbo.remoting.http12.message.codec;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.Configuration;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.http12.exception.UnsupportedMediaTypeException;
import org.apache.dubbo.remoting.http12.message.HttpMessageDecoder;
import org.apache.dubbo.remoting.http12.message.HttpMessageDecoderFactory;
import org.apache.dubbo.remoting.http12.message.HttpMessageEncoder;
import org.apache.dubbo.remoting.http12.message.HttpMessageEncoderFactory;
import org.apache.dubbo.rpc.Constants;
import org.apache.dubbo.rpc.model.FrameworkModel;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public final class CodecUtils {
Expand All @@ -37,13 +44,20 @@ public final class CodecUtils {
private final List<HttpMessageEncoderFactory> encoderFactories;
private final Map<String, Optional<HttpMessageEncoderFactory>> encoderCache = new ConcurrentHashMap<>();
private final Map<String, Optional<HttpMessageDecoderFactory>> decoderCache = new ConcurrentHashMap<>();
private Set<String> disallowedContentTypes = Collections.emptySet();

public CodecUtils(FrameworkModel frameworkModel) {
this.frameworkModel = frameworkModel;
decoderFactories = frameworkModel.getActivateExtensions(HttpMessageDecoderFactory.class);
encoderFactories = frameworkModel.getActivateExtensions(HttpMessageEncoderFactory.class);
decoderFactories.forEach(f -> decoderCache.putIfAbsent(f.mediaType().getName(), Optional.of(f)));
encoderFactories.forEach(f -> encoderCache.putIfAbsent(f.mediaType().getName(), Optional.of(f)));

Configuration configuration = ConfigurationUtils.getGlobalConfiguration(frameworkModel.defaultApplication());
String allowContentTypes = configuration.getString(Constants.H2_SETTINGS_DISALLOWED_CONTENT_TYPES, null);
if (allowContentTypes != null) {
disallowedContentTypes = new HashSet<>(StringUtils.tokenizeToList(allowContentTypes));
}
}

public HttpMessageDecoder determineHttpMessageDecoder(URL url, String mediaType) {
Expand All @@ -69,9 +83,10 @@ public HttpMessageEncoder determineHttpMessageEncoder(String mediaType) {
public Optional<HttpMessageDecoderFactory> determineHttpMessageDecoderFactory(String mediaType) {
Assert.notNull(mediaType, "mediaType must not be null");
return decoderCache.computeIfAbsent(mediaType, k -> {
for (HttpMessageDecoderFactory decoderFactory : decoderFactories) {
if (decoderFactory.supports(k)) {
return Optional.of(decoderFactory);
for (HttpMessageDecoderFactory factory : decoderFactories) {
if (factory.supports(k)
&& !disallowedContentTypes.contains(factory.mediaType().getName())) {
return Optional.of(factory);
}
}
return Optional.empty();
Expand All @@ -81,9 +96,10 @@ public Optional<HttpMessageDecoderFactory> determineHttpMessageDecoderFactory(St
public Optional<HttpMessageEncoderFactory> determineHttpMessageEncoderFactory(String mediaType) {
Assert.notNull(mediaType, "mediaType must not be null");
return encoderCache.computeIfAbsent(mediaType, k -> {
for (HttpMessageEncoderFactory encoderFactory : encoderFactories) {
if (encoderFactory.supports(k)) {
return Optional.of(encoderFactory);
for (HttpMessageEncoderFactory factory : encoderFactories) {
if (factory.supports(k)
&& !disallowedContentTypes.contains(factory.mediaType().getName())) {
return Optional.of(factory);
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public interface Constants {
String H2_SETTINGS_BUILTIN_SERVICE_INIT = "dubbo.tri.builtin.service.init";

String H2_SETTINGS_JSON_FRAMEWORK_NAME = "dubbo.protocol.triple.rest.json-framework";
String H2_SETTINGS_DISALLOWED_CONTENT_TYPES = "dubbo.protocol.triple.rest.disallowed-content-types";

String H2_SETTINGS_VERBOSE_ENABLED = "dubbo.protocol.triple.verbose";
String H2_SETTINGS_SERVLET_ENABLED = "dubbo.protocol.triple.servlet.enabled";
Expand Down

0 comments on commit d7c11c7

Please sign in to comment.