lasdf1234 commented on code in PR #10971: URL: https://github.com/apache/gravitino/pull/10971#discussion_r3212753426
########## common/src/main/java/org/apache/gravitino/dto/requests/CreateUserRequest.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.dto.requests; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; +import lombok.extern.jackson.Jacksonized; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.rest.RESTRequest; + +/** Represents a request to create a built-in IdP user. */ +@Getter +@EqualsAndHashCode +@ToString +@Builder +@Jacksonized +public class CreateUserRequest implements RESTRequest { + + @JsonProperty("user") + private final String user; + + @JsonProperty("password") + private final String password; + + /** Default constructor for CreateUserRequest. (Used for Jackson deserialization.) */ + public CreateUserRequest() { + this(null, null); + } + + /** + * Creates a new CreateUserRequest. + * + * @param user The user name of the built-in IdP user. + * @param password The password of the built-in IdP user. + */ + public CreateUserRequest(String user, String password) { + super(); + this.user = user; + this.password = password; + } + + /** + * Validates the {@link CreateUserRequest} request. + * + * @throws IllegalArgumentException If the request is invalid, this exception is thrown. + */ + @Override + public void validate() throws IllegalArgumentException { + Preconditions.checkArgument( + StringUtils.isNotBlank(user), "\"user\" field is required and cannot be empty"); + Preconditions.checkArgument( + StringUtils.isNotBlank(password), "\"password\" field is required and cannot be empty"); Review Comment: resolved ########## server/src/main/java/org/apache/gravitino/server/web/rest/ExceptionHandlers.java: ########## @@ -576,6 +584,74 @@ public Response handle(OperationType op, String group, String metalake, Exceptio } } + private static class IdpUserExceptionHandler extends BaseExceptionHandler { + + private static final ExceptionHandler INSTANCE = new IdpUserExceptionHandler(); + + private static String getUserErrorMsg(String user, String operation, String reason) { + return String.format( + "Failed to operate built-in IdP user %s operation [%s], reason [%s]", + user, operation, reason); + } + + @Override + public Response handle(OperationType op, String user, String ignored, Exception e) { + String formatted = StringUtil.isBlank(user) ? "" : " [" + user + "]"; + String errorMsg = getUserErrorMsg(formatted, op.name(), getErrorMsg(e)); + LOG.warn(errorMsg, e); + + if (e instanceof IllegalArgumentException) { + return Utils.illegalArguments(errorMsg, e); + + } else if (e instanceof NotFoundException) { + return Utils.notFound(errorMsg, e); Review Comment: resolved ########## server/src/main/java/org/apache/gravitino/server/web/rest/ExceptionHandlers.java: ########## @@ -576,6 +584,74 @@ public Response handle(OperationType op, String group, String metalake, Exceptio } } + private static class IdpUserExceptionHandler extends BaseExceptionHandler { + + private static final ExceptionHandler INSTANCE = new IdpUserExceptionHandler(); + + private static String getUserErrorMsg(String user, String operation, String reason) { + return String.format( + "Failed to operate built-in IdP user %s operation [%s], reason [%s]", + user, operation, reason); + } + + @Override + public Response handle(OperationType op, String user, String ignored, Exception e) { + String formatted = StringUtil.isBlank(user) ? "" : " [" + user + "]"; + String errorMsg = getUserErrorMsg(formatted, op.name(), getErrorMsg(e)); + LOG.warn(errorMsg, e); + + if (e instanceof IllegalArgumentException) { + return Utils.illegalArguments(errorMsg, e); + + } else if (e instanceof NotFoundException) { + return Utils.notFound(errorMsg, e); + + } else if (e instanceof UserAlreadyExistsException) { + return Utils.alreadyExists(errorMsg, e); + + } else if (e instanceof NotInUseException) { + return Utils.notInUse(errorMsg, e); + + } else { + return Utils.internalError(errorMsg, e); + } + } + } + + private static class IdpGroupExceptionHandler extends BaseExceptionHandler { + + private static final ExceptionHandler INSTANCE = new IdpGroupExceptionHandler(); + + private static String getGroupErrorMsg(String group, String operation, String reason) { + return String.format( + "Failed to operate built-in IdP group %s operation [%s], reason [%s]", + group, operation, reason); + } + + @Override + public Response handle(OperationType op, String group, String ignored, Exception e) { + String formatted = StringUtil.isBlank(group) ? "" : " [" + group + "]"; + String errorMsg = getGroupErrorMsg(formatted, op.name(), getErrorMsg(e)); + LOG.warn(errorMsg, e); + + if (e instanceof IllegalArgumentException) { + return Utils.illegalArguments(errorMsg, e); + + } else if (e instanceof NotFoundException) { + return Utils.notFound(errorMsg, e); Review Comment: resolved -- 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]
