1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.apiclient.webflux.contract.spring;
18
19 import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
20
21 import java.lang.reflect.Method;
22 import java.util.Optional;
23 import java.util.function.Function;
24 import org.bremersee.apiclient.webflux.Invocation;
25 import org.bremersee.apiclient.webflux.contract.HttpRequestMethod;
26 import org.springframework.util.Assert;
27 import org.springframework.web.bind.annotation.DeleteMapping;
28 import org.springframework.web.bind.annotation.GetMapping;
29 import org.springframework.web.bind.annotation.PatchMapping;
30 import org.springframework.web.bind.annotation.PostMapping;
31 import org.springframework.web.bind.annotation.PutMapping;
32 import org.springframework.web.bind.annotation.RequestMapping;
33
34
35
36
37
38
39 public class HttpMethodResolver implements Function<Invocation, HttpRequestMethod> {
40
41 @Override
42 public HttpRequestMethod apply(Invocation invocation) {
43
44 Assert.notNull(invocation, "Invocation must be present.");
45 Method method = invocation.getMethod();
46 return Optional.ofNullable(findAnnotation(method, RequestMapping.class))
47 .filter(requestMapping -> requestMapping.method().length > 0)
48 .flatMap(requestMapping -> HttpRequestMethod.resolve(requestMapping.method()[0].name()))
49 .or(() -> Optional.ofNullable(findAnnotation(method, GetMapping.class))
50 .map(a -> HttpRequestMethod.GET))
51 .or(() -> Optional.ofNullable(findAnnotation(method, PostMapping.class))
52 .map(a -> HttpRequestMethod.POST))
53 .or(() -> Optional.ofNullable(findAnnotation(method, PutMapping.class))
54 .map(a -> HttpRequestMethod.PUT))
55 .or(() -> Optional.ofNullable(findAnnotation(method, PatchMapping.class))
56 .map(a -> HttpRequestMethod.PATCH))
57 .or(() -> Optional.ofNullable(findAnnotation(method, DeleteMapping.class))
58 .map(a -> HttpRequestMethod.DELETE))
59 .orElseThrow(() -> new IllegalStateException(
60 String.format("Cannot find request method on method '%s'.", method.getName())));
61 }
62 }