|
34 | 34 | import com.google.gson.JsonElement; |
35 | 35 | import com.google.gson.JsonParser; |
36 | 36 | import com.google.gson.stream.JsonToken; |
| 37 | +import com.sap.cloud.sdk.cloudplatform.connectivity.UriQueryMerger; |
37 | 38 | import com.sap.cloud.sdk.datamodel.odata.client.JsonPath; |
38 | 39 | import com.sap.cloud.sdk.datamodel.odata.client.ODataProtocol; |
39 | 40 | import com.sap.cloud.sdk.datamodel.odata.client.ODataResponseDeserializer; |
@@ -554,10 +555,10 @@ public Option<String> getNextLink() |
554 | 555 | for( final JsonPath path : getODataRequest().getProtocol().getPathToNextLink().getPaths() ) { |
555 | 556 | final ResultElement resultElement = getResultElement(path); |
556 | 557 | if( resultElement != null ) { |
557 | | - return Option |
558 | | - .of(resultElement) |
559 | | - .map(ResultElement::asString) |
560 | | - .peek(link -> log.debug("Found reference to next page: {}", link)); |
| 558 | + String nextLink = resultElement.asString(); |
| 559 | + log.debug("Found reference to next page: {}", nextLink); |
| 560 | + nextLink = removeDuplicateQueryParameters(nextLink); |
| 561 | + return Option.of(nextLink); |
561 | 562 | } |
562 | 563 | } |
563 | 564 | log.debug("Result does not reference any further pages."); |
@@ -751,4 +752,31 @@ private void assertResultTypeIsNotVoid( @Nonnull final Class<?> cls ) |
751 | 752 | } |
752 | 753 |
|
753 | 754 | } |
| 755 | + |
| 756 | + @Nonnull |
| 757 | + private String removeDuplicateQueryParameters( @Nonnull final String nextLink ) |
| 758 | + { |
| 759 | + if( !(httpClient instanceof UriQueryMerger) ) { |
| 760 | + return nextLink; |
| 761 | + } |
| 762 | + final String query = ((UriQueryMerger) httpClient).mergeRequestUri(URI.create("")).getRawQuery(); |
| 763 | + if( query == null ) { |
| 764 | + return nextLink; |
| 765 | + } |
| 766 | + final String[] segments = nextLink.split("\\?", 2); |
| 767 | + final String[] queryArguments = query.split("&"); |
| 768 | + for( final String argument : queryArguments ) { |
| 769 | + if( segments[1].contains(argument) ) { |
| 770 | + segments[1] = segments[1].replace(argument, ""); |
| 771 | + } |
| 772 | + } |
| 773 | + if( nextLink.length() + 1 == segments[0].length() + segments[1].length() ) { |
| 774 | + return nextLink; |
| 775 | + } |
| 776 | + // after removal of arguments clean-up query: fix "?foo=bar&&&one=1", fix "?&one=1", fix "?foo=bar&" |
| 777 | + segments[1] = segments[1].replaceAll("&&+", "&").replace("?&", "?").replaceAll("&$", ""); |
| 778 | + final String updatedLink = segments[0] + "?" + segments[1]; |
| 779 | + log.debug("Updated reference to next page: {}", updatedLink); |
| 780 | + return updatedLink; |
| 781 | + } |
754 | 782 | } |
0 commit comments