Search in sources :

Example 1 with MessageBrokerSetupService

use of org.openremote.container.message.MessageBrokerSetupService 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();
}
Also used : MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) AssetStorageService(org.openremote.manager.asset.AssetStorageService) ConsoleAppService(org.openremote.manager.apps.ConsoleAppService) ArrayList(java.util.ArrayList) AssetProcessingService(org.openremote.manager.asset.AssetProcessingService) TimerService(org.openremote.container.timer.TimerService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) AssetDatapointService(org.openremote.manager.datapoint.AssetDatapointService) Container(org.openremote.container.Container) AgentService(org.openremote.manager.agent.AgentService) RulesService(org.openremote.manager.rules.RulesService) ManagerWebService(org.openremote.manager.web.ManagerWebService) SimulatorService(org.openremote.manager.simulator.SimulatorService) ClientEventService(org.openremote.manager.event.ClientEventService) Protocol(org.openremote.agent.protocol.Protocol) MapService(org.openremote.manager.map.MapService) RulesetStorageService(org.openremote.manager.rules.RulesetStorageService) I18NService(org.openremote.manager.i18n.I18NService) ManagerPersistenceService(org.openremote.manager.persistence.ManagerPersistenceService) NotificationService(org.openremote.manager.notification.NotificationService) AssetAttributeLinkingService(org.openremote.manager.asset.AssetAttributeLinkingService) ManagerExecutorService(org.openremote.manager.concurrent.ManagerExecutorService) MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) SetupService(org.openremote.manager.setup.SetupService) ContainerService(org.openremote.container.ContainerService) MessageBrokerService(org.openremote.container.message.MessageBrokerService)

Example 2 with MessageBrokerSetupService

use of org.openremote.container.message.MessageBrokerSetupService 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();
        }
    });
}
Also used : MessageBrokerService(org.openremote.container.message.MessageBrokerService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) AuthContext(org.openremote.container.security.AuthContext) Collection(java.util.Collection) CancelEventSubscription(org.openremote.model.event.shared.CancelEventSubscription) Exchange(org.apache.camel.Exchange) Constants(org.openremote.model.Constants) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Logger(java.util.logging.Logger) MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) Container(org.openremote.container.Container) WebsocketConstants(org.openremote.container.web.socket.WebsocketConstants) SyslogEvent(org.openremote.model.syslog.SyslogEvent) RouteBuilder(org.apache.camel.builder.RouteBuilder) ContainerService(org.openremote.container.ContainerService) TimerService(org.openremote.container.timer.TimerService) UnauthorizedEventSubscription(org.openremote.model.event.shared.UnauthorizedEventSubscription) PredicateBuilder.or(org.apache.camel.builder.PredicateBuilder.or) ManagerExecutorService(org.openremote.manager.concurrent.ManagerExecutorService) EventSubscription(org.openremote.model.event.shared.EventSubscription) SharedEvent(org.openremote.model.event.shared.SharedEvent) Collections(java.util.Collections) CancelEventSubscription(org.openremote.model.event.shared.CancelEventSubscription) MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) RouteBuilder(org.apache.camel.builder.RouteBuilder) AuthContext(org.openremote.container.security.AuthContext) UnauthorizedEventSubscription(org.openremote.model.event.shared.UnauthorizedEventSubscription) TimerService(org.openremote.container.timer.TimerService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) CancelEventSubscription(org.openremote.model.event.shared.CancelEventSubscription) UnauthorizedEventSubscription(org.openremote.model.event.shared.UnauthorizedEventSubscription) EventSubscription(org.openremote.model.event.shared.EventSubscription) SharedEvent(org.openremote.model.event.shared.SharedEvent) MessageBrokerService(org.openremote.container.message.MessageBrokerService)

Aggregations

Container (org.openremote.container.Container)2 ContainerService (org.openremote.container.ContainerService)2 MessageBrokerService (org.openremote.container.message.MessageBrokerService)2 MessageBrokerSetupService (org.openremote.container.message.MessageBrokerSetupService)2 TimerService (org.openremote.container.timer.TimerService)2 ManagerExecutorService (org.openremote.manager.concurrent.ManagerExecutorService)2 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Logger (java.util.logging.Logger)1 Exchange (org.apache.camel.Exchange)1 PredicateBuilder.or (org.apache.camel.builder.PredicateBuilder.or)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 Protocol (org.openremote.agent.protocol.Protocol)1 AuthContext (org.openremote.container.security.AuthContext)1 WebsocketConstants (org.openremote.container.web.socket.WebsocketConstants)1 AgentService (org.openremote.manager.agent.AgentService)1 ConsoleAppService (org.openremote.manager.apps.ConsoleAppService)1