View Javadoc
1   /*
2    * Copyright 2019-2026 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.comparator.spring.web;
18  
19  import static java.util.Objects.isNull;
20  
21  import java.nio.charset.StandardCharsets;
22  import java.util.Arrays;
23  import org.bremersee.comparator.model.SortOrder;
24  import org.springframework.core.MethodParameter;
25  import org.springframework.lang.NonNull;
26  import org.springframework.lang.Nullable;
27  import org.springframework.util.StringUtils;
28  import org.springframework.web.bind.support.WebDataBinderFactory;
29  import org.springframework.web.context.request.NativeWebRequest;
30  import org.springframework.web.method.support.HandlerMethodArgumentResolver;
31  import org.springframework.web.method.support.ModelAndViewContainer;
32  import org.springframework.web.util.UriUtils;
33  
34  /**
35   * The sort order handler method argument resolver.
36   *
37   * @author Christian Bremer
38   */
39  public class SortOrderHandlerMethodArgumentResolver
40      extends SortOrderHandlerMethodArgumentResolverSupport
41      implements HandlerMethodArgumentResolver {
42  
43    /**
44     * Instantiates a new sort order handler method argument resolver.
45     */
46    public SortOrderHandlerMethodArgumentResolver() {
47      super();
48    }
49  
50    @Override
51    public boolean supportsParameter(MethodParameter parameter) {
52      return SortOrder.class.equals(parameter.getParameterType());
53    }
54  
55    @Nullable
56    @Override
57    public SortOrder resolveArgument(
58        @NonNull MethodParameter parameter,
59        @Nullable ModelAndViewContainer mavContainer,
60        @NonNull NativeWebRequest webRequest,
61        @Nullable WebDataBinderFactory binderFactory) {
62  
63      String[] rawParameterValues = webRequest.getParameterValues(getParameterName(parameter));
64  
65      // No parameter
66      if (isNull(rawParameterValues) || rawParameterValues.length == 0) {
67        return getDefaultFromAnnotationOrFallback(parameter);
68      }
69  
70      // Single empty parameter, e.g "sort="
71      if (rawParameterValues.length == 1 && !StringUtils.hasText(rawParameterValues[0])) {
72        return getDefaultFromAnnotationOrFallback(parameter);
73      }
74  
75      var decoded = Arrays.stream(rawParameterValues)
76          .map(it -> UriUtils.decode(it, StandardCharsets.UTF_8))
77          .toList();
78  
79      return parseParameterValues(decoded);
80    }
81  }