diff --git a/server/bundles/io.cloudbeaver.resources.drivers.base/plugin.xml b/server/bundles/io.cloudbeaver.resources.drivers.base/plugin.xml index 51f1e1d6e4e..996cf868335 100644 --- a/server/bundles/io.cloudbeaver.resources.drivers.base/plugin.xml +++ b/server/bundles/io.cloudbeaver.resources.drivers.base/plugin.xml @@ -76,6 +76,7 @@ + diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/.gitignore b/server/bundles/io.stackblaze.dbeaver.ext.files/.gitignore new file mode 100644 index 00000000000..b83d22266ac --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/META-INF/MANIFEST.MF b/server/bundles/io.stackblaze.dbeaver.ext.files/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..9e03c5c02cd --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/META-INF/MANIFEST.MF @@ -0,0 +1,16 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Stackblaze Volume Files Driver +Bundle-Vendor: Stackblaze +Bundle-SymbolicName: io.stackblaze.dbeaver.ext.files;singleton:=true +Bundle-Version: 1.0.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-21 +Bundle-ActivationPolicy: lazy +Bundle-Localization: OSGI-INF/l10n/bundle +Bundle-ClassPath: . +Require-Bundle: org.jkiss.dbeaver.model;visibility:=reexport, + org.jkiss.dbeaver.model.sql;visibility:=reexport, + org.jkiss.dbeaver.registry;visibility:=reexport +Export-Package: io.stackblaze.dbeaver.ext.files, + io.stackblaze.dbeaver.ext.files.model +Automatic-Module-Name: io.stackblaze.dbeaver.ext.files diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/OSGI-INF/l10n/bundle.properties b/server/bundles/io.stackblaze.dbeaver.ext.files/OSGI-INF/l10n/bundle.properties new file mode 100644 index 00000000000..6f00e650e0e --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/OSGI-INF/l10n/bundle.properties @@ -0,0 +1,5 @@ +meta.io.stackblaze.dbeaver.ext.files.model.FilesFolder.name.name=Name +meta.io.stackblaze.dbeaver.ext.files.model.FilesFolder.path.name=Path +meta.io.stackblaze.dbeaver.ext.files.model.FilesEntry.name.name=Name +meta.io.stackblaze.dbeaver.ext.files.model.FilesEntry.size.name=Size +meta.io.stackblaze.dbeaver.ext.files.model.FilesEntry.path.name=Path diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/README.md b/server/bundles/io.stackblaze.dbeaver.ext.files/README.md new file mode 100644 index 00000000000..5b2ac8cd7bc --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/README.md @@ -0,0 +1,27 @@ +# Stackblaze leftover-volume files driver for CloudBeaver CE + +Native (non-JDBC) datasource so CloudBeaver can browse a leftover PVC +through the HTTP listing sidecar kubero-server mounts on the volume. + +## Driver id + +`files:http` (provider `files`, driver `http`) + +## Tree + +``` +connection +├── wp-content/ (folder) +│ └── uploads/ (folder) +│ └── img.jpg (file — Data tab shows name/size/preview) +└── wp-config.php (file) +``` + +Connects to `http://{host}:{port}` with the connection password as a +Bearer token. Endpoints: `GET /health`, `GET /ls?path=`, `GET /cat?path=`. + +## Build notes + +- No extra JARs — uses `java.net.http.HttpClient`. +- Registered in `io.cloudbeaver.resources.drivers.base` and included in + `io.cloudbeaver.server.feature`. diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/build.properties b/server/bundles/io.stackblaze.dbeaver.ext.files/build.properties new file mode 100644 index 00000000000..3c833df018f --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/build.properties @@ -0,0 +1,6 @@ +source.. = src/ +output.. = target/classes/ +bin.includes = .,\ + META-INF/,\ + OSGI-INF/,\ + plugin.xml diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/plugin.xml b/server/bundles/io.stackblaze.dbeaver.ext.files/plugin.xml new file mode 100644 index 00000000000..36600f3305a --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/plugin.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/pom.xml b/server/bundles/io.stackblaze.dbeaver.ext.files/pom.xml new file mode 100644 index 00000000000..23eade575aa --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + io.cloudbeaver + bundles + 1.0.0-SNAPSHOT + ../ + + io.stackblaze.dbeaver.ext.files + 1.0.0-SNAPSHOT + eclipse-plugin + diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/FilesConstants.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/FilesConstants.java new file mode 100644 index 00000000000..82491f37730 --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/FilesConstants.java @@ -0,0 +1,14 @@ +package io.stackblaze.dbeaver.ext.files; + +public final class FilesConstants { + public static final String DRIVER_ID = "http"; + public static final String PROVIDER_ID = "files"; + public static final String FULL_DRIVER_ID = "files:http"; + public static final int DEFAULT_PORT = 8080; + public static final int PREVIEW_CHARS = 8_192; + /** Hard ceiling for a real (Save-to-file) content fetch — matches the sidecar's own cap. */ + public static final long MAX_DOWNLOAD_BYTES = 100L * 1024 * 1024; + + private FilesConstants() { + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/FilesDataSourceProvider.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/FilesDataSourceProvider.java new file mode 100644 index 00000000000..7468299eeee --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/FilesDataSourceProvider.java @@ -0,0 +1,57 @@ +package io.stackblaze.dbeaver.ext.files; + +import io.stackblaze.dbeaver.ext.files.model.FilesDataSource; +import org.jkiss.code.NotNull; +import org.jkiss.dbeaver.DBException; +import org.jkiss.dbeaver.model.DBPDataSourceContainer; +import org.jkiss.dbeaver.model.DatabaseURL; +import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration; +import org.jkiss.dbeaver.model.connection.DBPDriver; +import org.jkiss.dbeaver.model.impl.AbstractDataSourceProvider; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; +import org.jkiss.utils.CommonUtils; + +/** + * Native (non-JDBC) leftover-volume file browser for CloudBeaver CE. + */ +public class FilesDataSourceProvider extends AbstractDataSourceProvider { + + public FilesDataSourceProvider() { + super(FilesDataSource.class); + } + + @Override + public long getFeatures() { + return FEATURE_CATALOGS; + } + + @Override + public boolean providesDriverClasses(@NotNull DBPDriver driver) { + return false; + } + + @NotNull + @Override + public String getConnectionURL(@NotNull DBPDriver driver, @NotNull DBPConnectionConfiguration connectionInfo) { + String template = driver.getSampleURL(); + if (!CommonUtils.isEmpty(template)) { + try { + return DatabaseURL.generateUrlByTemplate(driver, connectionInfo); + } catch (DBException e) { + // Fall through to the constructed http URL. + } + } + String host = CommonUtils.notEmpty(connectionInfo.getHostName()); + String port = CommonUtils.notEmpty(connectionInfo.getHostPort()); + return "http://" + host + ":" + port; + } + + @NotNull + @Override + public FilesDataSource openDataSource( + @NotNull DBRProgressMonitor monitor, + @NotNull DBPDataSourceContainer container + ) throws DBException { + return new FilesDataSource(monitor, container); + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesClient.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesClient.java new file mode 100644 index 00000000000..aeca186313f --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesClient.java @@ -0,0 +1,197 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.DBException; + +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + +/** Tiny HTTP client for the leftover-volume listing sidecar. */ +public class FilesClient { + + public record Entry(String name, boolean directory, long size, long mtime) { + } + + private final HttpClient http = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(10)) + .build(); + private final String base; + private final String token; + + public FilesClient(@NotNull String host, int port, @Nullable String token) { + this.base = "http://" + host + ":" + port; + this.token = token == null ? "" : token; + } + + public void ping() throws DBException { + request("/health", true); + } + + @NotNull + public List list(@NotNull String path) throws DBException { + String body = request("/cgi-bin/api?op=ls&path=" + enc(path) + tokenParam(), false); + List out = new ArrayList<>(); + int idx = 0; + while (true) { + int nameAt = body.indexOf("\"name\"", idx); + if (nameAt < 0) { + break; + } + String name = jsonString(body, nameAt); + int typeAt = body.indexOf("\"type\"", nameAt); + String type = typeAt >= 0 ? jsonString(body, typeAt) : "file"; + long size = jsonLong(body, body.indexOf("\"size\"", nameAt)); + long mtime = jsonLong(body, body.indexOf("\"mtime\"", nameAt)); + if (name != null && !name.isEmpty()) { + out.add(new Entry(name, "dir".equalsIgnoreCase(type), size, mtime)); + } + idx = nameAt + 6; + } + return out; + } + + /** Small text preview for the grid cell. Server-side cap is chars*4 (UTF-8 worst case); truncated again here. */ + @NotNull + public String cat(@NotNull String path, int maxChars) throws DBException { + long byteCap = (long) maxChars * 4; + String body = request( + "/cgi-bin/api?op=cat&path=" + enc(path) + "&max=" + byteCap + tokenParam(), false); + if (body.length() > maxChars) { + return body.substring(0, maxChars); + } + return body; + } + + /** Full, byte-exact content for view/download — used only once a cell's LOB panel is opened. */ + @NotNull + public byte[] catBytes(@NotNull String path, long maxBytes) throws DBException { + return requestBytes("/cgi-bin/api?op=cat&path=" + enc(path) + "&max=" + maxBytes + tokenParam()); + } + + @NotNull + private String request(@NotNull String pathAndQuery, boolean allowEmpty) throws DBException { + try { + HttpRequest.Builder b = HttpRequest.newBuilder() + .uri(URI.create(base + pathAndQuery)) + .timeout(Duration.ofSeconds(20)) + .GET(); + if (!token.isEmpty()) { + b.header("Authorization", "Bearer " + token); + } + HttpResponse res = http.send(b.build(), HttpResponse.BodyHandlers.ofString()); + if (res.statusCode() >= 400) { + throw new DBException("Volume files HTTP " + res.statusCode() + " for " + pathAndQuery); + } + String body = res.body() == null ? "" : res.body(); + if (!allowEmpty && body.isEmpty()) { + throw new DBException("Empty response from volume files " + pathAndQuery); + } + return body; + } catch (DBException e) { + throw e; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new DBException("Volume files request interrupted", e); + } catch (Exception e) { + throw new DBException("Failed to reach volume files at " + base + ": " + e.getMessage(), e); + } + } + + @NotNull + private byte[] requestBytes(@NotNull String pathAndQuery) throws DBException { + try { + HttpRequest.Builder b = HttpRequest.newBuilder() + .uri(URI.create(base + pathAndQuery)) + .timeout(Duration.ofSeconds(60)) + .GET(); + if (!token.isEmpty()) { + b.header("Authorization", "Bearer " + token); + } + HttpResponse res = http.send(b.build(), HttpResponse.BodyHandlers.ofByteArray()); + if (res.statusCode() >= 400) { + throw new DBException("Volume files HTTP " + res.statusCode() + " for " + pathAndQuery); + } + return res.body() == null ? new byte[0] : res.body(); + } catch (DBException e) { + throw e; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new DBException("Volume files request interrupted", e); + } catch (Exception e) { + throw new DBException("Failed to reach volume files at " + base + ": " + e.getMessage(), e); + } + } + + /** + * Token as a query parameter. busybox httpd reliably passes QUERY_STRING to + * CGI but consumes the Authorization header for its own Basic auth, so the + * sidecar reads the token from ?t=. The Bearer header is still sent (below) + * as a harmless fallback. + */ + @NotNull + private String tokenParam() { + if (token.isEmpty()) { + return ""; + } + return "&t=" + URLEncoder.encode(token, StandardCharsets.UTF_8); + } + + @NotNull + private static String enc(@Nullable String path) { + return URLEncoder.encode(path == null || path.isEmpty() ? "/" : path, StandardCharsets.UTF_8); + } + + @NotNull + private static String jsonString(@NotNull String json, int keyAt) { + if (keyAt < 0) { + return ""; + } + int colon = json.indexOf(':', keyAt); + if (colon < 0) { + return ""; + } + int q1 = json.indexOf('"', colon + 1); + if (q1 < 0) { + return ""; + } + int q2 = json.indexOf('"', q1 + 1); + if (q2 < 0) { + return ""; + } + return json.substring(q1 + 1, q2); + } + + private static long jsonLong(@NotNull String json, int keyAt) { + if (keyAt < 0) { + return 0; + } + int colon = json.indexOf(':', keyAt); + if (colon < 0) { + return 0; + } + int i = colon + 1; + while (i < json.length() && (json.charAt(i) == ' ' || json.charAt(i) == '\n')) { + i++; + } + int j = i; + while (j < json.length() && (Character.isDigit(json.charAt(j)) || json.charAt(j) == '-')) { + j++; + } + if (j == i) { + return 0; + } + try { + return Long.parseLong(json.substring(i, j)); + } catch (NumberFormatException e) { + return 0; + } + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesContentValue.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesContentValue.java new file mode 100644 index 00000000000..f1af35fcf2b --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesContentValue.java @@ -0,0 +1,118 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import io.stackblaze.dbeaver.ext.files.FilesConstants; +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.DBException; +import org.jkiss.dbeaver.model.DBPDataSource; +import org.jkiss.dbeaver.model.data.DBDContent; +import org.jkiss.dbeaver.model.data.DBDContentStorage; +import org.jkiss.dbeaver.model.data.DBDDisplayFormat; +import org.jkiss.dbeaver.model.data.storage.BytesContentStorage; +import org.jkiss.dbeaver.model.exec.DBCException; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; + +/** + * A file's real content, fetched lazily on first open. Gives the grid's LOB + * panel (view/hex/image sub-viewers, Save-to-file) real byte-exact data, + * separate from the small truncated text preview shown inline in the grid. + * Read-only: the engine never writes back to the volume. + */ +public class FilesContentValue implements DBDContent { + + private final FilesDataSource dataSource; + private final String path; + private final long declaredSize; + private DBDContentStorage storage; + + public FilesContentValue(@NotNull FilesDataSource dataSource, @NotNull String path, long declaredSize) { + this.dataSource = dataSource; + this.path = path; + this.declaredSize = declaredSize; + } + + @NotNull + @Override + public DBPDataSource getDataSource() { + return dataSource; + } + + @Override + public long getContentLength() { + return declaredSize; + } + + @NotNull + @Override + public String getContentType() { + return "application/octet-stream"; + } + + @Nullable + @Override + public String getDisplayString(@NotNull DBDDisplayFormat format) { + return "[FILE " + declaredSize + " bytes]"; + } + + @Nullable + @Override + public DBDContentStorage getContents(@NotNull DBRProgressMonitor monitor) throws DBCException { + if (storage == null) { + if (declaredSize > FilesConstants.MAX_DOWNLOAD_BYTES) { + throw new DBCException( + "File is " + declaredSize + " bytes — over the " + + FilesConstants.MAX_DOWNLOAD_BYTES + " byte view/download limit"); + } + try { + byte[] data = dataSource.client().catBytes(path, declaredSize); + storage = new BytesContentStorage(data, "UTF-8"); + } catch (DBException e) { + throw new DBCException("Failed to fetch file content: " + e.getMessage(), e); + } + } + return storage; + } + + @Override + public boolean updateContents( + @NotNull DBRProgressMonitor monitor, + @Nullable DBDContentStorage newStorage + ) { + // Read-only engine: leftover-volume columns are never updatable + // (LocalResultSetColumn.isReadOnly() is always true), so the UI has + // no path to reach this — kept as an explicit no-op, not a stub bug. + return false; + } + + @Override + public void resetContents() { + if (storage != null) { + storage.release(); + storage = null; + } + } + + @Nullable + @Override + public Object getRawValue() { + return this; + } + + @Override + public boolean isNull() { + return false; + } + + @Override + public boolean isModified() { + return false; + } + + @Override + public void release() { + if (storage != null) { + storage.release(); + storage = null; + } + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesDataSource.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesDataSource.java new file mode 100644 index 00000000000..f40287564c3 --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesDataSource.java @@ -0,0 +1,172 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import io.stackblaze.dbeaver.ext.files.FilesConstants; +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.DBException; +import org.jkiss.dbeaver.model.DBPDataSourceContainer; +import org.jkiss.dbeaver.model.DBPExclusiveResource; +import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration; +import org.jkiss.dbeaver.model.exec.DBCExecutionContext; +import org.jkiss.dbeaver.model.impl.AbstractDataSource; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; +import org.jkiss.dbeaver.model.sql.SQLDialect; +import org.jkiss.dbeaver.model.struct.DBSInstance; +import org.jkiss.dbeaver.model.struct.DBSObject; +import org.jkiss.dbeaver.model.struct.DBSObjectContainer; +import org.jkiss.utils.CommonUtils; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Root leftover-volume data source. Children are the volume's top-level files. + */ +public class FilesDataSource extends AbstractDataSource + implements DBSInstance, DBSObjectContainer { + + private final FilesExecutionContext executionContext; + private final FilesExclusiveResource exclusiveLock = new FilesExclusiveResource(); + private final FilesDataSourceInfo info = new FilesDataSourceInfo(); + private FilesClient client; + private FilesFolder root; + private boolean connected; + + public FilesDataSource( + @NotNull DBRProgressMonitor monitor, + @NotNull DBPDataSourceContainer container + ) throws DBException { + super(container); + this.executionContext = new FilesExecutionContext(this, "Main"); + connect(monitor); + } + + private void connect(@NotNull DBRProgressMonitor monitor) throws DBException { + DBPConnectionConfiguration cfg = container.getActualConnectionConfiguration(); + String host = CommonUtils.notEmpty(cfg.getHostName()); + if (host.isEmpty()) { + host = "localhost"; + } + int port = CommonUtils.toInt(cfg.getHostPort(), FilesConstants.DEFAULT_PORT); + String token = cfg.getUserPassword(); + monitor.subTask("Connect to volume files " + host + ":" + port); + client = new FilesClient(host, port, token); + client.ping(); + root = new FilesFolder(this, this, "/", "/"); + connected = true; + } + + @NotNull + FilesClient client() throws DBException { + if (client == null || !connected) { + throw new DBException("Volume files is not connected"); + } + return client; + } + + public boolean isConnected() { + return connected && client != null; + } + + /** Property accessor used by the navigator tree (`property="items"`). */ + @Nullable + public List getItems() { + return root != null ? root.getItems() : null; + } + + @NotNull + @Override + public FilesDataSourceInfo getInfo() { + return info; + } + + @NotNull + @Override + public SQLDialect getSQLDialect() { + return FilesSQLDialect.INSTANCE; + } + + @Override + public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException { + if (root != null) { + root.getItems(monitor); + } + } + + @NotNull + @Override + public DBCExecutionContext getDefaultContext(@NotNull DBRProgressMonitor monitor, boolean meta) { + return executionContext; + } + + @NotNull + @Override + public DBCExecutionContext[] getAllContexts() { + return new DBCExecutionContext[]{executionContext}; + } + + @NotNull + @Override + public DBCExecutionContext openIsolatedContext( + @NotNull DBRProgressMonitor monitor, + @NotNull String purpose, + @Nullable DBCExecutionContext initFrom + ) { + return new FilesExecutionContext(this, purpose); + } + + @NotNull + @Override + public DBSInstance getDefaultInstance() { + return this; + } + + @NotNull + @Override + public Collection getAvailableInstances() { + return Collections.singletonList(this); + } + + @Override + public void shutdown(@NotNull DBRProgressMonitor monitor) { + connected = false; + client = null; + try { + executionContext.close(); + } catch (Exception ignored) { + // the context is already gone + } + } + + @NotNull + @Override + public DBPExclusiveResource getExclusiveLock() { + return exclusiveLock; + } + + @Nullable + @Override + public Collection getChildren(@NotNull DBRProgressMonitor monitor) throws DBException { + return root == null ? Collections.emptyList() : root.getItems(monitor); + } + + @Nullable + @Override + public DBSObject getChild(@NotNull DBRProgressMonitor monitor, @NotNull String childName) throws DBException { + return root == null ? null : root.getChild(monitor, childName); + } + + @NotNull + @Override + public Class getPrimaryChildType(@Nullable DBRProgressMonitor monitor) { + return FilesFolder.class; + } + + @Override + public void cacheStructure(@NotNull DBRProgressMonitor monitor, int scope) throws DBException { + if (root != null) { + root.cacheStructure(monitor, scope); + } + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesDataSourceInfo.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesDataSourceInfo.java new file mode 100644 index 00000000000..195fdc98235 --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesDataSourceInfo.java @@ -0,0 +1,71 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.dbeaver.model.impl.AbstractDataSourceInfo; +import org.osgi.framework.Version; + +public class FilesDataSourceInfo extends AbstractDataSourceInfo { + + @NotNull + @Override + public String getDatabaseProductName() { + return "Volume files"; + } + + @NotNull + @Override + public String getDatabaseProductVersion() { + return "1"; + } + + @NotNull + @Override + public Version getDatabaseVersion() { + return new Version(1, 0, 0); + } + + @NotNull + @Override + public String getDriverName() { + return "HTTP files (Stackblaze)"; + } + + @NotNull + @Override + public String getDriverVersion() { + return "1.x"; + } + + @NotNull + @Override + public String getSchemaTerm() { + return "Folder"; + } + + @NotNull + @Override + public String getProcedureTerm() { + return ""; + } + + @NotNull + @Override + public String getCatalogTerm() { + return "Volume"; + } + + @Override + public boolean supportsResultSetLimit() { + return true; + } + + @Override + public boolean isReadOnlyData() { + return true; + } + + @Override + public boolean isDynamicMetadata() { + return true; + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesEntry.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesEntry.java new file mode 100644 index 00000000000..829514295ab --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesEntry.java @@ -0,0 +1,158 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import io.stackblaze.dbeaver.ext.files.FilesConstants; +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.DBException; +import org.jkiss.dbeaver.Log; +import org.jkiss.dbeaver.model.DBPDataKind; +import org.jkiss.dbeaver.model.data.DBDDataFilter; +import org.jkiss.dbeaver.model.data.DBDDataReceiver; +import org.jkiss.dbeaver.model.exec.DBCException; +import org.jkiss.dbeaver.model.exec.DBCExecutionSource; +import org.jkiss.dbeaver.model.exec.DBCSession; +import org.jkiss.dbeaver.model.exec.DBCStatistics; +import org.jkiss.dbeaver.model.impl.local.LocalResultSet; +import org.jkiss.dbeaver.model.impl.local.LocalStatement; +import org.jkiss.dbeaver.model.meta.Property; +import org.jkiss.dbeaver.model.struct.DBSDataContainer; +import org.jkiss.dbeaver.model.struct.DBSObject; + +/** + * A file on the leftover volume. Data tab shows name, size, a small truncated + * text preview, and a "content" LOB column for the real byte-exact view/ + * download (opened lazily via the grid's content panel). + */ +public class FilesEntry implements DBSObject, DBSDataContainer { + + private static final Log log = Log.getLog(FilesEntry.class); + + private final FilesDataSource dataSource; + private final FilesFolder parent; + private final String path; + private final String name; + private final long size; + + public FilesEntry( + @NotNull FilesDataSource dataSource, + @NotNull FilesFolder parent, + @NotNull String path, + @NotNull String name, + long size + ) { + this.dataSource = dataSource; + this.parent = parent; + this.path = path; + this.name = name; + this.size = size; + } + + @NotNull + @Override + @Property(viewable = true, order = 1) + public String getName() { + return name; + } + + @Property(viewable = true, order = 2) + public long getSize() { + return size; + } + + @NotNull + @Property(viewable = true, order = 3) + public String getPath() { + return path; + } + + @Nullable + @Override + public String getDescription() { + return size + " bytes"; + } + + @Override + public boolean isPersisted() { + return true; + } + + @Nullable + @Override + public DBSObject getParentObject() { + return parent; + } + + @NotNull + @Override + public FilesDataSource getDataSource() { + return dataSource; + } + + @NotNull + @Override + public String[] getSupportedFeatures() { + return new String[]{ FEATURE_DATA_SELECT, FEATURE_DATA_COUNT }; + } + + @NotNull + @Override + public DBCStatistics readData( + @Nullable DBCExecutionSource source, + @NotNull DBCSession session, + @NotNull DBDDataReceiver dataReceiver, + @Nullable DBDDataFilter dataFilter, + long firstRow, + long maxRows, + long flags, + int fetchSize + ) throws DBException { + DBCStatistics stats = new DBCStatistics(); + long start = System.currentTimeMillis(); + LocalStatement statement = new LocalStatement(session, "FILES CAT " + path); + LocalResultSet resultSet = new LocalResultSet<>(session, statement); + try { + resultSet.addColumn("name", DBPDataKind.STRING); + resultSet.addColumn("size", DBPDataKind.NUMERIC); + resultSet.addColumn("preview", DBPDataKind.STRING); + resultSet.addColumn("content", DBPDataKind.CONTENT); + String preview = dataSource.client().cat(path, FilesConstants.PREVIEW_CHARS); + FilesContentValue content = new FilesContentValue(dataSource, path, size); + resultSet.addRow(name, size, preview, content); + dataReceiver.fetchStart(session, resultSet, firstRow, maxRows); + long rows = 0; + while (resultSet.nextRow()) { + dataReceiver.fetchRow(session, resultSet); + rows++; + } + stats.setRowsFetched(rows); + dataReceiver.fetchEnd(session, resultSet); + } catch (DBException e) { + throw e; + } catch (Exception e) { + throw new DBCException("Failed to read file '" + path + "': " + e.getMessage(), e); + } finally { + stats.setExecuteTime(System.currentTimeMillis() - start); + try { + resultSet.close(); + } catch (Exception e) { + log.debug(e); + } + try { + dataReceiver.close(); + } catch (Exception e) { + log.debug(e); + } + } + return stats; + } + + @Override + public long countData( + @NotNull DBCExecutionSource source, + @NotNull DBCSession session, + @Nullable DBDDataFilter dataFilter, + long flags + ) { + return 1; + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesExclusiveResource.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesExclusiveResource.java new file mode 100644 index 00000000000..5b7c8d636b7 --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesExclusiveResource.java @@ -0,0 +1,58 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.dbeaver.model.DBPExclusiveResource; + +import java.util.HashSet; +import java.util.Set; + +public class FilesExclusiveResource implements DBPExclusiveResource { + + private final Object mutex = new Object(); + private final Set tasks = new HashSet<>(); + private boolean locked; + + @NotNull + @Override + public Object acquireExclusiveLock() { + synchronized (mutex) { + while (locked) { + try { + mutex.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + locked = true; + return mutex; + } + } + + @Override + public void releaseExclusiveLock(@NotNull Object lock) { + synchronized (mutex) { + locked = false; + mutex.notifyAll(); + } + } + + @NotNull + @Override + public Object acquireTaskLock(@NotNull String taskName, boolean checkDup) { + synchronized (mutex) { + if (checkDup && tasks.contains(taskName)) { + return TASK_PROCESED; + } + tasks.add(taskName); + return taskName; + } + } + + @Override + public void releaseTaskLock(@NotNull String taskName, @NotNull Object lock) { + synchronized (mutex) { + tasks.remove(taskName); + } + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesExecutionContext.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesExecutionContext.java new file mode 100644 index 00000000000..b0a9d9c6d3b --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesExecutionContext.java @@ -0,0 +1,55 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.dbeaver.DBException; +import org.jkiss.dbeaver.model.exec.DBCException; +import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose; +import org.jkiss.dbeaver.model.exec.DBCInvalidatePhase; +import org.jkiss.dbeaver.model.exec.DBCSession; +import org.jkiss.dbeaver.model.impl.AbstractExecutionContext; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; + +public class FilesExecutionContext extends AbstractExecutionContext { + + private volatile boolean connected = true; + + public FilesExecutionContext(@NotNull FilesDataSource dataSource, @NotNull String purpose) { + super(dataSource, purpose); + } + + @Override + public boolean isConnected() { + return connected && getDataSource().isConnected(); + } + + @NotNull + @Override + public DBCSession openSession( + @NotNull DBRProgressMonitor monitor, + @NotNull DBCExecutionPurpose purpose, + @NotNull String task + ) { + return new FilesSession(monitor, this, purpose, task); + } + + @Override + public void checkContextAlive(@NotNull DBRProgressMonitor monitor) throws DBException { + if (!isConnected()) { + throw new DBCException("Volume files connection is closed"); + } + } + + @Override + public void invalidateContext( + @NotNull DBRProgressMonitor monitor, + @NotNull DBCInvalidatePhase phase + ) throws DBException { + // reconnect is lazy on the next list/cat + } + + @Override + public void close() { + connected = false; + closeContext(); + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesFolder.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesFolder.java new file mode 100644 index 00000000000..9bf5017c26f --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesFolder.java @@ -0,0 +1,135 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.DBException; +import org.jkiss.dbeaver.model.meta.Property; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; +import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor; +import org.jkiss.dbeaver.model.struct.DBSObject; +import org.jkiss.dbeaver.model.struct.DBSObjectContainer; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** A directory on the leftover volume. */ +public class FilesFolder implements DBSObject, DBSObjectContainer { + + private final FilesDataSource dataSource; + private final DBSObject parent; + private final String path; + private final String name; + private List items; + + public FilesFolder( + @NotNull FilesDataSource dataSource, + @Nullable DBSObject parent, + @NotNull String path, + @NotNull String name + ) { + this.dataSource = dataSource; + this.parent = parent; + this.path = path; + this.name = name; + } + + @NotNull + @Override + @Property(viewable = true, order = 1) + public String getName() { + return name; + } + + @NotNull + @Property(viewable = true, order = 2) + public String getPath() { + return path; + } + + @Nullable + @Override + public String getDescription() { + return "Folder"; + } + + @Override + public boolean isPersisted() { + return true; + } + + @Nullable + @Override + public DBSObject getParentObject() { + return parent; + } + + @NotNull + @Override + public FilesDataSource getDataSource() { + return dataSource; + } + + @NotNull + public synchronized List getItems(@NotNull DBRProgressMonitor monitor) throws DBException { + if (items != null) { + return items; + } + List found = new ArrayList<>(); + monitor.subTask("List " + path); + for (FilesClient.Entry e : dataSource.client().list(path)) { + String childPath = path.endsWith("/") ? path + e.name() : path + "/" + e.name(); + if (path.equals("/")) { + childPath = "/" + e.name(); + } + if (e.directory()) { + found.add(new FilesFolder(dataSource, this, childPath, e.name())); + } else { + found.add(new FilesEntry(dataSource, this, childPath, e.name(), e.size())); + } + } + items = found; + return items; + } + + @Nullable + public List getItems() { + if (items == null && dataSource.isConnected()) { + try { + return getItems(new VoidProgressMonitor()); + } catch (DBException e) { + return null; + } + } + return items; + } + + @Nullable + @Override + public Collection getChildren(@NotNull DBRProgressMonitor monitor) throws DBException { + return getItems(monitor); + } + + @Nullable + @Override + public DBSObject getChild(@NotNull DBRProgressMonitor monitor, @NotNull String childName) throws DBException { + for (DBSObject o : getItems(monitor)) { + if (o.getName().equals(childName)) { + return o; + } + } + return null; + } + + @NotNull + @Override + public Class getPrimaryChildType(@Nullable DBRProgressMonitor monitor) { + return FilesEntry.class; + } + + @Override + public void cacheStructure(@NotNull DBRProgressMonitor monitor, int scope) throws DBException { + items = null; + getItems(monitor); + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesSQLDialect.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesSQLDialect.java new file mode 100644 index 00000000000..16d336d1672 --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesSQLDialect.java @@ -0,0 +1,58 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.dbeaver.model.exec.DBCExecutionContext; +import org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect; +import org.jkiss.dbeaver.model.sql.SQLDialect; + +/** Minimal dialect so CloudBeaver can attach a files connection. */ +public class FilesSQLDialect extends BasicSQLDialect { + + public static final FilesSQLDialect INSTANCE = new FilesSQLDialect(); + + public FilesSQLDialect() { + } + + @NotNull + @Override + public String getDialectId() { + return "files"; + } + + @NotNull + @Override + public String getDialectName() { + return "Files"; + } + + @Override + public boolean supportsAliasInSelect() { + return false; + } + + @Override + public boolean supportsOrderBy() { + return false; + } + + @Override + public boolean isDelimiterAfterQuery() { + return false; + } + + @NotNull + @Override + public String[] getExecuteKeywords() { + return new String[0]; + } + + @NotNull + public static SQLDialect getDialect() { + return INSTANCE; + } + + @SuppressWarnings("unused") + public void initDriverSettings(@NotNull DBCExecutionContext context) { + // no-op + } +} diff --git a/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesSession.java b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesSession.java new file mode 100644 index 00000000000..610e892f988 --- /dev/null +++ b/server/bundles/io.stackblaze.dbeaver.ext.files/src/io/stackblaze/dbeaver/ext/files/model/FilesSession.java @@ -0,0 +1,59 @@ +package io.stackblaze.dbeaver.ext.files.model; + +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.model.exec.DBCException; +import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose; +import org.jkiss.dbeaver.model.exec.DBCStatement; +import org.jkiss.dbeaver.model.exec.DBCStatementType; +import org.jkiss.dbeaver.model.impl.AbstractSession; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; + +public class FilesSession extends AbstractSession { + + private final FilesExecutionContext context; + + public FilesSession( + @NotNull DBRProgressMonitor monitor, + @NotNull FilesExecutionContext context, + @NotNull DBCExecutionPurpose purpose, + @NotNull String taskTitle + ) { + super(monitor, purpose, taskTitle); + this.context = context; + } + + @NotNull + @Override + public FilesExecutionContext getExecutionContext() { + return context; + } + + @NotNull + @Override + public FilesDataSource getDataSource() { + return context.getDataSource(); + } + + @NotNull + @Override + public DBCStatement prepareStatement( + @NotNull DBCStatementType type, + @NotNull String query, + boolean scrollable, + boolean updatable, + boolean returnGeneratedKeys + ) throws DBCException { + throw new DBCException( + "SQL statements are not supported for volumes. Use the navigator to browse files." + ); + } + + @Override + public void cancelBlock( + @NotNull DBRProgressMonitor monitor, + @Nullable Thread blockThread + ) { + // no-op + } +} diff --git a/server/bundles/pom.xml b/server/bundles/pom.xml index b666d112f70..fa6a4adced4 100644 --- a/server/bundles/pom.xml +++ b/server/bundles/pom.xml @@ -35,6 +35,7 @@ io.stackblaze.dbeaver.ext.redis io.stackblaze.dbeaver.ext.s3 io.stackblaze.dbeaver.ext.mongodb + io.stackblaze.dbeaver.ext.files io.cloudbeaver.product.ce diff --git a/server/features/io.cloudbeaver.server.feature/feature.xml b/server/features/io.cloudbeaver.server.feature/feature.xml index 870d9f20014..c5f48044e3c 100644 --- a/server/features/io.cloudbeaver.server.feature/feature.xml +++ b/server/features/io.cloudbeaver.server.feature/feature.xml @@ -46,6 +46,7 @@ +