View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.apiclient.webflux.contract.spring;
18  
19  import java.util.function.Function;
20  import org.bremersee.apiclient.webflux.Invocation;
21  import org.springframework.util.StringUtils;
22  import org.springframework.web.bind.annotation.DeleteMapping;
23  import org.springframework.web.bind.annotation.GetMapping;
24  import org.springframework.web.bind.annotation.PatchMapping;
25  import org.springframework.web.bind.annotation.PostMapping;
26  import org.springframework.web.bind.annotation.PutMapping;
27  import org.springframework.web.bind.annotation.RequestMapping;
28  
29  /**
30   * The request path resolver.
31   *
32   * @author Christian Bremer
33   */
34  public class RequestPathResolver implements Function<Invocation, String> {
35  
36    @Override
37    public String apply(Invocation invocation) {
38      // Request mapping on class
39      String clsPath = invocation
40          .findAnnotationValueOnTargetClass(
41              RequestMapping.class, a -> a.value().length > 0, a -> a.value()[0])
42          .orElse("");
43  
44      // Request mapping on method
45      String path = invocation
46          .findAnnotationValueOnMethod(
47              RequestMapping.class, a -> a.value().length > 0, a -> a.value()[0])
48          .orElse("");
49      if (StringUtils.hasText(path)) {
50        return clsPath + path;
51      }
52  
53      // Get mapping on method
54      path = invocation
55          .findAnnotationValueOnMethod(
56              GetMapping.class, a -> a.value().length > 0, a -> a.value()[0])
57          .orElse("");
58      if (StringUtils.hasText(path)) {
59        return clsPath + path;
60      }
61  
62      // Post mapping on method
63      path = invocation
64          .findAnnotationValueOnMethod(
65              PostMapping.class, a -> a.value().length > 0, a -> a.value()[0])
66          .orElse("");
67      if (StringUtils.hasText(path)) {
68        return clsPath + path;
69      }
70  
71      // Put mapping on method
72      path = invocation
73          .findAnnotationValueOnMethod(
74              PutMapping.class, a -> a.value().length > 0, a -> a.value()[0])
75          .orElse("");
76      if (StringUtils.hasText(path)) {
77        return clsPath + path;
78      }
79  
80      // Patch mapping on method
81      path = invocation
82          .findAnnotationValueOnMethod(
83              PatchMapping.class, a -> a.value().length > 0, a -> a.value()[0])
84          .orElse("");
85      if (StringUtils.hasText(path)) {
86        return clsPath + path;
87      }
88  
89      // Delete mapping on method
90      path = invocation
91          .findAnnotationValueOnMethod(
92              DeleteMapping.class, a -> a.value().length > 0, a -> a.value()[0])
93          .orElse("");
94      return clsPath + path;
95    }
96  }