/
ModifyAclStrategy.java
64 lines (55 loc) · 2.5 KB
/
ModifyAclStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.things.persistence.actors.strategies.commands;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.base.common.Validator;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.things.AccessControlList;
import org.eclipse.ditto.model.things.AclValidator;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAcl;
import org.eclipse.ditto.signals.commands.things.modify.ModifyAclResponse;
import org.eclipse.ditto.signals.events.things.AclModified;
/**
* This strategy handles the {@link ModifyAcl} command.
*/
@Immutable
public final class ModifyAclStrategy
extends AbstractConditionalHeadersCheckingCommandStrategy<ModifyAcl, AccessControlList> {
/**
* Constructs a new {@code ModifyAclStrategy} object.
*/
ModifyAclStrategy() {
super(ModifyAcl.class);
}
@Override
protected Result doApply(final Context context, @Nullable final Thing thing,
final long nextRevision, final ModifyAcl command) {
final String thingId = context.getThingId();
final AccessControlList newAccessControlList = command.getAccessControlList();
final DittoHeaders dittoHeaders = command.getDittoHeaders();
final Validator aclValidator = AclValidator.newInstance(newAccessControlList,
Thing.MIN_REQUIRED_PERMISSIONS);
if (!aclValidator.isValid()) {
return ResultFactory.newErrorResult(ExceptionFactory.aclInvalid(thingId, aclValidator.getReason(),
dittoHeaders));
}
return ResultFactory.newMutationResult(command, AclModified.of(thingId, newAccessControlList, nextRevision,
getEventTimestamp(), dittoHeaders),
ModifyAclResponse.modified(thingId, newAccessControlList, command.getDittoHeaders()), this);
}
@Override
public Optional<AccessControlList> determineETagEntity(final ModifyAcl command, @Nullable final Thing thing) {
return getThingOrThrow(thing).getAccessControlList();
}
}