From 67472fc22284011b6efe3e8573b53a57481883b5 Mon Sep 17 00:00:00 2001 From: junjun Date: Mon, 22 Jul 2024 10:33:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/provider/CalciteProvider.java | 50 +------------------ .../datasource/provider/Provider.java | 49 +++++++++++++++++- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java b/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java index 1034a7d203..9266724765 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/provider/CalciteProvider.java @@ -1,9 +1,6 @@ package io.dataease.datasource.provider; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.google.gson.Gson; -import com.jcraft.jsch.JSch; -import com.jcraft.jsch.Session; import io.dataease.commons.utils.CommonThreadPool; import io.dataease.dataset.utils.FieldUtils; import io.dataease.datasource.dao.auto.entity.CoreDatasource; @@ -38,7 +35,6 @@ import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.sql.*; import java.util.*; import java.util.regex.Matcher; @@ -190,7 +186,7 @@ public class CalciteProvider extends Provider { } - private List fetchResultField(ResultSet rs ) throws Exception { + private List fetchResultField(ResultSet rs) throws Exception { List fieldList = new ArrayList<>(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); @@ -338,50 +334,6 @@ public class CalciteProvider extends Provider { return connectionObj; } - private void startSshSession(DatasourceConfiguration configuration, ConnectionObj connectionObj, Long datacourseId) throws Exception { - if (configuration.isUseSSH()) { - if (datacourseId == null) { - configuration.setLPort(getLport(null)); - connectionObj.setLPort(configuration.getLPort()); - connectionObj.setConfiguration(configuration); - Session session = initSession(configuration); - connectionObj.setSession(session); - } else { - Integer lport = Provider.getLPorts().get(datacourseId); - configuration.setLPort(lport); - if (lport != null) { - if (Provider.getSessions().get(datacourseId) == null || !Provider.getSessions().get(datacourseId).isConnected()) { - Session session = initSession(configuration); - Provider.getSessions().put(datacourseId, session); - } - } else { - configuration.setLPort(getLport(datacourseId)); - Session session = initSession(configuration); - Provider.getSessions().put(datacourseId, session); - } - configuration.setLPort(lport); - } - } - } - - private Session initSession(DatasourceConfiguration configuration) throws Exception { - JSch jsch = new JSch(); - Session session = jsch.getSession(configuration.getSshUserName(), configuration.getSshHost(), configuration.getSshPort()); - if (!configuration.getSshType().equalsIgnoreCase("password")) { - session.setConfig("PreferredAuthentications", "publickey"); - jsch.addIdentity("sshkey", configuration.getSshKey().getBytes(StandardCharsets.UTF_8), null, configuration.getSshKeyPassword() == null ? null : configuration.getSshKeyPassword().getBytes(StandardCharsets.UTF_8)); - } - if (configuration.getSshType().equalsIgnoreCase("password")) { - session.setPassword(configuration.getSshPassword()); - } - session.setConfig("StrictHostKeyChecking", "no"); - session.connect(); - session.setPortForwardingL(configuration.getLPort(), configuration.getHost(), configuration.getPort()); - - return session; - } - - private DatasetTableDTO getTableDesc(DatasourceRequest datasourceRequest, ResultSet resultSet) throws SQLException { DatasetTableDTO tableDesc = new DatasetTableDTO(); tableDesc.setDatasourceId(datasourceRequest.getDatasource().getId()); diff --git a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java index 321d0b94ab..352716a04b 100644 --- a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java +++ b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/provider/Provider.java @@ -1,5 +1,6 @@ package io.dataease.extensions.datasource.provider; +import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import io.dataease.exception.DEException; import io.dataease.extensions.datasource.constant.SqlPlaceholderConstants; @@ -15,10 +16,11 @@ import org.apache.calcite.sql.parser.SqlParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.Connection; -import java.sql.Statement; import java.io.IOException; import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.sql.Connection; +import java.sql.Statement; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -191,4 +193,47 @@ public abstract class Provider { return true; } } + + public void startSshSession(DatasourceConfiguration configuration, ConnectionObj connectionObj, Long datacourseId) throws Exception { + if (configuration.isUseSSH()) { + if (datacourseId == null) { + configuration.setLPort(getLport(null)); + connectionObj.setLPort(configuration.getLPort()); + connectionObj.setConfiguration(configuration); + Session session = initSession(configuration); + connectionObj.setSession(session); + } else { + Integer lport = Provider.getLPorts().get(datacourseId); + configuration.setLPort(lport); + if (lport != null) { + if (Provider.getSessions().get(datacourseId) == null || !Provider.getSessions().get(datacourseId).isConnected()) { + Session session = initSession(configuration); + Provider.getSessions().put(datacourseId, session); + } + } else { + configuration.setLPort(getLport(datacourseId)); + Session session = initSession(configuration); + Provider.getSessions().put(datacourseId, session); + } + configuration.setLPort(lport); + } + } + } + + public Session initSession(DatasourceConfiguration configuration) throws Exception { + JSch jsch = new JSch(); + Session session = jsch.getSession(configuration.getSshUserName(), configuration.getSshHost(), configuration.getSshPort()); + if (!configuration.getSshType().equalsIgnoreCase("password")) { + session.setConfig("PreferredAuthentications", "publickey"); + jsch.addIdentity("sshkey", configuration.getSshKey().getBytes(StandardCharsets.UTF_8), null, configuration.getSshKeyPassword() == null ? null : configuration.getSshKeyPassword().getBytes(StandardCharsets.UTF_8)); + } + if (configuration.getSshType().equalsIgnoreCase("password")) { + session.setPassword(configuration.getSshPassword()); + } + session.setConfig("StrictHostKeyChecking", "no"); + session.connect(); + session.setPortForwardingL(configuration.getLPort(), configuration.getHost(), configuration.getPort()); + + return session; + } }