Search in sources :

Example 1 with GatewayService

use of org.openremote.manager.gateway.GatewayService in project openremote by openremote.

the class AssetStorageService method init.

@Override
public void init(Container container) throws Exception {
    timerService = container.getService(TimerService.class);
    persistenceService = container.getService(PersistenceService.class);
    identityService = container.getService(ManagerIdentityService.class);
    clientEventService = container.getService(ClientEventService.class);
    gatewayService = container.getService(GatewayService.class);
    EventSubscriptionAuthorizer assetEventAuthorizer = AssetStorageService.assetInfoAuthorizer(identityService, this);
    clientEventService.addSubscriptionAuthorizer((realm, auth, subscription) -> {
        if (!subscription.isEventType(AssetEvent.class)) {
            return false;
        }
        return assetEventAuthorizer.authorise(realm, auth, subscription);
    });
    container.getService(ManagerWebService.class).addApiSingleton(new AssetResourceImpl(container.getService(TimerService.class), identityService, this, container.getService(MessageBrokerService.class)));
    container.getService(ManagerWebService.class).addApiSingleton(new ConsoleResourceImpl(container.getService(TimerService.class), identityService, this, clientEventService));
    container.getService(MessageBrokerService.class).getContext().addRoutes(this);
}
Also used : PersistenceService(org.openremote.container.persistence.PersistenceService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) EventSubscriptionAuthorizer(org.openremote.manager.event.EventSubscriptionAuthorizer) ManagerWebService(org.openremote.manager.web.ManagerWebService) ConsoleResourceImpl(org.openremote.manager.asset.console.ConsoleResourceImpl) ClientEventService(org.openremote.manager.event.ClientEventService) TimerService(org.openremote.container.timer.TimerService) GatewayService(org.openremote.manager.gateway.GatewayService)

Example 2 with GatewayService

use of org.openremote.manager.gateway.GatewayService 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);
    gatewayService = container.getService(GatewayService.class);
    eventSubscriptions = new EventSubscriptions(container.getService(TimerService.class));
    messageBrokerService.getContext().getTypeConverterRegistry().addTypeConverters(new EventTypeConverters());
    // TODO: Remove prefix and just use event type then use a subscription wrapper to pass subscription ID around
    messageBrokerService.getContext().addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("websocket://" + WEBSOCKET_EVENTS).routeId("FromClientWebsocketEvents").process(exchange -> exchange.getIn().setHeader(HEADER_CONNECTION_TYPE, HEADER_CONNECTION_TYPE_WEBSOCKET)).to(ClientEventService.CLIENT_EVENT_QUEUE).end();
            from(ClientEventService.CLIENT_EVENT_QUEUE).routeId("ClientEvents").choice().when(header(ConnectionConstants.SESSION_OPEN)).process(exchange -> {
                String sessionKey = getSessionKey(exchange);
                sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));
                passToInterceptors(exchange);
            }).stop().when(or(header(ConnectionConstants.SESSION_CLOSE), header(ConnectionConstants.SESSION_CLOSE_ERROR))).process(exchange -> {
                String sessionKey = getSessionKey(exchange);
                sessionKeyInfoMap.remove(sessionKey);
                eventSubscriptions.cancelAll(sessionKey);
                passToInterceptors(exchange);
            }).stop().end().process(exchange -> {
                // Do basic formatting of exchange
                EventRequestResponseWrapper<?> requestResponse = null;
                if (exchange.getIn().getBody() instanceof EventRequestResponseWrapper) {
                    requestResponse = exchange.getIn().getBody(EventRequestResponseWrapper.class);
                } else if (exchange.getIn().getBody() instanceof String && exchange.getIn().getBody(String.class).startsWith(EventRequestResponseWrapper.MESSAGE_PREFIX)) {
                    requestResponse = exchange.getIn().getBody(EventRequestResponseWrapper.class);
                }
                if (requestResponse != null) {
                    SharedEvent event = requestResponse.getEvent();
                    exchange.getIn().setHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, requestResponse.getMessageId());
                    exchange.getIn().setBody(event);
                }
                if (exchange.getIn().getBody() instanceof String) {
                    String bodyStr = exchange.getIn().getBody(String.class);
                    if (bodyStr.startsWith(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX)) {
                        exchange.getIn().setBody(exchange.getIn().getBody(EventSubscription.class));
                    } else if (bodyStr.startsWith(CancelEventSubscription.MESSAGE_PREFIX)) {
                        exchange.getIn().setBody(exchange.getIn().getBody(CancelEventSubscription.class));
                    } else if (bodyStr.startsWith(SharedEvent.MESSAGE_PREFIX)) {
                        exchange.getIn().setBody(exchange.getIn().getBody(SharedEvent.class));
                    }
                }
                if (exchange.getIn().getBody() instanceof SharedEvent) {
                    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());
                    }
                }
            }).process(exchange -> passToInterceptors(exchange)).choice().when(body().isInstanceOf(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);
                boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext);
                boolean anonymousUser = authContext == null;
                String username = authContext == null ? "anonymous" : authContext.getUsername();
                String realm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);
                if (authorizeEventSubscription(realm, authContext, subscription)) {
                    eventSubscriptions.createOrUpdate(sessionKey, restrictedUser, anonymousUser, subscription);
                    subscription.setSubscribed(true);
                    sendToSession(sessionKey, subscription);
                } else {
                    LOG.warning("Unauthorized subscription from '" + username + "' in realm '" + realm + "': " + subscription);
                    sendToSession(sessionKey, new UnauthorizedEventSubscription<>(subscription));
                }
            }).stop().when(body().isInstanceOf(CancelEventSubscription.class)).process(exchange -> {
                String sessionKey = getSessionKey(exchange);
                eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));
            }).stop().when(body().isInstanceOf(SharedEvent.class)).choice().when(// Inbound messages from clients
            header(HEADER_CONNECTION_TYPE).isNotNull()).to(ClientEventService.CLIENT_EVENT_TOPIC).stop().when(// Outbound message to clients
            header(HEADER_CONNECTION_TYPE).isNull()).split(method(eventSubscriptions, "splitForSubscribers")).process(exchange -> {
                String sessionKey = getSessionKey(exchange);
                sendToSession(sessionKey, exchange.getIn().getBody());
            }).stop().endChoice().otherwise().process(exchange -> LOG.info("Unsupported message body: " + exchange.getIn().getBody())).end();
        }
    });
    // Add pending internal subscriptions
    if (!pendingInternalSubscriptions.isEmpty()) {
        pendingInternalSubscriptions.forEach(subscription -> eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, false, false, subscription));
    }
    pendingInternalSubscriptions = null;
}
Also used : java.util(java.util) AuthContext(org.openremote.container.security.AuthContext) CloseReason(javax.websocket.CloseReason) Exchange(org.apache.camel.Exchange) Level(java.util.logging.Level) Builder.header(org.apache.camel.builder.Builder.header) ManagerWebService(org.openremote.manager.web.ManagerWebService) MessageBrokerService(org.openremote.container.message.MessageBrokerService) Session(javax.websocket.Session) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) ContainerService(org.openremote.model.ContainerService) SESSION_TERMINATOR(org.openremote.container.web.ConnectionConstants.SESSION_TERMINATOR) Constants(org.openremote.model.Constants) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Logger(java.util.logging.Logger) Container(org.openremote.model.Container) Consumer(java.util.function.Consumer) SyslogEvent(org.openremote.model.syslog.SyslogEvent) MqttBrokerService(org.openremote.manager.mqtt.MqttBrokerService) RouteBuilder(org.apache.camel.builder.RouteBuilder) TimerService(org.openremote.container.timer.TimerService) org.openremote.model.event.shared(org.openremote.model.event.shared) PredicateBuilder.or(org.apache.camel.builder.PredicateBuilder.or) ConnectionConstants(org.openremote.container.web.ConnectionConstants) GatewayService(org.openremote.manager.gateway.GatewayService) RouteBuilder(org.apache.camel.builder.RouteBuilder) AuthContext(org.openremote.container.security.AuthContext) TimerService(org.openremote.container.timer.TimerService) GatewayService(org.openremote.manager.gateway.GatewayService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) MessageBrokerService(org.openremote.container.message.MessageBrokerService)

Example 3 with GatewayService

use of org.openremote.manager.gateway.GatewayService in project openremote by openremote.

the class RulesService method init.

@Override
public void init(Container container) throws Exception {
    executorService = container.getExecutorService();
    timerService = container.getService(TimerService.class);
    persistenceService = container.getService(PersistenceService.class);
    rulesetStorageService = container.getService(RulesetStorageService.class);
    identityService = container.getService(ManagerIdentityService.class);
    notificationService = container.getService(NotificationService.class);
    assetStorageService = container.getService(AssetStorageService.class);
    assetProcessingService = container.getService(AssetProcessingService.class);
    assetDatapointService = container.getService(AssetDatapointService.class);
    assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class);
    clientEventService = container.getService(ClientEventService.class);
    gatewayService = container.getService(GatewayService.class);
    if (initDone) {
        return;
    }
    clientEventService.addSubscriptionAuthorizer((realm, auth, subscription) -> {
        if (subscription.isEventType(RulesEngineStatusEvent.class) || subscription.isEventType(RulesetChangedEvent.class)) {
            if (auth == null) {
                return false;
            }
            if (auth.isSuperUser()) {
                return true;
            }
            // Regular user must have role
            if (!auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), auth.getClientId())) {
                return false;
            }
            boolean isRestrictedUser = identityService.getIdentityProvider().isRestrictedUser(auth);
            return !isRestrictedUser;
        }
        return false;
    });
    ServiceLoader.load(GeofenceAssetAdapter.class).forEach(geofenceAssetAdapter -> {
        LOG.fine("Adding GeofenceAssetAdapter: " + geofenceAssetAdapter.getClass().getName());
        geofenceAssetAdapters.add(geofenceAssetAdapter);
    });
    geofenceAssetAdapters.addAll(container.getServices(GeofenceAssetAdapter.class));
    geofenceAssetAdapters.sort(Comparator.comparingInt(GeofenceAssetAdapter::getPriority));
    container.getService(MessageBrokerService.class).getContext().addRoutes(this);
    configEventExpires = getString(container.getConfig(), RULE_EVENT_EXPIRES, RULE_EVENT_EXPIRES_DEFAULT);
    container.getService(ManagerWebService.class).addApiSingleton(new FlowResourceImpl(container.getService(TimerService.class), container.getService(ManagerIdentityService.class)));
    initDone = true;
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) AssetPredictedDatapointService(org.openremote.manager.datapoint.AssetPredictedDatapointService) AssetProcessingService(org.openremote.manager.asset.AssetProcessingService) GeofenceAssetAdapter(org.openremote.manager.rules.geofence.GeofenceAssetAdapter) NotificationService(org.openremote.manager.notification.NotificationService) TimerService(org.openremote.container.timer.TimerService) GatewayService(org.openremote.manager.gateway.GatewayService) PersistenceService(org.openremote.container.persistence.PersistenceService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) AssetDatapointService(org.openremote.manager.datapoint.AssetDatapointService) ManagerWebService(org.openremote.manager.web.ManagerWebService) ClientEventService(org.openremote.manager.event.ClientEventService) FlowResourceImpl(org.openremote.manager.rules.flow.FlowResourceImpl)

Aggregations

TimerService (org.openremote.container.timer.TimerService)3 GatewayService (org.openremote.manager.gateway.GatewayService)3 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)3 ManagerWebService (org.openremote.manager.web.ManagerWebService)3 PersistenceService (org.openremote.container.persistence.PersistenceService)2 ClientEventService (org.openremote.manager.event.ClientEventService)2 java.util (java.util)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Consumer (java.util.function.Consumer)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 CloseReason (javax.websocket.CloseReason)1 Session (javax.websocket.Session)1 Exchange (org.apache.camel.Exchange)1 Builder.header (org.apache.camel.builder.Builder.header)1 PredicateBuilder.or (org.apache.camel.builder.PredicateBuilder.or)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 MessageBrokerService (org.openremote.container.message.MessageBrokerService)1 AuthContext (org.openremote.container.security.AuthContext)1 ConnectionConstants (org.openremote.container.web.ConnectionConstants)1