2222import jakarta .inject .Inject ;
2323import jakarta .ws .rs .core .Response ;
2424import jakarta .ws .rs .core .SecurityContext ;
25- import java .util .function .Function ;
2625import org .apache .iceberg .catalog .Namespace ;
2726import org .apache .iceberg .exceptions .NotAuthorizedException ;
2827import org .apache .iceberg .rest .RESTUtil ;
3938import org .apache .polaris .service .types .AttachPolicyRequest ;
4039import org .apache .polaris .service .types .CreatePolicyRequest ;
4140import 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 ;
4244import org .apache .polaris .service .types .PolicyIdentifier ;
4345import org .apache .polaris .service .types .UpdatePolicyRequest ;
4446import 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}
0 commit comments