-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dsp): catalog pagination through continuation token and Link hea…
…der (#4103) feat(dsp): catalog pagination through Link header
- Loading branch information
Showing
21 changed files
with
824 additions
and
71 deletions.
There are no files selected for viewing
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
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
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
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
57 changes: 57 additions & 0 deletions
57
...rg/eclipse/edc/protocol/dsp/catalog/http/api/decorator/Base64continuationTokenSerDes.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,57 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.catalog.http.api.decorator; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.jsonld.spi.JsonLd; | ||
import org.eclipse.edc.protocol.dsp.http.spi.message.ContinuationTokenSerDes; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.Result; | ||
import org.eclipse.edc.transform.spi.TypeTransformerRegistry; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.Base64; | ||
|
||
public class Base64continuationTokenSerDes implements ContinuationTokenSerDes { | ||
|
||
private final TypeTransformerRegistry typeTransformerRegistry; | ||
private final JsonLd jsonLd; | ||
|
||
public Base64continuationTokenSerDes(TypeTransformerRegistry typeTransformerRegistry, JsonLd jsonLd) { | ||
this.typeTransformerRegistry = typeTransformerRegistry; | ||
this.jsonLd = jsonLd; | ||
} | ||
|
||
@Override | ||
public Result<String> serialize(QuerySpec querySpec) { | ||
return typeTransformerRegistry.transform(querySpec, JsonObject.class) | ||
.map(Object::toString) | ||
.map(String::getBytes) | ||
.map(Base64.getEncoder()::encodeToString); | ||
} | ||
|
||
@Override | ||
public Result<JsonObject> deserialize(String serialized) { | ||
try { | ||
var decode = Base64.getDecoder().decode(serialized); | ||
var jsonObject = Json.createReader(new ByteArrayInputStream(decode)).readObject(); | ||
return jsonLd.expand(jsonObject); | ||
} catch (Exception e) { | ||
return Result.failure("Cannot deserialize continuationToken: " + e.getMessage()); | ||
} | ||
|
||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...lipse/edc/protocol/dsp/catalog/http/api/decorator/CatalogPaginationResponseDecorator.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,63 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.catalog.http.api.decorator; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequestMessage; | ||
import org.eclipse.edc.protocol.dsp.http.spi.message.ContinuationTokenSerDes; | ||
import org.eclipse.edc.protocol.dsp.http.spi.message.ResponseDecorator; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
|
||
import java.net.URI; | ||
import java.util.function.IntBinaryOperator; | ||
|
||
public class CatalogPaginationResponseDecorator implements ResponseDecorator<CatalogRequestMessage, Catalog> { | ||
|
||
private static final String NEXT = "next"; | ||
private static final String PREV = "prev"; | ||
|
||
private final String requestUrl; | ||
private final ContinuationTokenSerDes continuationTokenSerDes; | ||
private final Monitor monitor; | ||
|
||
public CatalogPaginationResponseDecorator(String requestUrl, ContinuationTokenSerDes continuationTokenSerDes, Monitor monitor) { | ||
this.requestUrl = requestUrl; | ||
this.continuationTokenSerDes = continuationTokenSerDes; | ||
this.monitor = monitor; | ||
} | ||
|
||
@Override | ||
public Response.ResponseBuilder decorate(Response.ResponseBuilder responseBuilder, CatalogRequestMessage requestBody, Catalog responseBody) { | ||
var currentQuerySpec = requestBody.getQuerySpec(); | ||
if (responseBody.getDatasets().size() == currentQuerySpec.getLimit()) { | ||
addLink(NEXT, responseBuilder, currentQuerySpec, (offset, limit) -> offset + limit); | ||
} | ||
|
||
if (currentQuerySpec.getOffset() >= currentQuerySpec.getLimit()) { | ||
addLink(PREV, responseBuilder, currentQuerySpec, (offset, limit) -> offset - limit); | ||
} | ||
|
||
return responseBuilder; | ||
} | ||
|
||
private void addLink(String rel, Response.ResponseBuilder responseBuilder, QuerySpec currentQuerySpec, IntBinaryOperator newOffsetOperator) { | ||
var newOffset = newOffsetOperator.applyAsInt(currentQuerySpec.getOffset(), currentQuerySpec.getLimit()); | ||
continuationTokenSerDes.serialize(currentQuerySpec.toBuilder().offset(newOffset).build()) | ||
.onSuccess(token -> responseBuilder.link(URI.create(requestUrl + "?continuationToken=" + token), rel)) | ||
.onFailure(failure -> monitor.warning("Cannot serialize continuationToken for catalog pagination: " + failure.getFailureDetail())); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...org/eclipse/edc/protocol/dsp/catalog/http/api/decorator/ContinuationTokenManagerImpl.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 (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.catalog.http.api.decorator; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonObjectBuilder; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.CatalogRequestMessage; | ||
import org.eclipse.edc.protocol.dsp.http.spi.message.ContinuationTokenManager; | ||
import org.eclipse.edc.protocol.dsp.http.spi.message.ResponseDecorator; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.result.Result; | ||
|
||
import static org.eclipse.edc.protocol.dsp.spi.type.DspCatalogPropertyAndTypeNames.DSPACE_PROPERTY_FILTER; | ||
|
||
public class ContinuationTokenManagerImpl implements ContinuationTokenManager { | ||
|
||
private final Base64continuationTokenSerDes continuationTokenSerDes; | ||
private final Monitor monitor; | ||
|
||
public ContinuationTokenManagerImpl(Base64continuationTokenSerDes continuationTokenSerDes, Monitor monitor) { | ||
this.continuationTokenSerDes = continuationTokenSerDes; | ||
this.monitor = monitor; | ||
} | ||
|
||
@Override | ||
public ResponseDecorator<CatalogRequestMessage, Catalog> createResponseDecorator(String requestUrl) { | ||
return new CatalogPaginationResponseDecorator(requestUrl, continuationTokenSerDes, monitor); | ||
} | ||
|
||
@Override | ||
public Result<JsonObject> applyQueryFromToken(JsonObject requestMessage, String continuationToken) { | ||
return continuationTokenSerDes.deserialize(continuationToken) | ||
.map(query -> Json.createArrayBuilder().add(query)) | ||
.map(filter -> Json.createObjectBuilder(requestMessage).add(DSPACE_PROPERTY_FILTER, filter)) | ||
.map(JsonObjectBuilder::build); | ||
} | ||
} |
Oops, something went wrong.