forked from eclipse-ditto/ditto
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-ditto#964] add notifications to Ditto protocol; fix ShardReg…
…ionExtractorTest. Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
- Loading branch information
Showing
28 changed files
with
536 additions
and
70 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
28 changes: 28 additions & 0 deletions
28
...dapter/src/main/java/org/eclipse/ditto/protocoladapter/NotificationsTopicPathBuilder.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,28 @@ | ||
/* | ||
* Copyright (c) 2021 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.protocoladapter; | ||
|
||
/** | ||
* Builder to create a topic path for notifications. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public interface NotificationsTopicPathBuilder extends TopicPathBuildable { | ||
|
||
/** | ||
* Set the notification name on the topic path. | ||
* | ||
* @return this builder. | ||
*/ | ||
NotificationsTopicPathBuilder name(String name); | ||
} |
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
79 changes: 79 additions & 0 deletions
79
...ava/org/eclipse/ditto/protocoladapter/adaptables/PolicyNotificationMappingStrategies.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,79 @@ | ||
/* | ||
* Copyright (c) 2021 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.protocoladapter.adaptables; | ||
|
||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.json.JsonValue; | ||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.policies.PolicyId; | ||
import org.eclipse.ditto.model.policies.SubjectId; | ||
import org.eclipse.ditto.protocoladapter.Adaptable; | ||
import org.eclipse.ditto.protocoladapter.JsonifiableMapper; | ||
import org.eclipse.ditto.signals.notifications.policies.PolicyNotification; | ||
import org.eclipse.ditto.signals.notifications.policies.SubjectExpiryNotification; | ||
|
||
/** | ||
* Defines mapping strategies (map from signal type to JsonifiableMapper) for policy notifications. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
final class PolicyNotificationMappingStrategies extends AbstractPolicyMappingStrategies<PolicyNotification<?>> { | ||
|
||
private static final PolicyNotificationMappingStrategies INSTANCE = new PolicyNotificationMappingStrategies(); | ||
|
||
private PolicyNotificationMappingStrategies() { | ||
super(initMappingStrategies()); | ||
} | ||
|
||
/** | ||
* Get the unique instance of this class. | ||
* | ||
* @return the instance. | ||
*/ | ||
public static PolicyNotificationMappingStrategies getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
private static Map<String, JsonifiableMapper<PolicyNotification<?>>> initMappingStrategies() { | ||
final Map<String, JsonifiableMapper<PolicyNotification<?>>> mappingStrategies = new HashMap<>(); | ||
mappingStrategies.put(SubjectExpiryNotification.TYPE, | ||
PolicyNotificationMappingStrategies::toSubjectExpiryNotification); | ||
return mappingStrategies; | ||
} | ||
|
||
private static SubjectExpiryNotification toSubjectExpiryNotification(final Adaptable adaptable) { | ||
final PolicyId policyId = policyIdFromTopicPath(adaptable.getTopicPath()); | ||
final DittoHeaders dittoHeaders = dittoHeadersFrom(adaptable); | ||
final JsonObject payload = adaptable.getPayload() | ||
.getValue() | ||
.filter(JsonValue::isObject) | ||
.map(JsonValue::asObject) | ||
.orElseThrow(NoSuchElementException::new); | ||
final Instant expiry = Instant.parse(payload.getValueOrThrow(SubjectExpiryNotification.JsonFields.EXPIRY)); | ||
final Collection<SubjectId> expiringSubjectIds = | ||
payload.getValueOrThrow(SubjectExpiryNotification.JsonFields.EXPIRING_SUBJECTS) | ||
.stream() | ||
.map(JsonValue::asString) | ||
.map(SubjectId::newInstance) | ||
.collect(Collectors.toList()); | ||
return SubjectExpiryNotification.of(policyId, expiry, expiringSubjectIds, dittoHeaders); | ||
} | ||
|
||
} |
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
72 changes: 72 additions & 0 deletions
72
...r/src/main/java/org/eclipse/ditto/protocoladapter/policies/PolicyNotificationAdapter.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,72 @@ | ||
/* | ||
* Copyright (c) 2021 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.protocoladapter.policies; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.eclipse.ditto.protocoladapter.Adaptable; | ||
import org.eclipse.ditto.protocoladapter.HeaderTranslator; | ||
import org.eclipse.ditto.protocoladapter.TopicPath; | ||
import org.eclipse.ditto.protocoladapter.UnknownTopicPathException; | ||
import org.eclipse.ditto.protocoladapter.adaptables.MappingStrategiesFactory; | ||
import org.eclipse.ditto.protocoladapter.signals.SignalMapperFactory; | ||
import org.eclipse.ditto.signals.notifications.policies.PolicyNotification; | ||
|
||
/** | ||
* Adapter for mapping a {@link PolicyNotification} to and from an {@link org.eclipse.ditto.protocoladapter.Adaptable}. | ||
*/ | ||
final class PolicyNotificationAdapter extends AbstractPolicyAdapter<PolicyNotification<?>> { | ||
|
||
private PolicyNotificationAdapter(final HeaderTranslator headerTranslator) { | ||
super(MappingStrategiesFactory.getPolicyNotificationMappingStrategies(), | ||
SignalMapperFactory.newPolicyNotificationSignalMapper(), | ||
headerTranslator); | ||
} | ||
|
||
/** | ||
* Returns a new PolicyNotificationAdapter. | ||
* | ||
* @param headerTranslator translator between external and Ditto headers. | ||
* @return the adapter. | ||
*/ | ||
public static PolicyNotificationAdapter of(final HeaderTranslator headerTranslator) { | ||
return new PolicyNotificationAdapter(requireNonNull(headerTranslator)); | ||
} | ||
|
||
@Override | ||
public Set<TopicPath.Criterion> getCriteria() { | ||
return Collections.singleton(TopicPath.Criterion.NOTIFICATIONS); | ||
} | ||
|
||
@Override | ||
public Set<TopicPath.Action> getActions() { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public boolean isForResponses() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected String getType(final Adaptable adaptable) { | ||
final TopicPath topicPath = adaptable.getTopicPath(); | ||
final String commandName = | ||
topicPath.getSubject().orElseThrow(() -> UnknownTopicPathException.newBuilder(topicPath).build()); | ||
return topicPath.getGroup() + "." + getTypeCriterionAsString(topicPath) + ":" + commandName; | ||
} | ||
|
||
} |
Oops, something went wrong.