forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HateoasPageableHandlerMethodArgumentResolver.java
124 lines (103 loc) · 4.68 KB
/
HateoasPageableHandlerMethodArgumentResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.web;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.mvc.UriComponentsContributor;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Extension of {@link PageableHandlerMethodArgumentResolver} that also supports enhancing URIs using Spring HATEOAS
* support.
*
* @since 1.6
* @author Oliver Gierke
* @author Nick Williams
*/
@SuppressWarnings("deprecation")
public class HateoasPageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolver implements
UriComponentsContributor {
/**
* A {@link HateoasPageableHandlerMethodArgumentResolver} preconfigured to the setup of
* {@link PageableArgumentResolver}. Use that if you need to stick to the former request parameters an 1-indexed
* behavior. This will be removed in the next major version (1.7). So consider migrating to the new way of exposing
* request parameters.
*/
@Deprecated public static final HateoasPageableHandlerMethodArgumentResolver LEGACY;
private static final HateoasSortHandlerMethodArgumentResolver DEFAULT_SORT_RESOLVER = new HateoasSortHandlerMethodArgumentResolver();
static {
HateoasSortHandlerMethodArgumentResolver LEGACY_SORT = new HateoasSortHandlerMethodArgumentResolver();
LEGACY_SORT.setLegacyMode(true);
LEGACY_SORT.setSortParameter("page.sort");
LEGACY = new HateoasPageableHandlerMethodArgumentResolver(LEGACY_SORT);
LEGACY.setPageParameterName("page.page");
LEGACY.setSizeParameterName("page.size");
LEGACY.setFallbackPageable(new PageRequest(1, 10));
LEGACY.setOneIndexedParameters(true);
}
private final HateoasSortHandlerMethodArgumentResolver sortResolver;
/**
* Constructs an instance of this resolver with a default {@link HateoasSortHandlerMethodArgumentResolver}.
*/
public HateoasPageableHandlerMethodArgumentResolver() {
this(null);
}
/**
* Creates a new {@link HateoasPageableHandlerMethodArgumentResolver} using the given
* {@link HateoasSortHandlerMethodArgumentResolver}..
*
* @param sortResolver
*/
public HateoasPageableHandlerMethodArgumentResolver(HateoasSortHandlerMethodArgumentResolver sortResolver) {
super(getDefaultedSortResolver(sortResolver));
this.sortResolver = getDefaultedSortResolver(sortResolver);
}
/**
* Returns the template variable for the pagination parameters.
*
* @param parameter can be {@literal null}.
* @return
* @since 1.7
*/
public String getPaginationTemplateVariables(MethodParameter parameter) {
String pagePropertyName = getParameterNameToUse(getPageParameterName(), parameter);
String sizePropertyName = getParameterNameToUse(getSizeParameterName(), parameter);
String sortTemplateVariables = sortResolver.getSortTemplateVariables(parameter);
return String.format("{?%s,%s}{&%s", pagePropertyName, sizePropertyName, sortTemplateVariables.substring(2));
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.mvc.UriComponentsContributor#enhance(org.springframework.web.util.UriComponentsBuilder, org.springframework.core.MethodParameter, java.lang.Object)
*/
@Override
public void enhance(UriComponentsBuilder builder, MethodParameter parameter, Object value) {
if (!(value instanceof Pageable)) {
return;
}
Pageable pageable = (Pageable) value;
String pagePropertyName = getParameterNameToUse(getPageParameterName(), parameter);
String sizePropertyName = getParameterNameToUse(getSizeParameterName(), parameter);
int pageNumber = pageable.getPageNumber();
builder.replaceQueryParam(pagePropertyName, isOneIndexedParameters() ? pageNumber + 1 : pageNumber);
builder.replaceQueryParam(sizePropertyName, pageable.getPageSize() <= getMaxPageSize() ? pageable.getPageSize()
: getMaxPageSize());
this.sortResolver.enhance(builder, parameter, pageable.getSort());
}
private static HateoasSortHandlerMethodArgumentResolver getDefaultedSortResolver(
HateoasSortHandlerMethodArgumentResolver sortResolver) {
return sortResolver == null ? DEFAULT_SORT_RESOLVER : sortResolver;
}
}