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.util.ObjectUtils.isEmpty;
20
21 import java.util.List;
22 import org.bremersee.apiclient.webflux.Invocation;
23 import org.bremersee.apiclient.webflux.InvocationParameter;
24 import org.springframework.data.domain.Pageable;
25 import org.springframework.util.LinkedMultiValueMap;
26 import org.springframework.util.MultiValueMap;
27
28
29
30
31
32
33 public class PageableRequestParameterResolver extends SortRequestParameterResolver {
34
35 private String pageNumberRequestParamName = "page";
36
37 private String pageSizeRequestParamName = "size";
38
39
40
41
42
43
44
45 public PageableRequestParameterResolver withPageNumberRequestParamName(
46 String pageNumberRequestParamName) {
47
48 if (!isEmpty(pageNumberRequestParamName)) {
49 this.pageNumberRequestParamName = pageNumberRequestParamName;
50 }
51 return this;
52 }
53
54
55
56
57
58
59
60 public PageableRequestParameterResolver withPageSizeRequestParamName(
61 String pageSizeRequestParamName) {
62
63 if (!isEmpty(pageSizeRequestParamName)) {
64 this.pageSizeRequestParamName = pageSizeRequestParamName;
65 }
66 return this;
67 }
68
69 @Override
70 public MultiValueMap<String, Object> apply(Invocation invocation) {
71 return invocation.toMethodParameterStream()
72 .filter(this)
73 .map(InvocationParameter::getValue)
74 .map(value -> getRequestParameters((Pageable) value))
75 .collect(
76 LinkedMultiValueMap::new,
77 LinkedMultiValueMap::putAll,
78 LinkedMultiValueMap::putAll);
79 }
80
81 @Override
82 public boolean test(InvocationParameter invocationParameter) {
83 return invocationParameter.getValue() instanceof Pageable && invocationParameter
84 .hasNoneParameterAnnotation(Extensions.ILLEGAL_EXTENSIONS_ANNOTATIONS);
85 }
86
87
88
89
90
91
92
93 protected MultiValueMap<String, Object> getRequestParameters(Pageable pageable) {
94 MultiValueMap<String, Object> map = super.getRequestParameters(pageable.getSort());
95 if (pageable.isPaged()) {
96 map.put(pageNumberRequestParamName, List.of(pageable.getPageNumber()));
97 map.put(pageSizeRequestParamName, List.of(pageable.getPageSize()));
98 }
99 return map;
100 }
101 }