forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement argument resolver for OffsetScrollPosition
- Loading branch information
Showing
9 changed files
with
699 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/org/springframework/data/web/OffsetScrollPositionArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2016-2023 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 | ||
* | ||
* 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, | ||
* 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.OffsetScrollPosition; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.bind.WebDataBinder; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
/** | ||
* Argument resolver to extract a {@link OffsetScrollPosition} object from a {@link NativeWebRequest} for a particular | ||
* {@link MethodParameter}. A {@link OffsetScrollPositionArgumentResolver} can either resolve {@link OffsetScrollPosition} itself or wrap another | ||
* {@link OffsetScrollPositionArgumentResolver} to post-process {@link OffsetScrollPosition}. | ||
* | ||
* @since 3.2 | ||
* @author Yanming Zhou | ||
* @see HandlerMethodArgumentResolver | ||
*/ | ||
public interface OffsetScrollPositionArgumentResolver extends HandlerMethodArgumentResolver { | ||
|
||
/** | ||
* Resolves a {@link OffsetScrollPosition} method parameter into an argument value from a given request. | ||
* | ||
* @param parameter the method parameter to resolve. This parameter must have previously been passed to | ||
* {@link #supportsParameter} which must have returned {@code true}. | ||
* @param mavContainer the ModelAndViewContainer for the current request | ||
* @param webRequest the current request | ||
* @param binderFactory a factory for creating {@link WebDataBinder} instances | ||
* @return the resolved argument value | ||
*/ | ||
@NonNull | ||
@Override | ||
OffsetScrollPosition resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory); | ||
} |
49 changes: 49 additions & 0 deletions
49
.../java/org/springframework/data/web/OffsetScrollPositionHandlerMethodArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2013-2023 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 | ||
* | ||
* 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, | ||
* 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.OffsetScrollPosition; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* {@link HandlerMethodArgumentResolver} to automatically create {@link OffsetScrollPosition} instances from request parameters. | ||
* | ||
* @since 3.2 | ||
* @author Yanming Zhou | ||
*/ | ||
public class OffsetScrollPositionHandlerMethodArgumentResolver extends OffsetScrollPositionHandlerMethodArgumentResolverSupport | ||
implements OffsetScrollPositionArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return OffsetScrollPosition.class.equals(parameter.getParameterType()); | ||
} | ||
|
||
@Override | ||
public OffsetScrollPosition resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) { | ||
|
||
String[] offsetParameter = webRequest.getParameterValues(getOffsetParameter(parameter)); | ||
return parseParameterIntoOffsetScrollPosition(offsetParameter != null ? Arrays.asList(offsetParameter) : null); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...rg/springframework/data/web/OffsetScrollPositionHandlerMethodArgumentResolverSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2017-2023 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 | ||
* | ||
* 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, | ||
* 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.OffsetScrollPosition; | ||
import org.springframework.data.domain.ScrollPosition; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Base class providing methods for handler method argument resolvers to create {@link OffsetScrollPosition} instances from request | ||
* parameters. | ||
* | ||
* @since 3.2 | ||
* @author Yanming Zhou | ||
* @see OffsetScrollPositionHandlerMethodArgumentResolver | ||
* @see ReactiveOffsetScrollPositionHandlerMethodArgumentResolver | ||
*/ | ||
public abstract class OffsetScrollPositionHandlerMethodArgumentResolverSupport { | ||
|
||
private static final String DEFAULT_PARAMETER = "offset"; | ||
|
||
private static final String DEFAULT_QUALIFIER_DELIMITER = "_"; | ||
|
||
private String offsetParameter = DEFAULT_PARAMETER; | ||
|
||
private String qualifierDelimiter = DEFAULT_QUALIFIER_DELIMITER; | ||
|
||
/** | ||
* Configure the request parameter to lookup offset information from. Defaults to {@code offset}. | ||
* | ||
* @param offsetParameter must not be {@literal null} or empty. | ||
*/ | ||
public void setOffsetParameter(String offsetParameter) { | ||
|
||
Assert.hasText(offsetParameter, "offsetParameter must not be null nor empty"); | ||
this.offsetParameter = offsetParameter; | ||
} | ||
|
||
/** | ||
* Configures the delimiter used to separate the qualifier from the offset parameter. Defaults to {@code _}, so a | ||
* qualified offset property would look like {@code qualifier_offset}. | ||
* | ||
* @param qualifierDelimiter the qualifier delimiter to be used or {@literal null} to reset to the default. | ||
*/ | ||
public void setQualifierDelimiter(@Nullable String qualifierDelimiter) { | ||
this.qualifierDelimiter = qualifierDelimiter == null ? DEFAULT_QUALIFIER_DELIMITER : qualifierDelimiter; | ||
} | ||
|
||
/** | ||
* Returns the offset parameter to be looked up from the request. Potentially applies qualifiers to it. | ||
* | ||
* @param parameter can be {@literal null}. | ||
* @return the offset parameter | ||
*/ | ||
protected String getOffsetParameter(@Nullable MethodParameter parameter) { | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
|
||
String value = SpringDataAnnotationUtils.getQualifier(parameter); | ||
|
||
if (StringUtils.hasLength(value)) { | ||
builder.append(value); | ||
builder.append(qualifierDelimiter); | ||
} | ||
|
||
return builder.append(offsetParameter).toString(); | ||
} | ||
|
||
/** | ||
* Parses the given source into a {@link OffsetScrollPosition} instance. | ||
* | ||
* @param source could be {@literal null} or empty. | ||
* @return parsed OffsetScrollPosition | ||
*/ | ||
OffsetScrollPosition parseParameterIntoOffsetScrollPosition(@Nullable List<String> source) { | ||
// No parameter or Single empty parameter, e.g "offset=" | ||
if (source == null || source.size() == 1 && !StringUtils.hasText(source.get(0))) { | ||
return ScrollPosition.offset(); | ||
} | ||
try { | ||
long offset = Long.parseLong(source.get(0)); | ||
return ScrollPosition.offset(offset); | ||
} catch (NumberFormatException ex) { | ||
return ScrollPosition.offset(); | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...g/springframework/data/web/ReactiveOffsetScrollPositionHandlerMethodArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017-2023 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 | ||
* | ||
* 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, | ||
* 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.OffsetScrollPosition; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.reactive.BindingContext; | ||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver; | ||
import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Reactive {@link HandlerMethodArgumentResolver} to create {@link OffsetScrollPosition} instances from query string parameters. | ||
* | ||
* @since 3.2 | ||
* @author Yanming Zhou | ||
*/ | ||
public class ReactiveOffsetScrollPositionHandlerMethodArgumentResolver extends OffsetScrollPositionHandlerMethodArgumentResolverSupport | ||
implements SyncHandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return OffsetScrollPosition.class.equals(parameter.getParameterType()); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public OffsetScrollPosition resolveArgumentValue(MethodParameter parameter, BindingContext bindingContext, | ||
ServerWebExchange exchange) { | ||
|
||
List<String> offsetParameter = exchange.getRequest().getQueryParams().get(getOffsetParameter(parameter)); | ||
|
||
return parseParameterIntoOffsetScrollPosition(offsetParameter); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ramework/data/web/config/OffsetScrollPositionHandlerMethodArgumentResolverCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2017-2023 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 | ||
* | ||
* 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, | ||
* 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.config; | ||
|
||
import org.springframework.data.web.OffsetScrollPositionHandlerMethodArgumentResolver; | ||
|
||
/** | ||
* Callback interface that can be implemented by beans wishing to customize the | ||
* {@link OffsetScrollPositionHandlerMethodArgumentResolver} configuration. | ||
* | ||
* @since 2.0 | ||
* @author Yanming Zhou | ||
*/ | ||
public interface OffsetScrollPositionHandlerMethodArgumentResolverCustomizer { | ||
|
||
/** | ||
* Customize the given {@link OffsetScrollPositionHandlerMethodArgumentResolver}. | ||
* | ||
* @param offsetResolver the {@link OffsetScrollPositionHandlerMethodArgumentResolver} to customize, will never be {@literal null}. | ||
*/ | ||
void customize(OffsetScrollPositionHandlerMethodArgumentResolver offsetResolver); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.