@Override public PurgeNamespaceResponse setDittoHeaders(final DittoHeaders dittoHeaders) { if (Objects.equals(getDittoHeaders(), dittoHeaders)) { return this; } return new PurgeNamespaceResponse(getNamespace(), getResourceType(), isSuccessful(), dittoHeaders); }
private void purgeNamespace(final PurgeNamespace purgeNamespace) { LogUtil.enhanceLogWithCorrelationId(log, purgeNamespace); final Collection<S> namespaceSelections = selectNamespace(purgeNamespace.getNamespace()); log.info("Running <{}>. Affected collections: <{}>.", purgeNamespace, namespaceSelections); final ActorRef sender = getSender(); namespaceOps.purgeAll(namespaceSelections) .runWith(Sink.head(), materializer) .thenAccept(errors -> { // send response to speed up purge workflow final PurgeNamespaceResponse response; if (errors.isEmpty()) { response = PurgeNamespaceResponse.successful(purgeNamespace.getNamespace(), getResourceType(), purgeNamespace.getDittoHeaders()); } else { LogUtil.enhanceLogWithCorrelationId(log, purgeNamespace); final String namespace = purgeNamespace.getNamespace(); errors.forEach(error -> log.error(error, "Error purging namespace <{}>", namespace)); response = PurgeNamespaceResponse.failed(namespace, getResourceType(), purgeNamespace.getDittoHeaders()); } sender.tell(response, getSelf()); }) .exceptionally(error -> { LogUtil.enhanceLogWithCorrelationId(log, purgeNamespace); log.error(error, "Failed to purge namespace <{}>!", purgeNamespace.getNamespace()); // Reply nothing - DB errors were converted to stream elements and handled return null; }); }
expectMsg(PurgeNamespaceResponse.successful(purgedNamespace, getResourceType(), dittoHeaders));
/** * Returns an instance of {@code PurgeNamespaceResponse} which indicates that a namespace purge failed. * * @param namespace the namespace the returned response relates to. * @param resourceType type of the {@code Resource} represented by the returned response. * @param dittoHeaders the headers of the command which caused the returned response. * @return a response for a failed namespace purge. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code namespace} or {@code resourceType} is empty. */ public static PurgeNamespaceResponse failed(final CharSequence namespace, final CharSequence resourceType, final DittoHeaders dittoHeaders) { return new PurgeNamespaceResponse(namespace, resourceType, false, dittoHeaders); }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } if (!super.equals(o)) { return false; } final PurgeNamespaceResponse that = (PurgeNamespaceResponse) o; return that.canEqual(this) && successful == that.successful && super.equals(that); }
@Override protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, final Predicate<JsonField> predicate) { super.appendPayload(jsonObjectBuilder, schemaVersion, predicate); jsonObjectBuilder.set(JsonFields.SUCCESSFUL, isSuccessful(), schemaVersion.and(predicate)); }
/** * Returns an instance of {@code PurgeNamespaceResponse} which indicates a successful namespace purge. * * @param namespace the namespace the returned response relates to. * @param resourceType type of the {@code Resource} represented by the returned response. * @param dittoHeaders the headers of the command which caused the returned response. * @return a response for a successful namespace purge. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code namespace} or {@code resourceType} is empty. */ public static PurgeNamespaceResponse successful(final CharSequence namespace, final CharSequence resourceType, final DittoHeaders dittoHeaders) { return new PurgeNamespaceResponse(namespace, resourceType, true, dittoHeaders); }
/** * Creates a new {@code PurgeNamespaceResponse} from the given JSON object. * * @param jsonObject the JSON object of which the PurgeNamespaceResponse is to be created. * @param headers the headers. * @return the deserialized response. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} was not in the expected format. * @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain * <ul> * <li>{@link org.eclipse.ditto.signals.commands.namespaces.NamespaceCommandResponse.JsonFields#NAMESPACE},</li> * <li>{@link org.eclipse.ditto.signals.commands.namespaces.NamespaceCommandResponse.JsonFields#RESOURCE_TYPE} or</li> * <li>{@link JsonFields#SUCCESSFUL}.</li> * </ul> */ public static PurgeNamespaceResponse fromJson(final JsonObject jsonObject, final DittoHeaders headers) { return new CommandResponseJsonDeserializer<PurgeNamespaceResponse>(TYPE, jsonObject).deserialize(statusCode -> { final String namespace = jsonObject.getValueOrThrow(NamespaceCommandResponse.JsonFields.NAMESPACE); final String resourceType = jsonObject.getValueOrThrow(NamespaceCommandResponse.JsonFields.RESOURCE_TYPE); final Boolean isSuccessful = jsonObject.getValueOrThrow(JsonFields.SUCCESSFUL); return new PurgeNamespaceResponse(namespace, resourceType, isSuccessful, headers); }); }