Skip to content

Commit 34165d5

Browse files
committed
use newHandlerWrapper directly
1 parent b400e8b commit 34165d5

File tree

2 files changed

+30
-66
lines changed

2 files changed

+30
-66
lines changed

service/common/src/main/java/org/apache/polaris/service/catalog/policy/PolicyCatalogAdapter.java

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import jakarta.inject.Inject;
2323
import jakarta.ws.rs.core.Response;
2424
import jakarta.ws.rs.core.SecurityContext;
25-
import java.util.function.Function;
2625
import org.apache.iceberg.catalog.Namespace;
2726
import org.apache.iceberg.exceptions.NotAuthorizedException;
2827
import org.apache.iceberg.rest.RESTUtil;
@@ -39,6 +38,9 @@
3938
import org.apache.polaris.service.types.AttachPolicyRequest;
4039
import org.apache.polaris.service.types.CreatePolicyRequest;
4140
import org.apache.polaris.service.types.DetachPolicyRequest;
41+
import org.apache.polaris.service.types.GetApplicablePoliciesResponse;
42+
import org.apache.polaris.service.types.ListPoliciesResponse;
43+
import org.apache.polaris.service.types.LoadPolicyResponse;
4244
import org.apache.polaris.service.types.PolicyIdentifier;
4345
import org.apache.polaris.service.types.UpdatePolicyRequest;
4446
import org.slf4j.Logger;
@@ -71,24 +73,7 @@ public PolicyCatalogAdapter(
7173
this.prefixParser = prefixParser;
7274
}
7375

74-
private Response withCatalog(
75-
SecurityContext securityContext,
76-
String prefix,
77-
Function<PolicyCatalogHandler, Response> action) {
78-
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
79-
try (PolicyCatalogHandler wrapper = newHandlerWrapper(securityContext, catalogName)) {
80-
return action.apply(wrapper);
81-
} catch (RuntimeException e) {
82-
LOGGER.debug("RuntimeException while operating on policy catalog. Propagating to caller.", e);
83-
throw e;
84-
} catch (Exception e) {
85-
LOGGER.error("Error while operating on policy catalog", e);
86-
throw new RuntimeException(e);
87-
}
88-
}
89-
90-
private PolicyCatalogHandler newHandlerWrapper(
91-
SecurityContext securityContext, String catalogName) {
76+
private PolicyCatalogHandler newHandlerWrapper(SecurityContext securityContext, String prefix) {
9277
var authenticatedPrincipal = (AuthenticatedPolarisPrincipal) securityContext.getUserPrincipal();
9378
if (authenticatedPrincipal == null) {
9479
throw new NotAuthorizedException("Failed to find authenticatedPrincipal in SecurityContext");
@@ -99,7 +84,7 @@ private PolicyCatalogHandler newHandlerWrapper(
9984
entityManager,
10085
metaStoreManager,
10186
securityContext,
102-
catalogName,
87+
prefixParser.prefixToCatalogName(realmContext, prefix),
10388
polarisAuthorizer);
10489
}
10590

@@ -111,10 +96,9 @@ public Response createPolicy(
11196
RealmContext realmContext,
11297
SecurityContext securityContext) {
11398
Namespace ns = decodeNamespace(namespace);
114-
return withCatalog(
115-
securityContext,
116-
prefix,
117-
catalog -> Response.ok(catalog.createPolicy(ns, createPolicyRequest)).build());
99+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
100+
LoadPolicyResponse response = handler.createPolicy(ns, createPolicyRequest);
101+
return Response.ok(response).build();
118102
}
119103

120104
@Override
@@ -129,8 +113,9 @@ public Response listPolicies(
129113
Namespace ns = decodeNamespace(namespace);
130114
PolicyType type =
131115
policyType != null ? PolicyType.fromName(RESTUtil.decodeString(policyType)) : null;
132-
return withCatalog(
133-
securityContext, prefix, catalog -> Response.ok(catalog.listPolicies(ns, type)).build());
116+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
117+
ListPoliciesResponse response = handler.listPolicies(ns, type);
118+
return Response.ok(response).build();
134119
}
135120

136121
@Override
@@ -142,8 +127,9 @@ public Response loadPolicy(
142127
SecurityContext securityContext) {
143128
Namespace ns = decodeNamespace(namespace);
144129
PolicyIdentifier identifier = new PolicyIdentifier(ns, RESTUtil.decodeString(policyName));
145-
return withCatalog(
146-
securityContext, prefix, catalog -> Response.ok(catalog.loadPolicy(identifier)).build());
130+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
131+
LoadPolicyResponse response = handler.loadPolicy(identifier);
132+
return Response.ok(response).build();
147133
}
148134

149135
@Override
@@ -156,10 +142,9 @@ public Response updatePolicy(
156142
SecurityContext securityContext) {
157143
Namespace ns = decodeNamespace(namespace);
158144
PolicyIdentifier identifier = new PolicyIdentifier(ns, RESTUtil.decodeString(policyName));
159-
return withCatalog(
160-
securityContext,
161-
prefix,
162-
catalog -> Response.ok(catalog.updatePolicy(identifier, updatePolicyRequest)).build());
145+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
146+
LoadPolicyResponse response = handler.updatePolicy(identifier, updatePolicyRequest);
147+
return Response.ok(response).build();
163148
}
164149

165150
@Override
@@ -172,13 +157,9 @@ public Response dropPolicy(
172157
SecurityContext securityContext) {
173158
Namespace ns = decodeNamespace(namespace);
174159
PolicyIdentifier identifier = new PolicyIdentifier(ns, RESTUtil.decodeString(policyName));
175-
return withCatalog(
176-
securityContext,
177-
prefix,
178-
catalog -> {
179-
catalog.dropPolicy(identifier, detachAll != null && detachAll);
180-
return Response.status(Response.Status.NO_CONTENT).build();
181-
});
160+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
161+
handler.dropPolicy(identifier, detachAll != null && detachAll);
162+
return Response.status(Response.Status.NO_CONTENT).build();
182163
}
183164

184165
@Override
@@ -191,13 +172,9 @@ public Response attachPolicy(
191172
SecurityContext securityContext) {
192173
Namespace ns = decodeNamespace(namespace);
193174
PolicyIdentifier identifier = new PolicyIdentifier(ns, RESTUtil.decodeString(policyName));
194-
return withCatalog(
195-
securityContext,
196-
prefix,
197-
catalog -> {
198-
catalog.attachPolicy(identifier, attachPolicyRequest);
199-
return Response.status(Response.Status.NO_CONTENT).build();
200-
});
175+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
176+
handler.attachPolicy(identifier, attachPolicyRequest);
177+
return Response.status(Response.Status.NO_CONTENT).build();
201178
}
202179

203180
@Override
@@ -210,13 +187,9 @@ public Response detachPolicy(
210187
SecurityContext securityContext) {
211188
Namespace ns = decodeNamespace(namespace);
212189
PolicyIdentifier identifier = new PolicyIdentifier(ns, RESTUtil.decodeString(policyName));
213-
return withCatalog(
214-
securityContext,
215-
prefix,
216-
catalog -> {
217-
catalog.detachPolicy(identifier, detachPolicyRequest);
218-
return Response.status(Response.Status.NO_CONTENT).build();
219-
});
190+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
191+
handler.detachPolicy(identifier, detachPolicyRequest);
192+
return Response.status(Response.Status.NO_CONTENT).build();
220193
}
221194

222195
@Override
@@ -233,9 +206,8 @@ public Response getApplicablePolicies(
233206
String target = targetName != null ? RESTUtil.decodeString(targetName) : null;
234207
PolicyType type =
235208
policyType != null ? PolicyType.fromName(RESTUtil.decodeString(policyType)) : null;
236-
return withCatalog(
237-
securityContext,
238-
prefix,
239-
catalog -> Response.ok(catalog.getApplicablePolicies(ns, target, type)).build());
209+
PolicyCatalogHandler handler = newHandlerWrapper(securityContext, prefix);
210+
GetApplicablePoliciesResponse response = handler.getApplicablePolicies(ns, target, type);
211+
return Response.ok(response).build();
240212
}
241213
}

service/common/src/main/java/org/apache/polaris/service/catalog/policy/PolicyCatalogHandler.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.common.base.Strings;
2222
import jakarta.annotation.Nullable;
2323
import jakarta.ws.rs.core.SecurityContext;
24-
import java.io.Closeable;
2524
import java.util.Arrays;
2625
import java.util.HashSet;
2726
import java.util.List;
@@ -55,7 +54,7 @@
5554
import org.apache.polaris.service.types.PolicyIdentifier;
5655
import org.apache.polaris.service.types.UpdatePolicyRequest;
5756

58-
public class PolicyCatalogHandler extends CatalogHandler implements AutoCloseable {
57+
public class PolicyCatalogHandler extends CatalogHandler {
5958

6059
private PolarisMetaStoreManager metaStoreManager;
6160

@@ -335,11 +334,4 @@ public void enforcePolicyStoreEnabledOrThrow() {
335334
throw new UnsupportedOperationException("Policy store support is not enabled");
336335
}
337336
}
338-
339-
@Override
340-
public void close() throws Exception {
341-
if (policyCatalog instanceof Closeable closeable) {
342-
closeable.close();
343-
}
344-
}
345337
}

0 commit comments

Comments
 (0)