Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean committed Dec 26, 2023
1 parent 959371f commit dd05ec4
Show file tree
Hide file tree
Showing 17 changed files with 1,080 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public R getRight() {
return right;
}

public boolean isNull() {
return this == NULL || left == null && right == null;
}

@Override
public L getKey() {
return left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public interface HttpRequest extends RequestMetadata {

String charset();

List<String> accept();
String accept();

Locale locale();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,6 @@ public static List<Locale> parseAcceptLanguage(String header) {
return new ArrayList<>(locales.values());
}

public static Pair<String, String> parseMimeType(String mimeType) {
if (StringUtils.isEmpty(mimeType)) {
throw new HttpStatusException(415, "mimeType must not be empty");
}

int index = mimeType.indexOf(';');
String fullType = (index == -1 ? mimeType : mimeType.substring(0, index)).trim();
if (WILDCARD_TYPE.equals(fullType)) {
fullType = "*/*";
}
int subIndex = fullType.indexOf('/');
if (subIndex == -1) {
throw new HttpStatusException(415, "mimeType '" + fullType + "' does not contain '/'");
}
if (subIndex == fullType.length() - 1) {
throw new HttpStatusException(415, "mimeType '" + fullType + "' does not contain subtype after '/'");
}
String type = fullType.substring(0, subIndex);
String subType = fullType.substring(subIndex + 1);
if (WILDCARD_TYPE.equals(type) && !WILDCARD_TYPE.equals(subType)) {
throw new HttpStatusException(415, "mimeType '" + fullType + "' is invalid");
}
return new Pair<>(type, subType);
}

public static HttpPostRequestDecoder createPostRequestDecoder(
HttpRequest request, InputStream inputStream, String charset) {
ByteBuf data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,8 @@ private String getCharset0() {
}

@Override
public List<String> accept() {
if (accept == null) {
accept = HttpUtils.parseAccept(headers.getFirst("accept-language"));
}
return accept;
public String accept() {
return headers.getFirst(HttpHeaderNames.ACCEPT.getName());
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
*/
package org.apache.dubbo.rpc.protocol.tri.rest.mapping.condition;

import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.http12.HttpHeaderNames;
import org.apache.dubbo.remoting.http12.HttpRequest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ConsumesCondition implements Condition<ConsumesCondition, HttpRequest> {

public static final MediaTypeExpression DEFAULT = new MediaTypeExpression("application/octet-stream");
public static final MediaTypeExpression DEFAULT = MediaTypeExpression.parse("application/octet-stream");

private final List<MediaTypeExpression> expressions;

Expand All @@ -37,32 +36,44 @@ public ConsumesCondition(String... consumes) {
}

public ConsumesCondition(String[] consumes, String[] headers) {
List<MediaTypeExpression> expressions = new ArrayList<>();
Set<MediaTypeExpression> expressions = null;
if (headers != null) {
for (String header : headers) {
Expression<String> expr = new NameValueExpression(header);
if ("content-type".equalsIgnoreCase(expr.getName())) {
String mimeTypes = expr.getValue();
if (StringUtils.isEmpty(mimeTypes)) {
NameValueExpression expr = NameValueExpression.parse(header);
if (HttpHeaderNames.CONTENT_TYPE.getName().equalsIgnoreCase(expr.getName())) {
MediaTypeExpression expression = MediaTypeExpression.parse(expr.getValue());
if (expression == null) {
continue;
}
for (String mimeType : StringUtils.tokenize(mimeTypes, ',')) {
expressions.add(new MediaTypeExpression(mimeType));
if (expressions == null) {
expressions = new LinkedHashSet<>();
}
expressions.add(expression);
}
}
}
if (consumes != null) {
for (String consume : consumes) {
expressions.add(new MediaTypeExpression(consume));
MediaTypeExpression expression = MediaTypeExpression.parse(consume);
if (expression == null) {
continue;
}
if (expressions == null) {
expressions = new LinkedHashSet<>();
}
expressions.add(expression);
}
}
this.expressions = expressions;
if (expressions == null) {
this.expressions = Collections.emptyList();
} else {
this.expressions = new ArrayList<>(expressions);
Collections.sort(this.expressions);
}
}

private ConsumesCondition(Collection<MediaTypeExpression> expressions) {
this.expressions = new ArrayList<>(expressions);
Collections.sort(this.expressions);
private ConsumesCondition(List<MediaTypeExpression> expressions) {
this.expressions = expressions;
}

@Override
Expand All @@ -77,14 +88,18 @@ public ConsumesCondition match(HttpRequest request) {
}

String contentType = request.contentType();
MediaTypeExpression mediaType = contentType == null ? DEFAULT : new MediaTypeExpression(contentType);
Set<MediaTypeExpression> result = new LinkedHashSet<>();
for (MediaTypeExpression expression : expressions) {
MediaTypeExpression mediaType = contentType == null ? DEFAULT : MediaTypeExpression.parse(contentType);
List<MediaTypeExpression> result = null;
for (int i = 0, len = expressions.size(); i < len; i++) {
MediaTypeExpression expression = expressions.get(i);
if (expression.match(mediaType)) {
if (result == null) {
result = new ArrayList<>();
}
result.add(expression);
}
}
return result.isEmpty() ? null : new ConsumesCondition(result);
return result == null ? null : new ConsumesCondition(result);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,54 @@
*/
package org.apache.dubbo.rpc.protocol.tri.rest.mapping.condition;

import org.apache.dubbo.remoting.http12.HttpHeaderNames;
import org.apache.dubbo.remoting.http12.HttpRequest;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
import static org.apache.dubbo.remoting.http12.HttpHeaderNames.ACCEPT;
import static org.apache.dubbo.remoting.http12.HttpHeaderNames.CONTENT_TYPE;

public final class HeadersCondition implements Condition<HeadersCondition, HttpRequest> {

private final Set<Expression<String>> expressions;
private final Set<NameValueExpression> expressions;

public HeadersCondition(String... headers) {
Set<Expression<String>> expressions = new LinkedHashSet<>();
Set<NameValueExpression> expressions = null;
if (headers != null) {
for (String header : headers) {
Expression<String> expr = new NameValueExpression(header);
if ("accept".equalsIgnoreCase(expr.getName()) || "content-type".equalsIgnoreCase(expr.getName())) {
NameValueExpression expr = NameValueExpression.parse(header);
String name = expr.getName();
if (ACCEPT.getName().equalsIgnoreCase(name) || CONTENT_TYPE.getName().equalsIgnoreCase(name)) {
continue;
}
if (expressions == null) {
expressions = new LinkedHashSet<>();
}
expressions.add(expr);
}
}
this.expressions = expressions;
this.expressions = expressions == null ? Collections.emptySet() : expressions;
}

private HeadersCondition(Collection<Expression<String>> conditions) {
expressions = new LinkedHashSet<>(conditions);
private HeadersCondition(Collection<NameValueExpression> expressions) {
this.expressions = isEmpty(expressions) ? Collections.emptySet() : new LinkedHashSet<>(expressions);
}

@Override
public HeadersCondition combine(HeadersCondition other) {
Set<Expression<String>> set = new LinkedHashSet<>(expressions);
Set<NameValueExpression> set = new LinkedHashSet<>(expressions);
set.addAll(other.expressions);
return new HeadersCondition(set);
}

@Override
public HeadersCondition match(HttpRequest request) {
for (Expression<String> expression : expressions) {
for (NameValueExpression expression : expressions) {
if (!expression.match(request::hasHeader, request::header)) {
return null;
}
Expand Down
Loading

0 comments on commit dd05ec4

Please sign in to comment.