jerqi commented on code in PR #9467:
URL: https://github.com/apache/gravitino/pull/9467#discussion_r2614072219
##########
server/src/main/java/org/apache/gravitino/server/web/filter/ParameterUtil.java:
##########
@@ -65,149 +67,67 @@ public static Object extractFromParameters(Parameter[]
parameters, Object[] args
public static Map<Entity.EntityType, NameIdentifier>
extractNameIdentifierFromParameters(
Parameter[] parameters, Object[] args) {
- Map<Entity.EntityType, String> entities = new HashMap<>();
- Map<Entity.EntityType, NameIdentifier> nameIdentifierMap = new HashMap<>();
- // Extract AuthorizationMetadata
- for (int i = 0; i < parameters.length; i++) {
- Parameter parameter = parameters[i];
- AuthorizationMetadata authorizeResource =
- parameter.getAnnotation(AuthorizationMetadata.class);
- if (authorizeResource == null) {
- continue;
+ Map<Entity.EntityType, String> entities =
extractEntitiesFromAnnotations(parameters, args);
+
+ // Build name identifiers from extracted entities
+ Map<Entity.EntityType, NameIdentifier> nameIdentifierMap =
+ NameIdentifierUtil.buildNameIdentifierMap(entities);
+
+ // Extract full name and object type for metadata objects
+ Optional<String> fullName =
+ extractAnnotatedValue(parameters, args, AuthorizationFullName.class);
+ Optional<String> metadataObjectType =
+ extractAnnotatedValue(parameters, args, AuthorizationObjectType.class);
+
+ // Handle fullName and metadataObjectType if present
+ if (fullName.isPresent() && metadataObjectType.isPresent()) {
+ String metalake = entities.get(Entity.EntityType.METALAKE);
+ if (metalake != null) {
+ MetadataObject.Type type =
+
MetadataObject.Type.valueOf(metadataObjectType.get().toUpperCase(Locale.ROOT));
+ NameIdentifier nameIdentifier =
+ MetadataObjectUtil.toEntityIdent(metalake,
MetadataObjects.parse(fullName.get(), type));
+ nameIdentifierMap.putAll(
+ MetadataAuthzHelper.splitMetadataNames(
+ metalake, MetadataObjectUtil.toEntityType(type),
nameIdentifier));
}
- Entity.EntityType type = authorizeResource.type();
- entities.put(type, String.valueOf(args[i]));
}
- String metalake = entities.get(Entity.EntityType.METALAKE);
- String catalog = entities.get(Entity.EntityType.CATALOG);
- String schema = entities.get(Entity.EntityType.SCHEMA);
- String table = entities.get(Entity.EntityType.TABLE);
- String topic = entities.get(Entity.EntityType.TOPIC);
- String fileset = entities.get(Entity.EntityType.FILESET);
+ return nameIdentifierMap;
+ }
- // Extract full name and types
- String fullName = null;
- String metadataObjectType = null;
+ private static Map<Entity.EntityType, String> extractEntitiesFromAnnotations(
+ Parameter[] parameters, Object[] args) {
+ Map<Entity.EntityType, String> entities = new HashMap<>();
for (int i = 0; i < parameters.length; i++) {
- Parameter parameter = parameters[i];
- AuthorizationFullName authorizeFullName =
- parameter.getAnnotation(AuthorizationFullName.class);
- if (authorizeFullName != null) {
- fullName = String.valueOf(args[i]);
- }
-
- AuthorizationObjectType objectType =
parameter.getAnnotation(AuthorizationObjectType.class);
- if (objectType != null) {
- metadataObjectType = String.valueOf(args[i]);
+ AuthorizationMetadata annotation =
parameters[i].getAnnotation(AuthorizationMetadata.class);
+ if (annotation != null) {
+ entities.put(annotation.type(), String.valueOf(args[i]));
}
}
+ return entities;
+ }
- entities.forEach(
- (type, metadata) -> {
- switch (type) {
- case CATALOG:
- nameIdentifierMap.put(
- Entity.EntityType.CATALOG,
NameIdentifierUtil.ofCatalog(metalake, catalog));
- break;
- case SCHEMA:
- nameIdentifierMap.put(
- Entity.EntityType.SCHEMA,
NameIdentifierUtil.ofSchema(metalake, catalog, schema));
- break;
- case TABLE:
- nameIdentifierMap.put(
- Entity.EntityType.TABLE,
- NameIdentifierUtil.ofTable(metalake, catalog, schema,
table));
- break;
- case TOPIC:
- nameIdentifierMap.put(
- Entity.EntityType.TOPIC,
- NameIdentifierUtil.ofTopic(metalake, catalog, schema,
topic));
- break;
- case FILESET:
- nameIdentifierMap.put(
- Entity.EntityType.FILESET,
- NameIdentifierUtil.ofFileset(metalake, catalog, schema,
fileset));
- break;
- case MODEL:
- String model = entities.get(Entity.EntityType.MODEL);
- nameIdentifierMap.put(
- Entity.EntityType.MODEL,
- NameIdentifierUtil.ofModel(metalake, catalog, schema,
model));
- break;
- case METALAKE:
- nameIdentifierMap.put(
- Entity.EntityType.METALAKE,
NameIdentifierUtil.ofMetalake(metalake));
- break;
- case USER:
- nameIdentifierMap.put(
- Entity.EntityType.USER,
- NameIdentifierUtil.ofUser(metadata,
entities.get(Entity.EntityType.USER)));
- break;
- case GROUP:
- nameIdentifierMap.put(
- Entity.EntityType.GROUP,
- NameIdentifierUtil.ofGroup(metalake,
entities.get(Entity.EntityType.GROUP)));
- break;
- case ROLE:
- nameIdentifierMap.put(
- Entity.EntityType.ROLE,
- NameIdentifierUtil.ofRole(metalake,
entities.get(Entity.EntityType.ROLE)));
- break;
- case TAG:
- nameIdentifierMap.put(
- Entity.EntityType.TAG,
- NameIdentifierUtil.ofTag(metalake,
entities.get(Entity.EntityType.TAG)));
- break;
-
- case POLICY:
- nameIdentifierMap.put(
- Entity.EntityType.POLICY,
- NameIdentifierUtil.ofPolicy(metalake,
entities.get(Entity.EntityType.POLICY)));
- break;
-
- case JOB:
- nameIdentifierMap.put(
- Entity.EntityType.JOB,
- NameIdentifierUtil.ofJob(metalake,
entities.get(Entity.EntityType.JOB)));
- break;
-
- case JOB_TEMPLATE:
- nameIdentifierMap.put(
- Entity.EntityType.JOB_TEMPLATE,
- NameIdentifierUtil.ofJobTemplate(
- metalake, entities.get(Entity.EntityType.JOB_TEMPLATE)));
- break;
-
- default:
- break;
- }
- });
-
- // Extract fullName and metadataObjectType
- if (fullName != null && metadataObjectType != null && metalake != null) {
- MetadataObject.Type type =
-
MetadataObject.Type.valueOf(metadataObjectType.toUpperCase(Locale.ROOT));
- NameIdentifier nameIdentifier =
- MetadataObjectUtil.toEntityIdent(metalake,
MetadataObjects.parse(fullName, type));
- nameIdentifierMap.putAll(
- MetadataAuthzHelper.splitMetadataNames(
- metalake, MetadataObjectUtil.toEntityType(type),
nameIdentifier));
+ private static Optional<String> extractAnnotatedValue(
Review Comment:
It's different. We can't reuse it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]