xunliu commented on code in PR #4354: URL: https://github.com/apache/gravitino/pull/4354#discussion_r1703957877
########## server/src/main/java/org/apache/gravitino/server/web/rest/OwnerOperations.java: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.server.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import java.util.Locale; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.MetadataObjects; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.OwnerManager; +import org.apache.gravitino.dto.requests.OwnerSetRequest; +import org.apache.gravitino.dto.responses.MessageResponse; +import org.apache.gravitino.dto.responses.OwnerResponse; +import org.apache.gravitino.dto.responses.SetResponse; +import org.apache.gravitino.dto.util.DTOConverters; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.server.authorization.NameBindings; +import org.apache.gravitino.server.web.Utils; + [email protected] +@Path("/metalakes/{metalake}/owners") +public class OwnerOperations { + + private final OwnerManager ownershipManager; + + @Context private HttpServletRequest httpRequest; + + public OwnerOperations() { + // Because ownerManager may be null when Gravitino doesn't enable authorization, + // and Jersey injection doesn't support null value. So OwnerOperations chooses to retrieve + // ownerManager from GravitinoEnv instead of injection here. + this.ownershipManager = GravitinoEnv.getInstance().ownerManager(); + } + + @GET + @Path("{type}/{fullName}") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "get-object-owner." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "get-object-owner", absolute = true) + public Response getOwnerForObject( + @PathParam("metalake") String metalake, + @PathParam("type") String type, + @PathParam("fullName") String fullName) { + try { + MetadataObject object = + MetadataObjects.parse( + fullName, MetadataObject.Type.valueOf(type.toUpperCase(Locale.ROOT))); + return Utils.doAs( + httpRequest, + () -> { + Optional<Owner> owner = ownershipManager.getOwner(metalake, object); + if (owner.isPresent()) { + return Utils.ok(new OwnerResponse(DTOConverters.toDTO(owner.get()))); + } else { + return Utils.ok(new MessageResponse("Owner is not set")); Review Comment: Whether an empty `OwnerResponse("")` can be return? Additionally, A RESTful interface can return two different type (Owner|Message) Responses, I think this uncommon, and will increase the burden of the interface caller. ########## server/src/test/java/org/apache/gravitino/server/web/rest/TestOwnerOperations.java: ########## @@ -0,0 +1,221 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.server.web.rest; + +import static org.apache.gravitino.Configs.TREE_LOCK_CLEAN_INTERVAL; +import static org.apache.gravitino.Configs.TREE_LOCK_MAX_NODE_IN_MEMORY; +import static org.apache.gravitino.Configs.TREE_LOCK_MIN_NODE_IN_MEMORY; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.gravitino.Config; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.OwnerManager; +import org.apache.gravitino.dto.authorization.OwnerDTO; +import org.apache.gravitino.dto.requests.OwnerSetRequest; +import org.apache.gravitino.dto.responses.ErrorConstants; +import org.apache.gravitino.dto.responses.ErrorResponse; +import org.apache.gravitino.dto.responses.MessageResponse; +import org.apache.gravitino.dto.responses.OwnerResponse; +import org.apache.gravitino.dto.responses.SetResponse; +import org.apache.gravitino.exceptions.NotFoundException; +import org.apache.gravitino.lock.LockManager; +import org.apache.gravitino.rest.RESTUtils; +import org.glassfish.hk2.utilities.binding.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +class TestOwnerOperations extends JerseyTest { + private static final OwnerManager manager = mock(OwnerManager.class); + + private static class MockServletRequestFactory extends ServletRequestFactoryBase { + @Override + public HttpServletRequest get() { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getRemoteUser()).thenReturn(null); + return request; + } + } + + @BeforeAll + public static void setup() throws IllegalAccessException { + Config config = mock(Config.class); + Mockito.doReturn(100000L).when(config).get(TREE_LOCK_MAX_NODE_IN_MEMORY); + Mockito.doReturn(1000L).when(config).get(TREE_LOCK_MIN_NODE_IN_MEMORY); + Mockito.doReturn(36000L).when(config).get(TREE_LOCK_CLEAN_INTERVAL); + FieldUtils.writeField(GravitinoEnv.getInstance(), "lockManager", new LockManager(config), true); + FieldUtils.writeField(GravitinoEnv.getInstance(), "ownerManager", manager, true); + } + + @Override + protected Application configure() { + try { + forceSet( + TestProperties.CONTAINER_PORT, String.valueOf(RESTUtils.findAvailablePort(2000, 3000))); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ResourceConfig resourceConfig = new ResourceConfig(); + resourceConfig.register(OwnerOperations.class); + resourceConfig.register( + new AbstractBinder() { + @Override + protected void configure() { + bindFactory(MockServletRequestFactory.class).to(HttpServletRequest.class); + } + }); + + return resourceConfig; + } + + @Test + void testGetOwnerForObject() { + Owner owner = + new Owner() { + @Override + public String name() { + return "test"; + } + + @Override + public Type type() { + return Type.USER; + } + }; + + when(manager.getOwner(any(), any())).thenReturn(Optional.of(owner)); + + Response resp = + target("/metalakes/metalake1/owners/metalake/metalake1") Review Comment: I think this request path is very confusing. If we need process schema, then the request path is `/metalakes/metalake1/owners/metalake/metalake1/catalog1/schema1` ? Cloud there be a better design? `/metalake1/catalog1/schema1/owner` ? ########## server/src/main/java/org/apache/gravitino/server/web/rest/OwnerOperations.java: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.server.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import java.util.Locale; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.MetadataObjects; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.OwnerManager; +import org.apache.gravitino.dto.requests.OwnerSetRequest; +import org.apache.gravitino.dto.responses.MessageResponse; +import org.apache.gravitino.dto.responses.OwnerResponse; +import org.apache.gravitino.dto.responses.SetResponse; +import org.apache.gravitino.dto.util.DTOConverters; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.server.authorization.NameBindings; +import org.apache.gravitino.server.web.Utils; + [email protected] +@Path("/metalakes/{metalake}/owners") +public class OwnerOperations { + + private final OwnerManager ownershipManager; Review Comment: Please change to `ownerManager`. -- 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]
