forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support an alias for a bunch of annotations (line#1560)
Motivation: A user may want to define his or her annotations as a custom annotation in order to apply the custom annotation to his or her annotated HTTP services. e.g: ``` // Define a custom annotation: @ProducesJson @LoggingDecorator @interface MyApiSpecification {} // Apply it to the annotated HTTP service: @get("/api") @MyApiSpecification public Something getSomething() {} ``` Modifications: - Add `AnnotationUtil` for finding a specific annotation or getting all annotations from an `AnnotatedElement`. - Use `AnnotationUtil` instead of `ReflectionUtils.getAllAnnotations()`. The old one does not respect the order of the annotations because its return type is a `Set<Annotation>` (more specifically it's `HashSet`). Result: - Won't depend on the `ReflectionUtils`'s implementation when getting annotations.
- Loading branch information
Showing
9 changed files
with
1,199 additions
and
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
dependencies { | ||
// cglib | ||
testCompile 'cglib:cglib' | ||
|
||
// Caffeine | ||
compile 'com.github.ben-manes.caffeine:caffeine' | ||
|
||
|
283 changes: 91 additions & 192 deletions
283
core/src/main/java/com/linecorp/armeria/internal/annotation/AnnotatedHttpServiceFactory.java
Large diffs are not rendered by default.
Oops, something went wrong.
369 changes: 369 additions & 0 deletions
369
core/src/main/java/com/linecorp/armeria/internal/annotation/AnnotationUtil.java
Large diffs are not rendered by default.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
...ava/com/linecorp/armeria/internal/annotation/AnnotatedHttpServiceAnnotationAliasTest.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,83 @@ | ||
/* | ||
* Copyright 2019 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.annotation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import com.linecorp.armeria.client.HttpClient; | ||
import com.linecorp.armeria.common.AggregatedHttpMessage; | ||
import com.linecorp.armeria.common.HttpData; | ||
import com.linecorp.armeria.common.HttpHeaders; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.common.MediaType; | ||
import com.linecorp.armeria.server.ServerBuilder; | ||
import com.linecorp.armeria.server.ServiceRequestContext; | ||
import com.linecorp.armeria.server.annotation.Get; | ||
import com.linecorp.armeria.server.annotation.Param; | ||
import com.linecorp.armeria.server.annotation.Produces; | ||
import com.linecorp.armeria.server.annotation.ResponseConverter; | ||
import com.linecorp.armeria.server.annotation.ResponseConverterFunction; | ||
import com.linecorp.armeria.testing.server.ServerRule; | ||
|
||
public class AnnotatedHttpServiceAnnotationAliasTest { | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@ResponseConverter(MyResponseConverter.class) | ||
@Produces("text/plain") | ||
@interface MyResponse {} | ||
|
||
static class MyResponseConverter implements ResponseConverterFunction { | ||
@Override | ||
public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers, | ||
@Nullable Object result, HttpHeaders trailingHeaders) | ||
throws Exception { | ||
return HttpResponse.of( | ||
headers, HttpData.ofUtf8("Hello, %s!", result), trailingHeaders); | ||
} | ||
} | ||
|
||
@ClassRule | ||
public static ServerRule rule = new ServerRule() { | ||
@Override | ||
protected void configure(ServerBuilder sb) throws Exception { | ||
sb.annotatedService(new Object() { | ||
@Get("/hello/{name}") | ||
@MyResponse | ||
public String name(@Param String name) { | ||
return name; | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
@Test | ||
public void serviceConfiguredWithAlias() { | ||
final AggregatedHttpMessage msg = HttpClient.of(rule.uri("/")).get("/hello/Armeria") | ||
.aggregate().join(); | ||
assertThat(msg.status()).isEqualTo(HttpStatus.OK); | ||
assertThat(msg.headers().contentType()).isEqualTo(MediaType.parse("text/plain")); | ||
assertThat(msg.content().toStringUtf8()).isEqualTo("Hello, Armeria!"); | ||
} | ||
} |
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.