Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbeaver/pro#3739 event registry #3159

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.jkiss.dbeaver.model.navigator.DBNModel;
import org.jkiss.dbeaver.model.rm.RMProject;
import org.jkiss.dbeaver.model.rm.RMUtils;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceEvent;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.runtime.jobs.DisconnectJob;

Expand Down Expand Up @@ -189,9 +189,9 @@ public List<WebConnectionInfo> getConnections() {
* updates data sources based on event in web session
*
* @param dataSourceIds list of updated connections
* @param type type of event
* @param eventId id of event
*/
public synchronized boolean updateProjectDataSources(@NotNull List<String> dataSourceIds, @NotNull WSEventType type) {
public synchronized boolean updateProjectDataSources(@NotNull List<String> dataSourceIds, @NotNull String eventId) {
var sendDataSourceUpdatedEvent = false;
DBPDataSourceRegistry registry = getDataSourceRegistry();
// save old connections
Expand All @@ -202,22 +202,22 @@ public synchronized boolean updateProjectDataSources(@NotNull List<String> dataS
DBPDataSourceContainer::getId,
Function.identity())
);
if (type == WSEventType.DATASOURCE_CREATED || type == WSEventType.DATASOURCE_UPDATED) {
if (WSDataSourceEvent.CREATED.equals(eventId) || WSDataSourceEvent.UPDATED.equals(eventId)) {
registry.refreshConfig(dataSourceIds);
}
for (String dsId : dataSourceIds) {
DataSourceDescriptor ds = (DataSourceDescriptor) registry.getDataSource(dsId);
if (ds == null) {
continue;
}
switch (type) {
case DATASOURCE_CREATED -> {
switch (eventId) {
case WSDataSourceEvent.CREATED -> {
addConnection(ds);
sendDataSourceUpdatedEvent = true;
}
case DATASOURCE_UPDATED -> // if settings were changed we need to send event
case WSDataSourceEvent.UPDATED -> // if settings were changed we need to send event
sendDataSourceUpdatedEvent |= !ds.equalSettings(oldDataSources.get(dsId));
case DATASOURCE_DELETED -> {
case WSDataSourceEvent.DELETED -> {
WebDataSourceUtils.disconnectDataSource(webSession, ds);
if (registry instanceof DBPDataSourceRegistryCache dsrc) {
dsrc.removeDataSourceFromList(ds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.jkiss.dbeaver.model.security.SMObjectType;
import org.jkiss.dbeaver.model.sql.DBQuotaException;
import org.jkiss.dbeaver.model.websocket.event.MessageType;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.WSSessionLogUpdatedEvent;
import org.jkiss.dbeaver.registry.ResourceTypeDescriptor;
import org.jkiss.dbeaver.registry.ResourceTypeRegistry;
Expand Down Expand Up @@ -777,7 +776,6 @@ protected <T> T doProjectOperation(String projectId, RMFileOperation<T> operatio
if (credentialsProvider.getActiveUserCredentials() != null) {
ServletAppUtils.getServletApplication().getEventController().addEvent(
new WSSessionLogUpdatedEvent(
WSEventType.SESSION_LOG_UPDATED,
credentialsProvider.getActiveUserCredentials().getSmSessionId(),
credentialsProvider.getActiveUserCredentials().getUserId(),
MessageType.ERROR,
Expand All @@ -796,7 +794,6 @@ protected <T> T doFileReadOperation(String projectId, Path file, RMFileOperation
if (credentialsProvider.getActiveUserCredentials() != null) {
ServletAppUtils.getServletApplication().getEventController().addEvent(
new WSSessionLogUpdatedEvent(
WSEventType.SESSION_LOG_UPDATED,
credentialsProvider.getActiveUserCredentials().getSmSessionId(),
credentialsProvider.getActiveUserCredentials().getUserId(),
MessageType.ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.jkiss.dbeaver.model.security.SMController;
import org.jkiss.dbeaver.model.sql.DBQuotaException;
import org.jkiss.dbeaver.model.websocket.event.MessageType;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.WSSessionLogUpdatedEvent;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.utils.CommonUtils;
Expand Down Expand Up @@ -602,7 +601,6 @@ public void addSessionMessage(WebServerMessage message) {
sessionMessages.add(message);
}
addSessionEvent(new WSSessionLogUpdatedEvent(
WSEventType.SESSION_LOG_UPDATED,
this.userContext.getSmSessionId(),
this.userContext.getUserId(),
MessageType.ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
import io.cloudbeaver.model.session.BaseWebSession;
import io.cloudbeaver.model.session.WebSession;
import io.cloudbeaver.server.CBApplication;
import io.cloudbeaver.server.CBPlatform;
import io.cloudbeaver.service.security.SMUtils;
import io.cloudbeaver.utils.ServletAppUtils;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.security.SMAdminController;
import org.jkiss.dbeaver.model.security.SMObjectPermissionsGrant;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.WSProjectUpdateEvent;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceEvent;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceProperty;
Expand Down Expand Up @@ -108,7 +106,7 @@ private Consumer<BaseWebSession> getUpdateUserDataSourcesInfoConsumer(
return;
}
boolean isAccessibleNow = project.findWebConnectionInfo(dataSourceId) != null;
if (WSEventType.OBJECT_PERMISSIONS_UPDATED.getEventId().equals(event.getId())) {
if (WSObjectPermissionEvent.UPDATED.equals(event.getId())) {
if (isAccessibleNow || !shouldBeAccessible) {
return;
}
Expand All @@ -122,7 +120,7 @@ private Consumer<BaseWebSession> getUpdateUserDataSourcesInfoConsumer(
WSDataSourceProperty.CONFIGURATION
)
);
} else if (WSEventType.OBJECT_PERMISSIONS_DELETED.getEventId().equals(event.getId())) {
} else if (WSObjectPermissionEvent.DELETED.equals(event.getId())) {
if (!isAccessibleNow || shouldBeAccessible) {
return;
}
Expand All @@ -147,7 +145,7 @@ private Consumer<BaseWebSession> getUpdateUserProjectsInfoConsumer(
) {
return (activeUserSession) -> {
try {
if (WSEventType.OBJECT_PERMISSIONS_UPDATED.getEventId().equals(event.getId())) {
if (WSObjectPermissionEvent.UPDATED.equals(event.getId())) {
var accessibleProjectIds = activeUserSession.getUserContext().getAccessibleProjectIds();
if (accessibleProjectIds.contains(event.getObjectId())) {
return;
Expand All @@ -160,7 +158,7 @@ private Consumer<BaseWebSession> getUpdateUserProjectsInfoConsumer(
projectId
)
);
} else if (WSEventType.OBJECT_PERMISSIONS_DELETED.getEventId().equals(event.getId())) {
} else if (WSObjectPermissionEvent.DELETED.equals(event.getId())) {
activeUserSession.removeSessionProject(projectId);
activeUserSession.addSessionEvent(
WSProjectUpdateEvent.delete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.rm.RMEvent;
import org.jkiss.dbeaver.model.rm.RMEventManager;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.resource.WSResourceUpdatedEvent;

/**
Expand All @@ -41,22 +40,22 @@ protected void updateSessionData(@NotNull BaseWebSession activeUserSession, @Not
if (activeUserSession instanceof WebSession) {
var webSession = (WebSession) activeUserSession;
acceptChangesInNavigatorTree(
WSEventType.valueById(event.getId()),
event.getId(),
event.getResourcePath(),
webSession.getProjectById(event.getProjectId())
);
}
activeUserSession.addSessionEvent(event);
}

private void acceptChangesInNavigatorTree(WSEventType eventType, String resourcePath, WebProjectImpl project) {
if (eventType == WSEventType.RM_RESOURCE_CREATED) {
private void acceptChangesInNavigatorTree(@NotNull String eventId, String resourcePath, WebProjectImpl project) {
if (WSResourceUpdatedEvent.CREATED.equals(eventId)) {
RMEventManager.fireEvent(
new RMEvent(RMEvent.Action.RESOURCE_ADD,
project.getRMProject(),
resourcePath)
);
} else if (eventType == WSEventType.RM_RESOURCE_DELETED) {
} else if (WSResourceUpdatedEvent.DELETED.equals(eventId)) {
RMEventManager.fireEvent(
new RMEvent(RMEvent.Action.RESOURCE_DELETE,
project.getRMProject(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,18 @@
package io.cloudbeaver.server.events;

import io.cloudbeaver.server.CBApplication;
import io.cloudbeaver.server.CBPlatform;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.websocket.WSEventHandler;
import org.jkiss.dbeaver.model.websocket.event.WSAbstractEvent;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.WSUserCloseSessionsEvent;
import org.jkiss.dbeaver.model.websocket.event.WSUserDeletedEvent;

public class WSUserEventHandler<EVENT extends WSAbstractEvent> implements WSEventHandler<EVENT> {
@Override
public void handleEvent(@NotNull EVENT event) {
var eventType = WSEventType.valueById(event.getId());
if (eventType == null) {
return;
}
var sessionManager = CBApplication.getInstance().getSessionManager();
switch (eventType) {
case CLOSE_USER_SESSIONS:
switch (event.getId()) {
case WSUserCloseSessionsEvent.ID:
if (event instanceof WSUserCloseSessionsEvent closeSessionsEvent) {
if (closeSessionsEvent.getSessionIds().isEmpty()) {
sessionManager.closeAllSessions(closeSessionsEvent.getSessionId());
Expand All @@ -43,7 +37,7 @@ public void handleEvent(@NotNull EVENT event) {
}
}
break;
case USER_DELETED:
case WSUserDeletedEvent.ID:
if (event instanceof WSUserDeletedEvent userDeletedEvent) {
sessionManager.closeUserSession(userDeletedEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.cloudbeaver.model.session.BaseWebSession;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.WSProjectEvent;

/**
Expand All @@ -29,7 +28,6 @@ public abstract class WSAbstractProjectEventHandler<EVENT extends WSProjectEvent
@Override
protected boolean isAcceptableInSession(@NotNull BaseWebSession activeUserSession, @NotNull EVENT event) {
return super.isAcceptableInSession(activeUserSession, event) &&
activeUserSession.isProjectAccessible(event.getProjectId()) &&
WSEventType.valueById(event.getId()) != null;
activeUserSession.isProjectAccessible(event.getProjectId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.cloudbeaver.model.session.WebSession;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceEvent;

/**
Expand All @@ -42,7 +41,7 @@ protected void updateSessionData(@NotNull BaseWebSession activeUserSession, @Not
}
sendEvent = project.updateProjectDataSources(
event.getDataSourceIds(),
WSEventType.valueById(event.getId())
event.getId()
);
}
if (sendEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.model.websocket.event.WSProjectUpdateEvent;

public class WSProjectUpdatedEventHandler extends WSAbstractProjectEventHandler<WSProjectUpdateEvent> {
Expand All @@ -33,10 +32,10 @@ protected void updateSessionData(@NotNull BaseWebSession activeUserSession, @Not
var eventId = event.getId();
var projectId = event.getProjectId();
try {
if (eventId.equals(WSEventType.RM_PROJECT_ADDED.getEventId())) {
if (WSProjectUpdateEvent.ADDED.equals(eventId)) {
activeUserSession.addSessionProject(projectId);
log.info("Project '" + projectId + "' added to '" + activeUserSession.getSessionId() + "' session");
} else if (eventId.equals(WSEventType.RM_PROJECT_REMOVED.getEventId())) {
} else if (WSProjectUpdateEvent.REMOVED.equals(eventId)) {
activeUserSession.removeSessionProject(projectId);
log.info("Project '" + projectId + "' removed from '" + activeUserSession.getSessionId() + "' session");
}
Expand All @@ -49,7 +48,7 @@ protected void updateSessionData(@NotNull BaseWebSession activeUserSession, @Not
@Override
protected boolean isAcceptableInSession(@NotNull BaseWebSession activeUserSession, @NotNull WSProjectUpdateEvent event) {
return !WSWebUtils.isSessionIdEquals(activeUserSession, event.getSessionId()) &&
(event.getId().equals(WSEventType.RM_PROJECT_REMOVED.getEventId()) ||
(event.getId().equals(WSProjectUpdateEvent.REMOVED) ||
activeUserSession.getUserContext().hasPermission(DBWConstants.PERMISSION_ADMIN));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.websocket.event.WSClientEvent;
import org.jkiss.dbeaver.model.websocket.event.WSClientEventType;
import org.jkiss.dbeaver.model.websocket.event.WSEvent;
import org.jkiss.dbeaver.model.websocket.event.client.WSSessionPingClientEvent;
import org.jkiss.dbeaver.model.websocket.event.client.WSSubscribeOnTopicClientEvent;
import org.jkiss.dbeaver.model.websocket.event.client.WSUnsubscribeFromTopicClientEvent;
import org.jkiss.dbeaver.model.websocket.event.client.WSUpdateActiveProjectsClientEvent;
import org.jkiss.dbeaver.model.websocket.event.session.WSAccessTokenExpiredEvent;
import org.jkiss.dbeaver.model.websocket.event.session.WSSocketConnectedEvent;
Expand Down Expand Up @@ -66,33 +68,43 @@ public void onMessage(String message) {
if (CommonUtils.isEmpty(message)) {
return;
}
var clientEvent = CBAbstractWebSocket.gson.fromJson(message, WSClientEvent.class);
var clientEventType = WSClientEventType.valueById(clientEvent.getId());
if (webSession == null) {
log.warn("No web session for browser event");
return;
}
if (clientEventType == null) {
WSClientEvent clientEvent;
try {
clientEvent = CBAbstractWebSocket.gson.fromJson(message, WSClientEvent.class);
} catch (Exception e) {
if (webSession != null) {
webSession.addSessionError(
new DBWebException("Invalid websocket event: " + e.getMessage())
);
}
log.error("Error parsing websocket event: " + e.getMessage(), e);
return;
}
if (clientEvent.getId() == null) {
webSession.addSessionError(
new DBWebException("Invalid websocket event: " + message)
);
return;
}
switch (clientEventType) {
case TOPIC_SUBSCRIBE: {
switch (clientEvent.getId()) {
case WSSubscribeOnTopicClientEvent.ID: {
webSession.getEventsFilter().subscribeOnEventTopic(clientEvent.getTopicId());
break;
}
case TOPIC_UNSUBSCRIBE: {
case WSUnsubscribeFromTopicClientEvent.ID: {
webSession.getEventsFilter().unsubscribeFromEventTopic(clientEvent.getTopicId());
break;
}
case ACTIVE_PROJECTS: {
case WSUpdateActiveProjectsClientEvent.ID: {
var projectEvent = (WSUpdateActiveProjectsClientEvent) clientEvent;
webSession.getEventsFilter().setSubscribedProjects(projectEvent.getProjectIds());
break;
}
case SESSION_PING: {
case WSSessionPingClientEvent.ID: {
if (webSession instanceof WebSession session) {
session.updateInfo(true);
}
Expand Down
Loading