Skip to content
29 changes: 29 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,17 @@ public void setConf(Configuration newConf) {

@Override
public List<TableIdentifier> listTables(SessionContext context, Namespace ns) {
checkNamespaceIsValid(ns);

ListTablesResponse response = client.get(
paths.tables(ns), ListTablesResponse.class, headers(context), ErrorHandlers.namespaceErrorHandler());
return response.identifiers();
}

@Override
public boolean dropTable(SessionContext context, TableIdentifier identifier) {
checkIdentifierIsValid(identifier);

try {
client.delete(paths.table(identifier), null, headers(context), ErrorHandlers.tableErrorHandler());
return true;
Expand All @@ -198,6 +202,9 @@ public boolean purgeTable(SessionContext context, TableIdentifier ident) {

@Override
public void renameTable(SessionContext context, TableIdentifier from, TableIdentifier to) {
checkIdentifierIsValid(from);
checkIdentifierIsValid(to);

RenameTableRequest request = RenameTableRequest.builder()
.withSource(from)
.withDestination(to)
Expand All @@ -214,6 +221,8 @@ private LoadTableResponse loadInternal(SessionContext context, TableIdentifier i

@Override
public Table loadTable(SessionContext context, TableIdentifier identifier) {
checkIdentifierIsValid(identifier);

MetadataTableType metadataType;
LoadTableResponse response;
try {
Expand Down Expand Up @@ -295,6 +304,8 @@ public List<Namespace> listNamespaces(SessionContext context, Namespace namespac

@Override
public Map<String, String> loadNamespaceMetadata(SessionContext context, Namespace ns) {
checkNamespaceIsValid(ns);

// TODO: rename to LoadNamespaceResponse?
GetNamespaceResponse response = client
.get(paths.namespace(ns), GetNamespaceResponse.class, headers(context), ErrorHandlers.namespaceErrorHandler());
Expand All @@ -303,6 +314,8 @@ public Map<String, String> loadNamespaceMetadata(SessionContext context, Namespa

@Override
public boolean dropNamespace(SessionContext context, Namespace ns) {
checkNamespaceIsValid(ns);

try {
client.delete(paths.namespace(ns), null, headers(context), ErrorHandlers.namespaceErrorHandler());
return true;
Expand All @@ -314,6 +327,8 @@ public boolean dropNamespace(SessionContext context, Namespace ns) {
@Override
public boolean updateNamespaceMetadata(SessionContext context, Namespace ns,
Map<String, String> updates, Set<String> removals) {
checkNamespaceIsValid(ns);

UpdateNamespacePropertiesRequest request = UpdateNamespacePropertiesRequest.builder()
.updateAll(updates)
.removeAll(removals)
Expand Down Expand Up @@ -404,6 +419,8 @@ private class Builder implements Catalog.TableBuilder {
private String location = null;

private Builder(TableIdentifier ident, Schema schema, SessionContext context) {
checkIdentifierIsValid(ident);

this.ident = ident;
this.schema = schema;
this.context = context;
Expand Down Expand Up @@ -678,6 +695,18 @@ private Long expiresInMs(Map<String, String> properties) {
}
}

private void checkIdentifierIsValid(TableIdentifier tableIdentifier) {
if (tableIdentifier.namespace().isEmpty()) {
throw new NoSuchTableException("Invalid table identifier: %s", tableIdentifier);
}
}

private void checkNamespaceIsValid(Namespace namespace) {
if (namespace.isEmpty()) {
throw new NoSuchNamespaceException("Invalid namespace: %s", namespace);
}
}

private static Map<String, String> configHeaders(Map<String, String> properties) {
return RESTUtil.extractPrefixMap(properties, "header.");
}
Expand Down