DATACMNS-1211 - Add ReactivePageableHandlerMethodArgumentResolver.
Add ReactivePageableHandlerMethodArgumentResolver and extract shared code from imperative PageableHandlerMethodArgumentResolver into PageableHandlerMethodArgumentResolverSupport. Original pull request: #264.
This commit is contained in:
@@ -67,138 +67,6 @@ public class PageableHandlerMethodArgumentResolver extends PageableHandlerMethod
|
||||
this.sortResolver = sortResolver == null ? DEFAULT_SORT_RESOLVER : sortResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link Pageable} to be used as fallback in case no {@link PageableDefault} or
|
||||
* {@link PageableDefault} (the latter only supported in legacy mode) can be found at the method parameter to be
|
||||
* resolved.
|
||||
* <p>
|
||||
* If you set this to {@literal Optional#empty()}, be aware that you controller methods will get {@literal null}
|
||||
* handed into them in case no {@link Pageable} data can be found in the request. Note, that doing so will require you
|
||||
* supply bot the page <em>and</em> the size parameter with the requests as there will be no default for any of the
|
||||
* parameters available.
|
||||
*
|
||||
* @param fallbackPageable the {@link Pageable} to be used as general fallback.
|
||||
*/
|
||||
public void setFallbackPageable(Pageable fallbackPageable) {
|
||||
|
||||
Assert.notNull(fallbackPageable, "Fallback Pageable must not be null!");
|
||||
|
||||
this.fallbackPageable = fallbackPageable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given {@link Pageable} is the fallback one.
|
||||
*
|
||||
* @param pageable can be {@literal null}.
|
||||
* @since 1.9
|
||||
* @return
|
||||
*/
|
||||
public boolean isFallbackPageable(Pageable pageable) {
|
||||
return fallbackPageable == null ? false : fallbackPageable.equals(pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the maximum page size to be accepted. This allows to put an upper boundary of the page size to prevent
|
||||
* potential attacks trying to issue an {@link OutOfMemoryError}. Defaults to {@link #DEFAULT_MAX_PAGE_SIZE}.
|
||||
*
|
||||
* @param maxPageSize the maxPageSize to set
|
||||
*/
|
||||
public void setMaxPageSize(int maxPageSize) {
|
||||
this.maxPageSize = maxPageSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the maximum page size to be accepted. This allows to put an upper boundary of the page size to prevent
|
||||
* potential attacks trying to issue an {@link OutOfMemoryError}. Defaults to {@link #DEFAULT_MAX_PAGE_SIZE}.
|
||||
*
|
||||
* @return the maximum page size allowed.
|
||||
*/
|
||||
protected int getMaxPageSize() {
|
||||
return this.maxPageSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the parameter name to be used to find the page number in the request. Defaults to {@code page}.
|
||||
*
|
||||
* @param pageParameterName the parameter name to be used, must not be {@literal null} or empty.
|
||||
*/
|
||||
public void setPageParameterName(String pageParameterName) {
|
||||
|
||||
Assert.hasText(pageParameterName, "Page parameter name must not be null or empty!");
|
||||
this.pageParameterName = pageParameterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the parameter name to be used to find the page number in the request. Defaults to {@code page}.
|
||||
*
|
||||
* @return the parameter name to be used, never {@literal null} or empty.
|
||||
*/
|
||||
protected String getPageParameterName() {
|
||||
return this.pageParameterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the parameter name to be used to find the page size in the request. Defaults to {@code size}.
|
||||
*
|
||||
* @param sizeParameterName the parameter name to be used, must not be {@literal null} or empty.
|
||||
*/
|
||||
public void setSizeParameterName(String sizeParameterName) {
|
||||
|
||||
Assert.hasText(sizeParameterName, "Size parameter name must not be null or empty!");
|
||||
this.sizeParameterName = sizeParameterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the parameter name to be used to find the page size in the request. Defaults to {@code size}.
|
||||
*
|
||||
* @return the parameter name to be used, never {@literal null} or empty.
|
||||
*/
|
||||
protected String getSizeParameterName() {
|
||||
return this.sizeParameterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a general prefix to be prepended to the page number and page size parameters. Useful to namespace the
|
||||
* property names used in case they are clashing with ones used by your application. By default, no prefix is used.
|
||||
*
|
||||
* @param prefix the prefix to be used or {@literal null} to reset to the default.
|
||||
*/
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix == null ? DEFAULT_PREFIX : prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* The delimiter to be used between the qualifier and the actual page number and size properties. Defaults to
|
||||
* {@code _}. So a qualifier of {@code foo} will result in a page number parameter of {@code foo_page}.
|
||||
*
|
||||
* @param qualifierDelimiter the delimiter to be used or {@literal null} to reset to the default.
|
||||
*/
|
||||
public void setQualifierDelimiter(String qualifierDelimiter) {
|
||||
this.qualifierDelimiter = qualifierDelimiter == null ? DEFAULT_QUALIFIER_DELIMITER : qualifierDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures whether to expose and assume 1-based page number indexes in the request parameters. Defaults to
|
||||
* {@literal false}, meaning a page number of 0 in the request equals the first page. If this is set to
|
||||
* {@literal true}, a page number of 1 in the request will be considered the first page.
|
||||
*
|
||||
* @param oneIndexedParameters the oneIndexedParameters to set
|
||||
*/
|
||||
public void setOneIndexedParameters(boolean oneIndexedParameters) {
|
||||
this.oneIndexedParameters = oneIndexedParameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether to expose and assume 1-based page number indexes in the request parameters. Defaults to
|
||||
* {@literal false}, meaning a page number of 0 in the request equals the first page. If this is set to
|
||||
* {@literal true}, a page number of 1 in the request will be considered the first page.
|
||||
*
|
||||
* @return whether to assume 1-based page number indexes in the request parameters.
|
||||
*/
|
||||
protected boolean isOneIndexedParameters() {
|
||||
return this.oneIndexedParameters;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter(org.springframework.core.MethodParameter)
|
||||
@@ -219,7 +87,6 @@ public class PageableHandlerMethodArgumentResolver extends PageableHandlerMethod
|
||||
String page = webRequest.getParameter(getParameterNameToUse(getPageParameterName(), methodParameter));
|
||||
String pageSize = webRequest.getParameter(getParameterNameToUse(getSizeParameterName(), methodParameter));
|
||||
|
||||
|
||||
Sort sort = sortResolver.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
|
||||
Pageable pageable = getPageable(methodParameter, page, pageSize);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* 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
|
||||
* https://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,
|
||||
@@ -35,7 +35,9 @@ import org.springframework.util.StringUtils;
|
||||
* configured. Default configuration uses request parameters beginning with
|
||||
* {@link #DEFAULT_PAGE_PARAMETER}{@link #DEFAULT_QUALIFIER_DELIMITER}.
|
||||
*
|
||||
* @since 2.1
|
||||
* @since 2.2
|
||||
* @see PageableHandlerMethodArgumentResolver
|
||||
* @see ReactivePageableHandlerMethodArgumentResolver
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public abstract class PageableHandlerMethodArgumentResolverSupport {
|
||||
@@ -83,7 +85,7 @@ public abstract class PageableHandlerMethodArgumentResolverSupport {
|
||||
* @return
|
||||
*/
|
||||
public boolean isFallbackPageable(Pageable pageable) {
|
||||
return fallbackPageable == null ? false : fallbackPageable.equals(pageable);
|
||||
return fallbackPageable.equals(pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* 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
|
||||
* https://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,
|
||||
@@ -32,7 +32,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
* controller methods. Request properties to be parsed can be configured. Default configuration uses request parameters
|
||||
* beginning with {@link #DEFAULT_PAGE_PARAMETER}{@link #DEFAULT_QUALIFIER_DELIMITER}.
|
||||
*
|
||||
* @since 2.1
|
||||
* @since 2.2
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ReactivePageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolverSupport
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* 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
|
||||
* https://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,
|
||||
@@ -31,7 +31,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
* Reactive {@link HandlerMethodArgumentResolver} to create {@link Sort} instances from query string parameters or
|
||||
* {@link SortDefault} annotations.
|
||||
*
|
||||
* @since 2.1
|
||||
* @since 2.2
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ReactiveSortHandlerMethodArgumentResolver extends SortHandlerMethodArgumentResolverSupport
|
||||
|
||||
@@ -15,15 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
@@ -41,66 +38,8 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class SortHandlerMethodArgumentResolver extends SortHandlerMethodArgumentResolverSupport {
|
||||
|
||||
private static final String DEFAULT_PARAMETER = "sort";
|
||||
private static final String DEFAULT_PROPERTY_DELIMITER = ",";
|
||||
private static final String DEFAULT_QUALIFIER_DELIMITER = "_";
|
||||
private static final Sort DEFAULT_SORT = Sort.unsorted();
|
||||
|
||||
private static final String SORT_DEFAULTS_NAME = SortDefault.SortDefaults.class.getSimpleName();
|
||||
private static final String SORT_DEFAULT_NAME = SortDefault.class.getSimpleName();
|
||||
|
||||
private Sort fallbackSort = DEFAULT_SORT;
|
||||
private String sortParameter = DEFAULT_PARAMETER;
|
||||
private String propertyDelimiter = DEFAULT_PROPERTY_DELIMITER;
|
||||
private String qualifierDelimiter = DEFAULT_QUALIFIER_DELIMITER;
|
||||
|
||||
/**
|
||||
* Configure the request parameter to lookup sort information from. Defaults to {@code sort}.
|
||||
*
|
||||
* @param sortParameter must not be {@literal null} or empty.
|
||||
*/
|
||||
public void setSortParameter(String sortParameter) {
|
||||
|
||||
Assert.hasText(sortParameter, "SortParameter must not be null nor empty!");
|
||||
this.sortParameter = sortParameter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the delimiter used to separate property references and the direction to be sorted by. Defaults to
|
||||
* {@code}, which means sort values look like this: {@code firstname,lastname,asc}.
|
||||
*
|
||||
* @param propertyDelimiter must not be {@literal null} or empty.
|
||||
*/
|
||||
public void setPropertyDelimiter(String propertyDelimiter) {
|
||||
|
||||
Assert.hasText(propertyDelimiter, "Property delimiter must not be null or empty!");
|
||||
this.propertyDelimiter = propertyDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the delimiter used to separate the qualifier from the sort parameter. Defaults to {@code _}, so a
|
||||
* qualified sort property would look like {@code qualifier_sort}.
|
||||
*
|
||||
* @param qualifierDelimiter the qualifier delimiter to be used or {@literal null} to reset to the default.
|
||||
*/
|
||||
public void setQualifierDelimiter(String qualifierDelimiter) {
|
||||
this.qualifierDelimiter = qualifierDelimiter == null ? DEFAULT_QUALIFIER_DELIMITER : qualifierDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link Sort} to be used as fallback in case no {@link SortDefault} or {@link SortDefaults} (the
|
||||
* latter only supported in legacy mode) can be found at the method parameter to be resolved.
|
||||
* <p>
|
||||
* If you set this to {@literal null}, be aware that you controller methods will get {@literal null} handed into them
|
||||
* in case no {@link Sort} data can be found in the request.
|
||||
*
|
||||
* @param fallbackSort the {@link Sort} to be used as general fallback.
|
||||
*/
|
||||
public void setFallbackSort(Sort fallbackSort) {
|
||||
this.fallbackSort = fallbackSort;
|
||||
}
|
||||
public class SortHandlerMethodArgumentResolver extends SortHandlerMethodArgumentResolverSupport
|
||||
implements SortArgumentResolver {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* 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
|
||||
* https://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,
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -35,7 +36,7 @@ import org.springframework.util.StringUtils;
|
||||
* Base class providing methods for handler method argument resolvers to create {@link Sort} instances from request
|
||||
* parameters or {@link SortDefault} annotations.
|
||||
*
|
||||
* @since 2.1
|
||||
* @since 2.2
|
||||
* @see SortHandlerMethodArgumentResolver
|
||||
* @see ReactiveSortHandlerMethodArgumentResolver
|
||||
* @author Mark Paluch
|
||||
@@ -203,7 +204,9 @@ public abstract class SortHandlerMethodArgumentResolverSupport {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] elements = part.split(delimiter);
|
||||
String[] elements = Arrays.stream(part.split(delimiter)) //
|
||||
.filter(SortHandlerMethodArgumentResolver::notOnlyDots) //
|
||||
.toArray(String[]::new);
|
||||
|
||||
Optional<Direction> direction = elements.length == 0 ? Optional.empty()
|
||||
: Direction.fromOptionalString(elements[elements.length - 1]);
|
||||
@@ -286,6 +289,16 @@ public abstract class SortHandlerMethodArgumentResolverSupport {
|
||||
return builder == null ? Collections.emptyList() : builder.dumpExpressionIfPresentInto(expressions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given source {@link String} consists of dots only.
|
||||
*
|
||||
* @param source must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
static boolean notOnlyDots(String source) {
|
||||
return StringUtils.hasText(source.replace(".", ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to easily build request parameter expressions for {@link Sort} instances.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user