From d3be30f55b594b7a382d4e7fc7f597d86ca3927e Mon Sep 17 00:00:00 2001 From: Ainur <59531286+yagudin10@users.noreply.github.com> Date: Tue, 29 Oct 2024 18:12:10 +0200 Subject: [PATCH] CB-5623 fix npe (#3030) --- .../src/io/cloudbeaver/model/fs/FSUtils.java | 2 +- .../cloudbeaver/model/session/WebSession.java | 8 ++++++ .../src/io/cloudbeaver/WebServiceUtils.java | 4 +-- .../WSFolderUpdatedEventHandlerImpl.java | 11 +++++--- .../service/core/impl/WebServiceCore.java | 2 +- .../navigator/impl/WebServiceNavigator.java | 26 +++++++++---------- .../service/sql/WebSQLProcessor.java | 2 +- .../service/sql/impl/WebServiceSQL.java | 4 ++- .../service/fs/impl/WebServiceFS.java | 4 +-- .../metadata/WebServiceBindingMetadata.java | 5 +++- 10 files changed, 43 insertions(+), 25 deletions(-) diff --git a/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/fs/FSUtils.java b/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/fs/FSUtils.java index e223ba8df4..ca64c38f36 100644 --- a/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/fs/FSUtils.java +++ b/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/fs/FSUtils.java @@ -48,7 +48,7 @@ public static Path getPathFromNode(@NotNull WebSession webSession, @NotNull Stri public static DBNPathBase getNodeByPath(@NotNull WebSession webSession, @NotNull String nodePath) throws DBException { DBRProgressMonitor monitor = webSession.getProgressMonitor(); - DBNModel navigatorModel = webSession.getNavigatorModel(); + DBNModel navigatorModel = webSession.getNavigatorModelOrThrow(); DBNNode node = navigatorModel.getNodeByPath(monitor, nodePath); if (!(node instanceof DBNPathBase dbnPath)) { throw new DBWebException("Node '" + nodePath + "' is not found in File Systems"); diff --git a/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/session/WebSession.java b/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/session/WebSession.java index ce10a80553..d4ee22fbc7 100644 --- a/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/session/WebSession.java +++ b/server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/model/session/WebSession.java @@ -399,10 +399,18 @@ public void setLocale(@Nullable String locale) { this.locale = locale != null ? locale : Locale.getDefault().getLanguage(); } + @Nullable public DBNModel getNavigatorModel() { return navigatorModel; } + @NotNull + public DBNModel getNavigatorModelOrThrow() throws DBWebException { + if (navigatorModel != null) { + return navigatorModel; + } + throw new DBWebException("Navigator model is not found in session"); + } /** * Returns and clears progress messages */ diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/WebServiceUtils.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/WebServiceUtils.java index 46ff6fa6bb..2d44b8ccde 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/WebServiceUtils.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/WebServiceUtils.java @@ -335,8 +335,8 @@ public static String getConnectionContainerInfo(DBPDataSourceContainer container return container.getName() + " [" + container.getId() + "]"; } - public static void updateConfigAndRefreshDatabases(WebSession session, String projectId) { - DBNProject projectNode = session.getNavigatorModel().getRoot().getProjectNode(session.getProjectById(projectId)); + public static void updateConfigAndRefreshDatabases(WebSession session, String projectId) throws DBWebException { + DBNProject projectNode = session.getNavigatorModelOrThrow().getRoot().getProjectNode(session.getProjectById(projectId)); DBNModel.updateConfigAndRefreshDatabases(projectNode.getDatabases()); } diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/events/WSFolderUpdatedEventHandlerImpl.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/events/WSFolderUpdatedEventHandlerImpl.java index 974a16bdb3..7806e116b2 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/events/WSFolderUpdatedEventHandlerImpl.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/events/WSFolderUpdatedEventHandlerImpl.java @@ -20,6 +20,7 @@ import io.cloudbeaver.model.session.WebSession; import org.jkiss.code.NotNull; import org.jkiss.dbeaver.Log; +import org.jkiss.dbeaver.model.navigator.DBNModel; import org.jkiss.dbeaver.model.websocket.event.datasource.WSDatasourceFolderEvent; import org.jkiss.utils.CommonUtils; @@ -32,15 +33,19 @@ public class WSFolderUpdatedEventHandlerImpl extends WSAbstractProjectEventHandl @Override protected void updateSessionData(@NotNull BaseWebSession activeUserSession, @NotNull WSDatasourceFolderEvent event) { - if (activeUserSession instanceof WebSession) { - var webSession = (WebSession) activeUserSession; + if (activeUserSession instanceof WebSession webSession) { var project = webSession.getProjectById(event.getProjectId()); if (project == null) { log.debug("Project " + event.getProjectId() + " is not found in session " + webSession.getSessionId()); return; } project.getDataSourceRegistry().refreshConfig(); - webSession.getNavigatorModel().getRoot().getProjectNode(project).getDatabases().refreshChildren(); + DBNModel navigatorModel = webSession.getNavigatorModel(); + if (navigatorModel == null) { + log.debug("Navigator model is not found in session " + webSession.getSessionId()); + return; + } + navigatorModel.getRoot().getProjectNode(project).getDatabases().refreshChildren(); } activeUserSession.addSessionEvent(event); } diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java index 77a5a40fc7..bb9617728e 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java @@ -639,7 +639,7 @@ public WebConnectionInfo copyConnectionFromNode( @NotNull WebConnectionConfig config ) throws DBWebException { try { - DBNModel navigatorModel = webSession.getNavigatorModel(); + DBNModel navigatorModel = webSession.getNavigatorModelOrThrow(); WebSessionProjectImpl project = getProjectById(webSession, projectId); DBPDataSourceRegistry dataSourceRegistry = project.getDataSourceRegistry(); diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/navigator/impl/WebServiceNavigator.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/navigator/impl/WebServiceNavigator.java index c22861f44c..cce3fd8430 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/navigator/impl/WebServiceNavigator.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/navigator/impl/WebServiceNavigator.java @@ -83,7 +83,7 @@ public List getNavigatorNodeChildren( DBNNode[] nodeChildren; boolean isRootPath = CommonUtils.isEmpty(parentPath) || "/".equals(parentPath) || ROOT_DATABASES.equals(parentPath); - DBNModel navigatorModel = session.getNavigatorModel(); + DBNModel navigatorModel = session.getNavigatorModelOrThrow(); Set applicableDrivers = WebServiceUtils.getApplicableDriversIds(); if (isRootPath) { DBNRoot rootNode = navigatorModel.getRoot(); @@ -155,8 +155,7 @@ public List getNavigatorNodeParents( try { DBRProgressMonitor monitor = session.getProgressMonitor(); - DBNModel navigatorModel = session.getNavigatorModel(); - DBNNode node = navigatorModel.getNodeByPath(monitor, nodePath); + DBNNode node = session.getNavigatorModelOrThrow().getNodeByPath(monitor, nodePath); if (node == null) { throw new DBWebException("Node '" + nodePath + "' not found"); } @@ -198,7 +197,7 @@ public WebNavigatorNodeInfo getNavigatorNodeInfo( try { DBRProgressMonitor monitor = session.getProgressMonitor(); - DBNNode node = session.getNavigatorModel().getNodeByPath(monitor, nodePath); + DBNNode node = session.getNavigatorModelOrThrow().getNodeByPath(monitor, nodePath); if (node == null) { throw new DBWebException("Navigator node '" + nodePath + "' not found"); } @@ -217,7 +216,7 @@ public boolean setNavigatorNodeFilter( try { DBRProgressMonitor monitor = webSession.getProgressMonitor(); - DBNNode node = webSession.getNavigatorModel().getNodeByPath(monitor, nodePath); + DBNNode node = webSession.getNavigatorModelOrThrow().getNodeByPath(monitor, nodePath); if (node == null) { throw new DBWebException("Navigator node '" + nodePath + "' not found"); } @@ -256,7 +255,7 @@ public boolean refreshNavigatorNode( try { DBRProgressMonitor monitor = session.getProgressMonitor(); - DBNNode node = session.getNavigatorModel().getNodeByPath(monitor, nodePath); + DBNNode node = session.getNavigatorModelOrThrow().getNodeByPath(monitor, nodePath); if (node == null) { throw new DBWebException("Navigator node '" + nodePath + "' not found"); } @@ -380,10 +379,9 @@ protected List getCatalogs(DBRProgressMonitor monitor, DBSO } @Nullable - protected WebNavigatorNodeInfo getNodeFromObject(WebSession session, DBSObject object){ - DBNModel navigatorModel = session.getNavigatorModel(); + protected WebNavigatorNodeInfo getNodeFromObject(WebSession session, DBSObject object) throws DBWebException { DBRProgressMonitor monitor = session.getProgressMonitor(); - DBNNode node = navigatorModel.getNodeByObject(monitor, object, false); + DBNNode node = session.getNavigatorModelOrThrow().getNodeByObject(monitor, object, false); return node == null ? null : new WebNavigatorNodeInfo(session, node); } @@ -397,7 +395,7 @@ public String renameNode( try { DBRProgressMonitor monitor = session.getProgressMonitor(); - DBNNode node = session.getNavigatorModel().getNodeByPath(monitor, nodePath); + DBNNode node = session.getNavigatorModelOrThrow().getNodeByPath(monitor, nodePath); if (node == null) { throw new DBWebException("Navigator node '" + nodePath + "' not found"); } @@ -502,8 +500,9 @@ public int deleteNodes( String projectId = null; boolean containsFolderNodes = false; Map nodes = new LinkedHashMap<>(); + DBNModel model = session.getNavigatorModelOrThrow(); for (String path : nodePaths) { - DBNNode node = session.getNavigatorModel().getNodeByPath(monitor, path); + DBNNode node = model.getNodeByPath(monitor, path); if (node == null) { throw new DBWebException("Navigator node '" + path + "' not found"); } @@ -598,9 +597,10 @@ public boolean moveNodesToFolder( try { DBRProgressMonitor monitor = session.getProgressMonitor(); DBNNode folderNode; - folderNode = session.getNavigatorModel().getNodeByPath(monitor, folderNodePath); + DBNModel navigatorModel = session.getNavigatorModelOrThrow(); + folderNode = navigatorModel.getNodeByPath(monitor, folderNodePath); for (String path : nodePaths) { - DBNNode node = session.getNavigatorModel().getNodeByPath(monitor, path); + DBNNode node = navigatorModel.getNodeByPath(monitor, path); if (node == null) { throw new DBWebException("Navigator node '" + path + "' not found"); } diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java index a58ab10961..436c67ae4c 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java @@ -950,7 +950,7 @@ private void checkDataEditAllowed(DBSEntity dataContainer) throws DBWebException @NotNull public T getDataContainerByNodePath(DBRProgressMonitor monitor, @NotNull String containerPath, Class type) throws DBException { - DBNNode node = webSession.getNavigatorModel().getNodeByPath(monitor, containerPath); + DBNNode node = webSession.getNavigatorModelOrThrow().getNodeByPath(monitor, containerPath); if (node == null) { throw new DBWebException("Container node '" + containerPath + "' not found"); } diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java index 88fece2598..36ff2f17d7 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java @@ -40,6 +40,7 @@ import org.jkiss.dbeaver.model.exec.trace.DBCTraceDynamic; import org.jkiss.dbeaver.model.exec.trace.DBCTraceProperty; import org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect; +import org.jkiss.dbeaver.model.navigator.DBNModel; import org.jkiss.dbeaver.model.navigator.DBNNode; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.sql.*; @@ -241,8 +242,9 @@ public String generateEntityQuery(@NotNull WebSession session, @NotNull String g private List getObjectListFromNodeIds(@NotNull WebSession session, @NotNull List nodePathList) throws DBWebException { try { List objectList = new ArrayList<>(nodePathList.size()); + DBNModel navigatorModel = session.getNavigatorModelOrThrow(); for (String nodePath : nodePathList) { - DBNNode node = session.getNavigatorModel().getNodeByPath(session.getProgressMonitor(), nodePath); + DBNNode node = navigatorModel.getNodeByPath(session.getProgressMonitor(), nodePath); if (node == null) { throw new DBException("Node '" + nodePath + "' not found"); } diff --git a/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java b/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java index f02d3ca6da..187184c780 100644 --- a/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java +++ b/server/bundles/io.cloudbeaver.service.fs/src/io/cloudbeaver/service/fs/impl/WebServiceFS.java @@ -51,7 +51,7 @@ public FSFileSystem[] getAvailableFileSystems(@NotNull WebSession webSession, @N if (project == null) { throw new DBException(MessageFormat.format("Project ''{0}'' is not found in session", projectId)); } - DBNProject projectNode = webSession.getNavigatorModel().getRoot().getProjectNode(project); + DBNProject projectNode = webSession.getNavigatorModelOrThrow().getRoot().getProjectNode(project); if (projectNode == null) { throw new DBException(MessageFormat.format("Project ''{0}'' is not found in navigator model", projectId)); } @@ -78,7 +78,7 @@ public FSFileSystem getFileSystem( @NotNull String nodePath ) throws DBWebException { try { - var node = webSession.getNavigatorModel().getNodeByPath(webSession.getProgressMonitor(), nodePath); + var node = webSession.getNavigatorModelOrThrow().getNodeByPath(webSession.getProgressMonitor(), nodePath); if (!(node instanceof DBNFileSystem fs)) { throw new DBException(MessageFormat.format("Node ''{0}'' is not File System", nodePath)); } diff --git a/server/bundles/io.cloudbeaver.service.metadata/src/io/cloudbeaver/service/metadata/WebServiceBindingMetadata.java b/server/bundles/io.cloudbeaver.service.metadata/src/io/cloudbeaver/service/metadata/WebServiceBindingMetadata.java index 9a4221fff6..d135f77666 100644 --- a/server/bundles/io.cloudbeaver.service.metadata/src/io/cloudbeaver/service/metadata/WebServiceBindingMetadata.java +++ b/server/bundles/io.cloudbeaver.service.metadata/src/io/cloudbeaver/service/metadata/WebServiceBindingMetadata.java @@ -52,6 +52,9 @@ public void bindWiring(DBWBindingContext model) throws DBWebException { private DBNNode getNodeFromPath(DataFetchingEnvironment env) throws DBException { WebSession webSession = getWebSession(env); String nodePath = env.getArgument("nodeId"); - return webSession.getNavigatorModel().getNodeByPath(webSession.getProgressMonitor(), nodePath); + if (nodePath == null) { + throw new DBException("Node path is null"); + } + return webSession.getNavigatorModelOrThrow().getNodeByPath(webSession.getProgressMonitor(), nodePath); } }