use of org.openremote.container.message.MessageBrokerService in project openremote by openremote.
the class Main method main.
public static void main(String[] args) throws Exception {
List<ContainerService> services = new ArrayList<ContainerService>() {
{
addAll(Arrays.asList(new TimerService(), new ManagerExecutorService(), new I18NService(), new ManagerPersistenceService(), new MessageBrokerSetupService(), new ManagerIdentityService(), new SetupService(), new ClientEventService(), new RulesetStorageService(), new RulesService(), new AssetStorageService(), new AssetDatapointService(), new AssetAttributeLinkingService(), new AssetProcessingService(), new MessageBrokerService()));
ServiceLoader.load(Protocol.class).forEach(this::add);
addAll(Arrays.asList(new AgentService(), new SimulatorService(), new MapService(), new NotificationService(), new ConsoleAppService(), new ManagerWebService()));
}
};
new Container(services).startBackground();
}
use of org.openremote.container.message.MessageBrokerService in project openremote by openremote.
the class AssetProcessingService method init.
@Override
public void init(Container container) throws Exception {
timerService = container.getService(TimerService.class);
identityService = container.getService(ManagerIdentityService.class);
persistenceService = container.getService(PersistenceService.class);
rulesService = container.getService(RulesService.class);
agentService = container.getService(AgentService.class);
assetStorageService = container.getService(AssetStorageService.class);
assetDatapointService = container.getService(AssetDatapointService.class);
assetAttributeLinkingService = container.getService(AssetAttributeLinkingService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
clientEventService = container.getService(ClientEventService.class);
clientEventService.addSubscriptionAuthorizer((auth, subscription) -> {
if (!subscription.isEventType(AttributeEvent.class)) {
return false;
}
// Always must have a filter, as you can't subscribe to ALL asset attribute events
if (subscription.getFilter() != null && subscription.getFilter() instanceof AttributeEvent.EntityIdFilter) {
AttributeEvent.EntityIdFilter filter = (AttributeEvent.EntityIdFilter) subscription.getFilter();
// Superuser can get attribute events for any asset
if (auth.isSuperUser())
return true;
// Regular user must have role
if (!auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
return false;
}
boolean isRestrictedUser = identityService.getIdentityProvider().isRestrictedUser(auth.getUserId());
// Client can subscribe to several assets
for (String assetId : filter.getEntityId()) {
Asset asset = assetStorageService.find(assetId);
// If the asset doesn't exist, subscription must fail
if (asset == null)
return false;
if (isRestrictedUser) {
// Restricted users can only get attribute events for their linked assets
if (!assetStorageService.isUserAsset(auth.getUserId(), assetId))
return false;
// TODO Restricted clients should only receive events for RESTRICTED_READ attributes!
} else {
// Regular users can only get attribute events for assets in their realm
if (!asset.getTenantRealm().equals(auth.getAuthenticatedRealm()))
return false;
}
}
return true;
}
return false;
});
processors.add(agentService);
processors.add(rulesService);
processors.add(assetDatapointService);
processors.add(assetAttributeLinkingService);
container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);
}
use of org.openremote.container.message.MessageBrokerService in project openremote by openremote.
the class ClientEventService method init.
@Override
public void init(Container container) throws Exception {
timerService = container.getService(TimerService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
identityService = container.getService(ManagerIdentityService.class);
eventSubscriptions = new EventSubscriptions(container.getService(TimerService.class), container.getService(ManagerExecutorService.class));
MessageBrokerSetupService messageBrokerSetupService = container.getService(MessageBrokerSetupService.class);
messageBrokerSetupService.getContext().getTypeConverterRegistry().addTypeConverters(new EventTypeConverters());
messageBrokerSetupService.getContext().addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("websocket://" + WEBSOCKET_EVENTS).routeId("FromClientWebsocketEvents").choice().when(header(WebsocketConstants.SESSION_OPEN)).process(exchange -> {
// Do nothing except stop the exchanges
}).stop().when(or(header(WebsocketConstants.SESSION_CLOSE), header(WebsocketConstants.SESSION_CLOSE_ERROR))).process(exchange -> {
String sessionKey = getSessionKey(exchange);
eventSubscriptions.cancelAll(sessionKey);
}).stop().end().choice().when(bodyAs(String.class).startsWith(EventSubscription.MESSAGE_PREFIX)).convertBodyTo(EventSubscription.class).process(exchange -> {
String sessionKey = getSessionKey(exchange);
EventSubscription subscription = exchange.getIn().getBody(EventSubscription.class);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
if (eventSubscriptionAuthorizers.stream().anyMatch(authorizer -> authorizer.apply(authContext, subscription))) {
boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId());
eventSubscriptions.update(sessionKey, restrictedUser, subscription);
} else {
LOG.warning("Unauthorized subscription from '" + authContext.getUsername() + "' in realm '" + authContext.getAuthenticatedRealm() + "': " + subscription);
sendToSession(sessionKey, new UnauthorizedEventSubscription(subscription.getEventType()));
}
}).when(bodyAs(String.class).startsWith(CancelEventSubscription.MESSAGE_PREFIX)).convertBodyTo(CancelEventSubscription.class).process(exchange -> {
String sessionKey = getSessionKey(exchange);
eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));
}).when(bodyAs(String.class).startsWith(SharedEvent.MESSAGE_PREFIX)).convertBodyTo(SharedEvent.class).process(exchange -> {
SharedEvent event = exchange.getIn().getBody(SharedEvent.class);
// If there is no timestamp in event, set to system time
if (event.getTimestamp() <= 0) {
event.setTimestamp(timerService.getCurrentTimeMillis());
}
}).to(ClientEventService.CLIENT_EVENT_TOPIC).otherwise().process(exchange -> LOG.fine("Unsupported message body: " + exchange.getIn().getBody())).end();
from(ClientEventService.CLIENT_EVENT_QUEUE).routeId("ToClientWebsocketEvents").choice().when(body().isInstanceOf(SharedEvent.class)).split(method(eventSubscriptions, "splitForSubscribers")).to("websocket://" + WEBSOCKET_EVENTS).end();
}
});
}
Aggregations