-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from bosch-io/feature/e2e_ack
End-2-end acks for Ditto Java Client
- Loading branch information
Showing
79 changed files
with
2,037 additions
and
790 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
75 changes: 75 additions & 0 deletions
75
java/src/main/java/org/eclipse/ditto/client/changes/AcknowledgementRequestHandle.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,75 @@ | ||
/* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.client.changes; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.json.JsonValue; | ||
import org.eclipse.ditto.model.base.acks.AcknowledgementLabel; | ||
import org.eclipse.ditto.model.base.common.HttpStatusCode; | ||
import org.eclipse.ditto.model.base.entity.id.EntityIdWithType; | ||
import org.eclipse.ditto.model.base.headers.WithDittoHeaders; | ||
import org.eclipse.ditto.signals.acks.base.Acknowledgement; | ||
|
||
/** | ||
* Abstraction for handling {@code AcknowledgementRequest}s issued by the Ditto backend together with e.g. | ||
* {@code Event}s. This handle provides means to {@code acknowledge} such acknowledgement requests with a custom | ||
* {@code HttpStatusCode}, an optional {@code payload} or - alternatively - with a self built {@code Acknowledgement}. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
@Immutable | ||
public interface AcknowledgementRequestHandle extends WithDittoHeaders<AcknowledgementRequestHandle> { | ||
|
||
/** | ||
* Returns the {@code AcknowledgementLabel} this handle was created for. | ||
* | ||
* @return the acknowledgement label to handle. | ||
*/ | ||
AcknowledgementLabel getAcknowledgementLabel(); | ||
|
||
/** | ||
* Returns the entity ID containing the entity type this handle was created for. | ||
* | ||
* @return the entity id to handle. | ||
*/ | ||
EntityIdWithType getEntityId(); | ||
|
||
/** | ||
* Builds and sends an {@code Acknowledgement} to the Ditto backend based on the information this handle instance | ||
* already has, combined with the passed {@code statusCode} and no {@code payload}. | ||
* | ||
* @param statusCode the http status code to apply for the acknowledgement to send: use a range between 200 and 300 | ||
* in order to declare a successful acknowledgement and a status code above 400 to declare a not successful one. | ||
*/ | ||
void acknowledge(HttpStatusCode statusCode); | ||
|
||
/** | ||
* Builds and sends an {@code Acknowledgement} to the Ditto backend based on the information this handle instance | ||
* already has, combined with the passed {@code statusCode} and the passed {@code payload}. | ||
* | ||
* @param statusCode the http status code to apply for the acknowledgement to send: use a range between 200 and 300 | ||
* in order to declare a successful acknowledgement and a status code above 400 to declare a not successful one. | ||
* @param payload the payload as {@code JsonValue} to include in the sent acknowledgement. | ||
*/ | ||
void acknowledge(HttpStatusCode statusCode, @Nullable JsonValue payload); | ||
|
||
/** | ||
* Sends the passed {@code acknowledgement} to the Ditto backend. | ||
* | ||
* @param acknowledgement the already built {@code Acknowledgement} to send to the Ditto backend. | ||
*/ | ||
void acknowledge(Acknowledgement acknowledgement); | ||
|
||
} |
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
131 changes: 131 additions & 0 deletions
131
...java/org/eclipse/ditto/client/changes/internal/ImmutableAcknowledgementRequestHandle.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,131 @@ | ||
/* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.client.changes.internal; | ||
|
||
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.client.changes.AcknowledgementRequestHandle; | ||
import org.eclipse.ditto.json.JsonValue; | ||
import org.eclipse.ditto.model.base.acks.AcknowledgementLabel; | ||
import org.eclipse.ditto.model.base.common.HttpStatusCode; | ||
import org.eclipse.ditto.model.base.entity.id.EntityIdWithType; | ||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.base.headers.DittoHeadersBuilder; | ||
import org.eclipse.ditto.signals.acks.base.Acknowledgement; | ||
|
||
/** | ||
* An immutable implementation of {@link AcknowledgementRequestHandle}. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
@Immutable | ||
final class ImmutableAcknowledgementRequestHandle implements AcknowledgementRequestHandle { | ||
|
||
private final AcknowledgementLabel acknowledgementLabel; | ||
private final EntityIdWithType entityId; | ||
private final DittoHeaders dittoHeaders; | ||
private final Consumer<Acknowledgement> acknowledgementPublisher; | ||
|
||
/** | ||
* Creates a new ImmutableAcknowledgementRequestHandle instance. | ||
* | ||
* @param acknowledgementLabel the acknowledgement label to handle. | ||
* @param entityId the entity id to handle. | ||
* @param dittoHeaders the ditto headers which were contained in the acknowledgement request to handle. | ||
* @param acknowledgementPublisher the consumer for publishing built acknowledgements to the Ditto backend. | ||
*/ | ||
ImmutableAcknowledgementRequestHandle(final AcknowledgementLabel acknowledgementLabel, | ||
final EntityIdWithType entityId, | ||
final DittoHeaders dittoHeaders, | ||
final Consumer<Acknowledgement> acknowledgementPublisher) { | ||
|
||
this.acknowledgementLabel = checkNotNull(acknowledgementLabel, "acknowledgementLabel"); | ||
this.entityId = checkNotNull(entityId, "entityId"); | ||
this.dittoHeaders = checkNotNull(dittoHeaders, "dittoHeaders"); | ||
this.acknowledgementPublisher = checkNotNull(acknowledgementPublisher, "acknowledgementPublisher"); | ||
} | ||
|
||
@Override | ||
public AcknowledgementLabel getAcknowledgementLabel() { | ||
return acknowledgementLabel; | ||
} | ||
|
||
@Override | ||
public EntityIdWithType getEntityId() { | ||
return entityId; | ||
} | ||
|
||
@Override | ||
public DittoHeaders getDittoHeaders() { | ||
return dittoHeaders; | ||
} | ||
|
||
@Override | ||
public AcknowledgementRequestHandle setDittoHeaders(final DittoHeaders dittoHeaders) { | ||
return new ImmutableAcknowledgementRequestHandle(acknowledgementLabel, entityId, dittoHeaders, | ||
acknowledgementPublisher); | ||
} | ||
|
||
@Override | ||
public void acknowledge(final HttpStatusCode statusCode) { | ||
acknowledge(statusCode, null); | ||
} | ||
|
||
@Override | ||
public void acknowledge(final HttpStatusCode statusCode, @Nullable final JsonValue payload) { | ||
// only retain the bare minimum of received DittoHeaders by default: | ||
final DittoHeadersBuilder<?, ?> dittoHeadersBuilder = DittoHeaders.newBuilder(); | ||
dittoHeaders.getCorrelationId().ifPresent(dittoHeadersBuilder::correlationId); | ||
final DittoHeaders minimizedDittoHeaders = dittoHeadersBuilder.build(); | ||
acknowledge(Acknowledgement.of(acknowledgementLabel, entityId, statusCode, minimizedDittoHeaders, payload)); | ||
} | ||
|
||
@Override | ||
public void acknowledge(final Acknowledgement acknowledgement) { | ||
acknowledgementPublisher.accept(acknowledgement); | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final ImmutableAcknowledgementRequestHandle that = (ImmutableAcknowledgementRequestHandle) o; | ||
return Objects.equals(acknowledgementLabel, that.acknowledgementLabel) && | ||
Objects.equals(entityId, that.entityId) && | ||
Objects.equals(dittoHeaders, that.dittoHeaders); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(acknowledgementLabel, entityId, dittoHeaders); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + " [" + | ||
"acknowledgementLabel=" + acknowledgementLabel + | ||
", entityId=" + entityId + | ||
", dittoHeaders=" + dittoHeaders + | ||
"]"; | ||
} | ||
} |
Oops, something went wrong.