-
Notifications
You must be signed in to change notification settings - Fork 214
/
RetrieveConfigBehavior.java
73 lines (67 loc) · 2.93 KB
/
RetrieveConfigBehavior.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
65
66
67
68
69
70
71
72
73
/*
* Copyright (c) 2019 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.internal.utils.pekko.actors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonRuntimeException;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.api.common.RetrieveConfig;
import org.eclipse.ditto.base.api.common.RetrieveConfigResponse;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigRenderOptions;
import com.typesafe.config.ConfigValue;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.Actor;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Behavior to respond to {@code RetrieveConfig}.
*/
public interface RetrieveConfigBehavior extends Actor {
/**
* Get the config. Requires explicit setting because config relevant to this actor may not be
* {@code context().system().settings().config()}.
*
* @return the config.
*/
Config getConfig();
/**
* Injectable behavior to handle {@code RetrieveConfig}.
*
* @return behavior to handle {@code RetrieveConfig}.
*/
default AbstractActor.Receive retrieveConfigBehavior() {
return ReceiveBuilder.create()
.match(RetrieveConfig.class, retrieveConfig -> {
try {
final Config rootConfig = getConfig();
final ConfigValue configValue =
retrieveConfig.getPath().map(rootConfig::getValue).orElseGet(rootConfig::root);
final JsonValue configJson =
JsonFactory.readFrom(configValue.render(ConfigRenderOptions.concise()));
final RetrieveConfigResponse response =
RetrieveConfigResponse.of(configJson, retrieveConfig.getDittoHeaders());
sender().tell(response, self());
} catch (final ConfigException | DittoRuntimeException | JsonRuntimeException e) {
final JsonObject payload = JsonObject.newBuilder()
.set("error", e.toString())
.build();
final RetrieveConfigResponse response =
RetrieveConfigResponse.of(payload, retrieveConfig.getDittoHeaders());
sender().tell(response, self());
}
})
.build();
}
}