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 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   * The pageable request parameter resolver.
30   *
31   * @author Christian Bremer
32   */
33  public class PageableRequestParameterResolver extends SortRequestParameterResolver {
34  
35    private String pageNumberRequestParamName = "page";
36  
37    private String pageSizeRequestParamName = "size";
38  
39    /**
40     * With page number request param name.
41     *
42     * @param pageNumberRequestParamName the page number request param name
43     * @return the pageable request parameter resolver
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     * With page size request param name.
56     *
57     * @param pageSizeRequestParamName the page size request param name
58     * @return the pageable request parameter resolver
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     * Gets request parameters.
89     *
90     * @param pageable the pageable
91     * @return the request parameters
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 }