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 java.util.Arrays;
20 import java.util.function.Function;
21 import org.bremersee.apiclient.webflux.Invocation;
22 import org.springframework.http.MediaType;
23 import org.springframework.web.bind.annotation.DeleteMapping;
24 import org.springframework.web.bind.annotation.GetMapping;
25 import org.springframework.web.bind.annotation.PatchMapping;
26 import org.springframework.web.bind.annotation.PostMapping;
27 import org.springframework.web.bind.annotation.PutMapping;
28 import org.springframework.web.bind.annotation.RequestMapping;
29
30
31
32
33
34
35 public class AcceptResolver implements Function<Invocation, MediaType> {
36
37 @Override
38 public MediaType apply(Invocation invocation) {
39 return Arrays.stream(
40 invocation.findAnnotationValueOnMethod(
41 RequestMapping.class, a -> a.produces().length > 0, RequestMapping::produces)
42 .or(() -> invocation.findAnnotationValueOnMethod(
43 GetMapping.class, a -> a.produces().length > 0, GetMapping::produces))
44 .or(() -> invocation.findAnnotationValueOnMethod(
45 PostMapping.class, a -> a.produces().length > 0, PostMapping::produces))
46 .or(() -> invocation.findAnnotationValueOnMethod(
47 PutMapping.class, a -> a.produces().length > 0, PutMapping::produces))
48 .or(() -> invocation.findAnnotationValueOnMethod(
49 PatchMapping.class, a -> a.produces().length > 0, PatchMapping::produces))
50 .or(() -> invocation.findAnnotationValueOnMethod(
51 DeleteMapping.class, a -> a.produces().length > 0, DeleteMapping::produces))
52 .orElse(new String[0]))
53 .map(this::parseMediaType)
54 .findFirst()
55 .orElse(MediaType.ALL);
56 }
57
58
59
60
61
62
63
64 protected MediaType parseMediaType(String mediaType) {
65 try {
66 return MediaType.parseMediaType(mediaType);
67 } catch (RuntimeException ignored) {
68 return null;
69 }
70 }
71 }