Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
ExchangeType
for the response of annotated service (#4177)
Motivation: `ExchangeType` has been introduced with #3956. The changes in #3956 was huge, so I didn't implement `ExchangeType` for the response type of an annotated service. Modifications: - Add `ResponseConverterFunction.isResponseStreaming()` to configure whether to enable response streaming. - The value is enabled by default because it abides by the original behavior. - The API uses `RequestHeaders` and a negotiated media type to determine response streaming. - Override `.isResponseStreaming()` for all known implementions. - Fix a bug where `ExchangeType` does not work for a `GrpcService` prefixed a path. - Breaking) `HttpService.exchangeType()` now takes `RouteingContext` as a single parameter. - A negotiated media type was required. - Add `RequestContext.exchangeType()` to get the `ExchangeType` of the current request. Result: - Annotated services perform better on unary responses.
- Loading branch information
Showing
64 changed files
with
1,809 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
core/src/main/java/com/linecorp/armeria/internal/server/annotation/ClassUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.armeria.internal.server.annotation; | ||
|
||
import static com.linecorp.armeria.internal.common.util.ObjectCollectingUtil.MONO_CLASS; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
public final class ClassUtil { | ||
|
||
/** | ||
* Converts the specified {@link Type} to a {@link Class} instance. | ||
*/ | ||
@Nullable | ||
public static Class<?> typeToClass(Type type) { | ||
if (type instanceof Class) { | ||
return (Class<?>) type; | ||
} | ||
if (type instanceof ParameterizedType) { | ||
return (Class<?>) ((ParameterizedType) type).getRawType(); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Unwraps an enclosing {@link CompletionStage}, {@code reactor.core.publisher.Mono}, or | ||
* {@code scala.concurrent.Future} and returns the type arguments. | ||
* Returns itself if the {@link Type} is not enclosed by one of them. | ||
*/ | ||
public static Type unwrapAsyncType(Type type) { | ||
if (type instanceof Class) { | ||
return type; | ||
} | ||
|
||
if (!(type instanceof ParameterizedType)) { | ||
return type; | ||
} | ||
|
||
final ParameterizedType ptype = (ParameterizedType) type; | ||
final Type[] typeArguments = ptype.getActualTypeArguments(); | ||
if (typeArguments.length == 0) { | ||
return type; | ||
} | ||
|
||
final Class<?> clazz = (Class<?>) ptype.getRawType(); | ||
final Type typeArgument = typeArguments[0]; | ||
if (CompletionStage.class.isAssignableFrom(clazz) || | ||
ScalaUtil.isScalaFuture(clazz) || | ||
(MONO_CLASS != null && MONO_CLASS.isAssignableFrom(clazz))) { | ||
return typeArgument; | ||
} | ||
return type; | ||
} | ||
|
||
private ClassUtil() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.