11package org .dddjava .jig .domain .model .information .inputs ;
22
33import org .dddjava .jig .domain .model .data .packages .PackageId ;
4- import org .dddjava .jig .domain .model .data .types .JigAnnotationReference ;
54import org .dddjava .jig .domain .model .data .types .TypeId ;
65import org .dddjava .jig .domain .model .information .members .CallerMethods ;
76import org .dddjava .jig .domain .model .information .members .JigMethod ;
87import org .dddjava .jig .domain .model .information .types .JigType ;
9- import org .slf4j .Logger ;
10- import org .slf4j .LoggerFactory ;
11-
12- import java .util .Arrays ;
13- import java .util .Locale ;
14- import java .util .function .Predicate ;
15-
16- import static java .util .stream .Collectors .joining ;
178
189/**
1910 * 入力アダプタのエントリーポイント
2415 * 制限事項:RequestMappingをメタアノテーションとした独自アノテーションが付与されたメソッドは、ハンドラとして扱われません。
2516 */
2617public record Entrypoint (EntrypointType entrypointType , JigType jigType , JigMethod jigMethod ,
27- JigAnnotationReference httpMappingAnnotation ) {
18+ EntrypointMapping entrypointMapping ) {
2819
2920 public boolean anyMatch (CallerMethods callerMethods ) {
3021 return callerMethods .contains (jigMethod .jigMethodId ());
@@ -46,90 +37,29 @@ public PackageId packageId() {
4637 * それ以外は種別の文字列表現。
4738 */
4839 public String pathText () {
49- return switch (entrypointType ()) {
50- case HTTP_API -> {
51- var httpEndpoint = HttpEntrypointPath .from (this );
52- // これだとクラスのパスがはいってない
53- yield "%s %s" .formatted (httpEndpoint .method (), httpEndpoint .methodPath ());
54- }
55- case QUEUE_LISTENER -> "queue: %s" .formatted (MessageListener .from (this ).queueName ());
56- default -> this .entrypointType ().toString ();
57- };
40+ return entrypointMapping .shortPathText ();
5841 }
5942
6043 public String methodLabelText () {
6144 if (entrypointType () == EntrypointType .HTTP_API ) {
62- return HttpEntrypointPath . from ( this ). entrypointName ();
45+ return resolveEntrypointName ();
6346 }
6447 return jigMethod ().labelText ();
6548 }
6649
6750 public String fullPathText () {
68- return HttpEntrypointPath . from ( this ). pathText ();
51+ return entrypointMapping . fullPathText ();
6952 }
7053
71- /**
72- * HTTPリクエストのエンドポイント
73- *
74- * エントリーポイントから情報を抜き出したもの。
75- */
76- private record HttpEntrypointPath (String method , String entrypointName , String classPath , String methodPath ) {
77- private static final Logger logger = LoggerFactory .getLogger (HttpEntrypointPath .class );
78-
79- public static HttpEntrypointPath from (Entrypoint entrypoint ) {
80- var jigMethod = entrypoint .jigMethod ();
81-
82- // NOTE: valueとpathの両方が指定されている場合は起動失敗(AnnotationConfigurationException)になるので、単純に合わせる
83- // org.springframework.core.annotation.AbstractAliasAwareAnnotationAttributeExtractor.getAttributeValue
84- // 複数( @RequestMapping({"a", "b"}) など)への対応は、そのうち。
85- String classPath = entrypoint .jigType ()
86- .annotationValueOf (TypeId .valueOf ("org.springframework.web.bind.annotation.RequestMapping" ), "value" , "path" )
87- .filter (value -> !"/" .equals (value )).orElse ("" );
88-
89- var entrypointName = resolveEntrypointName (jigMethod );
90- var requestMappingForMethod = entrypoint .httpMappingAnnotation ();
91-
92- if (requestMappingForMethod != null ) {
93- var methodPath = resolveMethodPath (requestMappingForMethod );
94- var httpMethod = resolveHttpMethod (requestMappingForMethod );
95- return new HttpEntrypointPath (httpMethod , entrypointName , classPath , methodPath );
96- }
97- // RequestMappingでないものをEntrypointと認識しているのはおかしい。
98- // Entrypointの時点で解決しているはずなので、ここで分岐があるのが設計ミス。
99- logger .warn ("{} のRequestMapping系アノテーションが検出されませんでした。JIGの不具合もしくは設定ミスです。" , jigMethod .simpleText ());
100- return new HttpEntrypointPath ("???" , entrypointName , classPath , "" );
101- }
102-
103- private static String resolveHttpMethod (JigAnnotationReference requestMappingForMethod ) {
104- var simpleText = requestMappingForMethod .id ().asSimpleText ();
105- // アノテーション名からHTTPメソッド名を解決する。
106- // RequestMappingはmethod要素の指定次第となるが、解決するのも手間だし、RequestMappingなどよりGetMappingの使用が推奨なので扱わない。
107- return "RequestMapping" .equals (simpleText ) ? "???" : simpleText .replace ("Mapping" , "" ).toUpperCase (Locale .ROOT );
108- }
109-
110- private static String resolveEntrypointName (JigMethod jigMethod ) {
111- return jigMethod
112- // Swaggerのアノテーションのsummaryが記述されていればそれを採用
113- .declarationAnnotationStream ()
114- .filter (methodAnnotation -> methodAnnotation .id ().equals (TypeId .valueOf ("io.swagger.v3.oas.annotations.Operation" )))
115- .flatMap (methodAnnotation -> methodAnnotation .elementTextOf ("summary" ).stream ())
116- // アノテーションの仕様上、同じアノテーションが複数あることもあるし、要素の文字列も配列で定義可能なのでAnyで取得。実際は0..1になる。
117- .findAny ()
118- // OpenAPIドキュメントの自動生成をしていないなど、解決できない場合は通常のメソッドラベル
119- .orElseGet (jigMethod ::labelText );
120- }
121-
122- private static String resolveMethodPath (JigAnnotationReference requestMappingForMethod ) {
123- return requestMappingForMethod .elementTextOf ("value" )
124- .or (() -> requestMappingForMethod .elementTextOf ("path" ))
125- .filter (Predicate .not (String ::isEmpty ))
126- .orElse ("/" );
127- }
128-
129- public String pathText () {
130- return Arrays .stream ((classPath + '/' + methodPath ).split ("/" , -1 ))
131- .filter (string -> !string .isEmpty ())
132- .collect (joining ("/" , "/" , "" ));
133- }
54+ private String resolveEntrypointName () {
55+ return jigMethod
56+ // Swaggerのアノテーションのsummaryが記述されていればそれを採用
57+ .declarationAnnotationStream ()
58+ .filter (methodAnnotation -> methodAnnotation .id ().equals (TypeId .valueOf ("io.swagger.v3.oas.annotations.Operation" )))
59+ .flatMap (methodAnnotation -> methodAnnotation .elementTextOf ("summary" ).stream ())
60+ // アノテーションの仕様上、同じアノテーションが複数あることもあるし、要素の文字列も配列で定義可能なのでAnyで取得。実際は0..1になる。
61+ .findAny ()
62+ // OpenAPIドキュメントの自動生成をしていないなど、解決できない場合は通常のメソッドラベル
63+ .orElseGet (jigMethod ::labelText );
13464 }
13565}
0 commit comments