1 /*
2  * Copyright 2013-2020 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  *      https://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 package org.springframework.data.web;
17
18 import java.util.Arrays;
19
20 import javax.annotation.Nullable;
21
22 import org.springframework.core.MethodParameter;
23 import org.springframework.data.domain.Sort;
24 import org.springframework.util.StringUtils;
25 import org.springframework.web.bind.support.WebDataBinderFactory;
26 import org.springframework.web.context.request.NativeWebRequest;
27 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
28 import org.springframework.web.method.support.ModelAndViewContainer;
29
30 /**
31  * {@link HandlerMethodArgumentResolver} to automatically create {@link Sort} instances from request parameters or
32  * {@link SortDefault} annotations.
33  *
34  * @since 1.6
35  * @author Oliver Gierke
36  * @author Thomas Darimont
37  * @author Nick Williams
38  * @author Mark Paluch
39  * @author Christoph Strobl
40  */

41 public class SortHandlerMethodArgumentResolver extends SortHandlerMethodArgumentResolverSupport
42         implements SortArgumentResolver {
43
44     /*
45      * (non-Javadoc)
46      * @see org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter(org.springframework.core.MethodParameter)
47      */

48     @Override
49     public boolean supportsParameter(MethodParameter parameter) {
50         return Sort.class.equals(parameter.getParameterType());
51     }
52
53     /*
54      * (non-Javadoc)
55      * @see org.springframework.web.method.support.HandlerMethodArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory)
56      */

57     @Override
58     public Sort resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
59             NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) {
60
61         String[] directionParameter = webRequest.getParameterValues(getSortParameter(parameter));
62
63         // No parameter
64         if (directionParameter == null) {
65             return getDefaultFromAnnotationOrFallback(parameter);
66         }
67
68         // Single empty parameter, e.g "sort="
69         if (directionParameter.length == 1 && !StringUtils.hasText(directionParameter[0])) {
70             return getDefaultFromAnnotationOrFallback(parameter);
71         }
72
73         return parseParameterIntoSort(Arrays.asList(directionParameter), getPropertyDelimiter());
74     }
75 }
76