|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package com.linecorp.armeria.server; |
|
|
|
import static com.google.common.base.Preconditions.checkArgument; |
|
import static com.linecorp.armeria.server.RoutingResult.LOWEST_SCORE; |
|
import static java.util.Objects.requireNonNull; |
|
|
|
import com.google.common.collect.ImmutableMap; |
|
|
|
import com.linecorp.armeria.common.MediaType; |
|
import com.linecorp.armeria.common.annotation.Nullable; |
|
import com.linecorp.armeria.internal.common.ArmeriaHttpUtil; |
|
|
|
|
|
|
|
|
|
public final class RoutingResultBuilder { |
|
|
|
private RoutingResultType type = RoutingResultType.MATCHED; |
|
|
|
@Nullable |
|
private String path; |
|
|
|
@Nullable |
|
private String query; |
|
|
|
@Nullable |
|
private ImmutableMap.Builder<String, String> pathParams; |
|
|
|
private int score = LOWEST_SCORE; |
|
|
|
@Nullable |
|
private MediaType negotiatedResponseMediaType; |
|
|
|
RoutingResultBuilder() {} |
|
|
|
RoutingResultBuilder(int expectedNumParams) { |
|
pathParams = ImmutableMap.builderWithExpectedSize(expectedNumParams); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder type(RoutingResultType type) { |
|
requireNonNull(type, "type"); |
|
checkArgument(type != RoutingResultType.NOT_MATCHED, |
|
"type: %s (expected: %s or %s)", |
|
type, RoutingResultType.MATCHED, RoutingResultType.CORS_PREFLIGHT); |
|
this.type = type; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder path(String path) { |
|
this.path = requireNonNull(path, "path"); |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder query(@Nullable String query) { |
|
this.query = query; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder decodedParam(String name, String value) { |
|
pathParams().put(requireNonNull(name, "name"), requireNonNull(value, "value")); |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder rawParam(String name, String value) { |
|
pathParams().put(requireNonNull(name, "name"), |
|
|
|
|
|
|
|
ArmeriaHttpUtil.decodePathParam(requireNonNull(value, "value"))); |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder score(int score) { |
|
this.score = score; |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResultBuilder negotiatedResponseMediaType(MediaType negotiatedResponseMediaType) { |
|
this.negotiatedResponseMediaType = requireNonNull(negotiatedResponseMediaType, |
|
"negotiatedResponseMediaType"); |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
public RoutingResult build() { |
|
if (path == null) { |
|
return RoutingResult.empty(); |
|
} |
|
|
|
return new RoutingResult(type, path, query, |
|
pathParams != null ? pathParams.build() : ImmutableMap.of(), |
|
score, negotiatedResponseMediaType); |
|
} |
|
|
|
private ImmutableMap.Builder<String, String> pathParams() { |
|
if (pathParams != null) { |
|
return pathParams; |
|
} |
|
return pathParams = ImmutableMap.builder(); |
|
} |
|
} |
|
|