diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/controller/HealthController.java b/pigx-register/src/main/java/com/alibaba/nacos/console/controller/HealthController.java index f5b0e9813..6799a0bce 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/controller/HealthController.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/controller/HealthController.java @@ -37,74 +37,77 @@ import javax.servlet.http.HttpServletRequest; @RestController("consoleHealth") @RequestMapping("/v1/console/health") public class HealthController { - - private static final Logger LOGGER = LoggerFactory.getLogger(HealthController.class); - - private final ConfigInfoPersistService configInfoPersistService; - - private final OperatorController apiCommands; - - @Autowired - public HealthController(ConfigInfoPersistService configInfoPersistService, OperatorController apiCommands) { - this.configInfoPersistService = configInfoPersistService; - this.apiCommands = apiCommands; - } - - /** - * Whether the Nacos is in broken states or not, and cannot recover except by being restarted. - * - * @return HTTP code equal to 200 indicates that Nacos is in right states. HTTP code equal to 500 indicates that - * Nacos is in broken states. - */ - @GetMapping("/liveness") - public ResponseEntity liveness() { - return ResponseEntity.ok().body("OK"); - } - - /** - * Ready to receive the request or not. - * - * @return HTTP code equal to 200 indicates that Nacos is ready. HTTP code equal to 500 indicates that Nacos is not - * ready. - */ - @GetMapping("/readiness") - public ResponseEntity readiness(HttpServletRequest request) { - boolean isConfigReadiness = isConfigReadiness(); - boolean isNamingReadiness = isNamingReadiness(request); - - if (isConfigReadiness && isNamingReadiness) { - return ResponseEntity.ok().body("OK"); - } - - if (!isConfigReadiness && !isNamingReadiness) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Config and Naming are not in readiness"); - } - - if (!isConfigReadiness) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Config is not in readiness"); - } - - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Naming is not in readiness"); - } - - private boolean isConfigReadiness() { - // check db - try { - configInfoPersistService.configInfoCount(""); - return true; - } catch (Exception e) { - LOGGER.error("Config health check fail.", e); - } - return false; - } - - private boolean isNamingReadiness(HttpServletRequest request) { - try { - apiCommands.metrics(request); - return true; - } catch (Exception e) { - LOGGER.error("Naming health check fail.", e); - } - return false; - } + + private static final Logger LOGGER = LoggerFactory.getLogger(HealthController.class); + + private final ConfigInfoPersistService configInfoPersistService; + + private final OperatorController apiCommands; + + @Autowired + public HealthController(ConfigInfoPersistService configInfoPersistService, OperatorController apiCommands) { + this.configInfoPersistService = configInfoPersistService; + this.apiCommands = apiCommands; + } + + /** + * Whether the Nacos is in broken states or not, and cannot recover except by being + * restarted. + * @return HTTP code equal to 200 indicates that Nacos is in right states. HTTP code + * equal to 500 indicates that Nacos is in broken states. + */ + @GetMapping("/liveness") + public ResponseEntity liveness() { + return ResponseEntity.ok().body("OK"); + } + + /** + * Ready to receive the request or not. + * @return HTTP code equal to 200 indicates that Nacos is ready. HTTP code equal to + * 500 indicates that Nacos is not ready. + */ + @GetMapping("/readiness") + public ResponseEntity readiness(HttpServletRequest request) { + boolean isConfigReadiness = isConfigReadiness(); + boolean isNamingReadiness = isNamingReadiness(request); + + if (isConfigReadiness && isNamingReadiness) { + return ResponseEntity.ok().body("OK"); + } + + if (!isConfigReadiness && !isNamingReadiness) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Config and Naming are not in readiness"); + } + + if (!isConfigReadiness) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Config is not in readiness"); + } + + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Naming is not in readiness"); + } + + private boolean isConfigReadiness() { + // check db + try { + configInfoPersistService.configInfoCount(""); + return true; + } + catch (Exception e) { + LOGGER.error("Config health check fail.", e); + } + return false; + } + + private boolean isNamingReadiness(HttpServletRequest request) { + try { + apiCommands.metrics(request); + return true; + } + catch (Exception e) { + LOGGER.error("Naming health check fail.", e); + } + return false; + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java b/pigx-register/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java index ffe1e31de..32d93ea2b 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java @@ -48,108 +48,104 @@ import java.util.regex.Pattern; @RestController @RequestMapping("/v1/console/namespaces") public class NamespaceController { - - @Autowired - private CommonPersistService commonPersistService; - - @Autowired - private NamespaceOperationService namespaceOperationService; - - private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); - - private static final int NAMESPACE_ID_MAX_LENGTH = 128; - - /** - * Get namespace list. - * - * @return namespace list - */ - @GetMapping - public RestResult> getNamespaces() { - return RestResultUtils.success(namespaceOperationService.getNamespaceList()); - } - - /** - * get namespace all info by namespace id. - * - * @param namespaceId namespaceId - * @return namespace all info - */ - @GetMapping(params = "show=all") - public NamespaceAllInfo getNamespace(@RequestParam("namespaceId") String namespaceId) throws NacosException { - return namespaceOperationService.getNamespace(namespaceId); - } - - /** - * create namespace. - * - * @param namespaceName namespace Name - * @param namespaceDesc namespace Desc - * @return whether create ok - */ - @PostMapping - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) - public Boolean createNamespace(@RequestParam("customNamespaceId") String namespaceId, - @RequestParam("namespaceName") String namespaceName, - @RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { - if (StringUtils.isBlank(namespaceId)) { - namespaceId = UUID.randomUUID().toString(); - } else { - namespaceId = namespaceId.trim(); - if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { - return false; - } - if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { - return false; - } - } - try { - return namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc); - } catch (NacosException e) { - return false; - } - } - - /** - * check namespaceId exist. - * - * @param namespaceId namespace id - * @return true if exist, otherwise false - */ - @GetMapping(params = "checkNamespaceIdExist=true") - public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId) { - if (StringUtils.isBlank(namespaceId)) { - return false; - } - return (commonPersistService.tenantInfoCountByTenantId(namespaceId) > 0); - } - - /** - * edit namespace. - * - * @param namespace namespace - * @param namespaceShowName namespace ShowName - * @param namespaceDesc namespace Desc - * @return whether edit ok - */ - @PutMapping - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) - public Boolean editNamespace(@RequestParam("namespace") String namespace, - @RequestParam("namespaceShowName") String namespaceShowName, - @RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { - return namespaceOperationService.editNamespace(namespace, namespaceShowName, namespaceDesc); - } - - /** - * del namespace by id. - * - * @param namespaceId namespace Id - * @return whether del ok - */ - @DeleteMapping - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) - public Boolean deleteNamespace(@RequestParam("namespaceId") String namespaceId) { - return namespaceOperationService.removeNamespace(namespaceId); - } - + + @Autowired + private CommonPersistService commonPersistService; + + @Autowired + private NamespaceOperationService namespaceOperationService; + + private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); + + private static final int NAMESPACE_ID_MAX_LENGTH = 128; + + /** + * Get namespace list. + * @return namespace list + */ + @GetMapping + public RestResult> getNamespaces() { + return RestResultUtils.success(namespaceOperationService.getNamespaceList()); + } + + /** + * get namespace all info by namespace id. + * @param namespaceId namespaceId + * @return namespace all info + */ + @GetMapping(params = "show=all") + public NamespaceAllInfo getNamespace(@RequestParam("namespaceId") String namespaceId) throws NacosException { + return namespaceOperationService.getNamespace(namespaceId); + } + + /** + * create namespace. + * @param namespaceName namespace Name + * @param namespaceDesc namespace Desc + * @return whether create ok + */ + @PostMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) + public Boolean createNamespace(@RequestParam("customNamespaceId") String namespaceId, + @RequestParam("namespaceName") String namespaceName, + @RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { + if (StringUtils.isBlank(namespaceId)) { + namespaceId = UUID.randomUUID().toString(); + } + else { + namespaceId = namespaceId.trim(); + if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { + return false; + } + if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { + return false; + } + } + try { + return namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc); + } + catch (NacosException e) { + return false; + } + } + + /** + * check namespaceId exist. + * @param namespaceId namespace id + * @return true if exist, otherwise false + */ + @GetMapping(params = "checkNamespaceIdExist=true") + public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId) { + if (StringUtils.isBlank(namespaceId)) { + return false; + } + return (commonPersistService.tenantInfoCountByTenantId(namespaceId) > 0); + } + + /** + * edit namespace. + * @param namespace namespace + * @param namespaceShowName namespace ShowName + * @param namespaceDesc namespace Desc + * @return whether edit ok + */ + @PutMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) + public Boolean editNamespace(@RequestParam("namespace") String namespace, + @RequestParam("namespaceShowName") String namespaceShowName, + @RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { + return namespaceOperationService.editNamespace(namespace, namespaceShowName, namespaceDesc); + } + + /** + * del namespace by id. + * @param namespaceId namespace Id + * @return whether del ok + */ + @DeleteMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) + public Boolean deleteNamespace(@RequestParam("namespaceId") String namespaceId) { + return namespaceOperationService.removeNamespace(namespaceId); + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.java b/pigx-register/src/main/java/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.java index c01325d5a..6827582ff 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/controller/v2/NamespaceControllerV2.java @@ -53,100 +53,96 @@ import java.util.regex.Pattern; @RestController @RequestMapping("/v2/console/namespace") public class NamespaceControllerV2 { - - private final NamespaceOperationService namespaceOperationService; - - public NamespaceControllerV2(NamespaceOperationService namespaceOperationService) { - this.namespaceOperationService = namespaceOperationService; - } - - private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); - - private static final int NAMESPACE_ID_MAX_LENGTH = 128; - - /** - * Get namespace list. - * - * @return namespace list - */ - @GetMapping("/list") - public Result> getNamespaceList() { - return Result.success(namespaceOperationService.getNamespaceList()); - } - - /** - * get namespace all info by namespace id. - * - * @param namespaceId namespaceId - * @return namespace all info - */ - @GetMapping() - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX - + "namespaces", action = ActionTypes.READ, signType = SignType.CONSOLE) - public Result getNamespace(@RequestParam("namespaceId") String namespaceId) - throws NacosException { - return Result.success(namespaceOperationService.getNamespace(namespaceId)); - } - - /** - * create namespace. - * - * @param namespaceForm namespaceForm. - * @return whether create ok - */ - @PostMapping - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX - + "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) - public Result createNamespace(NamespaceForm namespaceForm) throws NacosException { - - namespaceForm.validate(); - - String namespaceId = namespaceForm.getNamespaceId(); - String namespaceName = namespaceForm.getNamespaceName(); - String namespaceDesc = namespaceForm.getNamespaceDesc(); - - if (StringUtils.isBlank(namespaceId)) { - namespaceId = UUID.randomUUID().toString(); - } else { - namespaceId = namespaceId.trim(); - if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { - throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, - "namespaceId [" + namespaceId + "] mismatch the pattern"); - } - if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { - throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, - "too long namespaceId, over " + NAMESPACE_ID_MAX_LENGTH); - } - } - return Result.success(namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc)); - } - - /** - * edit namespace. - * - * @param namespaceForm namespace params - * @return whether edit ok - */ - @PutMapping - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX - + "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) - public Result editNamespace(NamespaceForm namespaceForm) throws NacosException { - namespaceForm.validate(); - return Result.success(namespaceOperationService - .editNamespace(namespaceForm.getNamespaceId(), namespaceForm.getNamespaceName(), - namespaceForm.getNamespaceDesc())); - } - - /** - * delete namespace by id. - * - * @param namespaceId namespace ID - * @return whether delete ok - */ - @DeleteMapping - @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX - + "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) - public Result deleteNamespace(@RequestParam("namespaceId") String namespaceId) { - return Result.success(namespaceOperationService.removeNamespace(namespaceId)); - } + + private final NamespaceOperationService namespaceOperationService; + + public NamespaceControllerV2(NamespaceOperationService namespaceOperationService) { + this.namespaceOperationService = namespaceOperationService; + } + + private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); + + private static final int NAMESPACE_ID_MAX_LENGTH = 128; + + /** + * Get namespace list. + * @return namespace list + */ + @GetMapping("/list") + public Result> getNamespaceList() { + return Result.success(namespaceOperationService.getNamespaceList()); + } + + /** + * get namespace all info by namespace id. + * @param namespaceId namespaceId + * @return namespace all info + */ + @GetMapping() + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.READ, + signType = SignType.CONSOLE) + public Result getNamespace(@RequestParam("namespaceId") String namespaceId) + throws NacosException { + return Result.success(namespaceOperationService.getNamespace(namespaceId)); + } + + /** + * create namespace. + * @param namespaceForm namespaceForm. + * @return whether create ok + */ + @PostMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE, + signType = SignType.CONSOLE) + public Result createNamespace(NamespaceForm namespaceForm) throws NacosException { + + namespaceForm.validate(); + + String namespaceId = namespaceForm.getNamespaceId(); + String namespaceName = namespaceForm.getNamespaceName(); + String namespaceDesc = namespaceForm.getNamespaceDesc(); + + if (StringUtils.isBlank(namespaceId)) { + namespaceId = UUID.randomUUID().toString(); + } + else { + namespaceId = namespaceId.trim(); + if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "namespaceId [" + namespaceId + "] mismatch the pattern"); + } + if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "too long namespaceId, over " + NAMESPACE_ID_MAX_LENGTH); + } + } + return Result.success(namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc)); + } + + /** + * edit namespace. + * @param namespaceForm namespace params + * @return whether edit ok + */ + @PutMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE, + signType = SignType.CONSOLE) + public Result editNamespace(NamespaceForm namespaceForm) throws NacosException { + namespaceForm.validate(); + return Result.success(namespaceOperationService.editNamespace(namespaceForm.getNamespaceId(), + namespaceForm.getNamespaceName(), namespaceForm.getNamespaceDesc())); + } + + /** + * delete namespace by id. + * @param namespaceId namespace ID + * @return whether delete ok + */ + @DeleteMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE, + signType = SignType.CONSOLE) + public Result deleteNamespace(@RequestParam("namespaceId") String namespaceId) { + return Result.success(namespaceOperationService.removeNamespace(namespaceId)); + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/enums/NamespaceTypeEnum.java b/pigx-register/src/main/java/com/alibaba/nacos/console/enums/NamespaceTypeEnum.java index 8674ce8ae..c1c67b5ce 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/enums/NamespaceTypeEnum.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/enums/NamespaceTypeEnum.java @@ -17,50 +17,50 @@ package com.alibaba.nacos.console.enums; /** - * the enum of namespace. - * 0 : Global configuration, 1 : Default private namespace ,2 : Custom namespace. + * the enum of namespace. 0 : Global configuration, 1 : Default private namespace ,2 : + * Custom namespace. * * @author chenglu * @date 2021-05-25 17:01 */ public enum NamespaceTypeEnum { - - /** - * Global configuration. - */ - GLOBAL(0, "Global configuration"), - - /** - * Default private namespace. - */ - PRIVATE(1, "Default private namespace"), - - /** - * Custom namespace. - */ - CUSTOM(2, "Custom namespace"); - - /** - * the namespace type. - */ - private final int type; - - /** - * the description. - */ - private final String description; - - NamespaceTypeEnum(int type, String description) { - this.type = type; - this.description = description; - } - - public int getType() { - return type; - } - - public String getDescription() { - return description; - } -} + /** + * Global configuration. + */ + GLOBAL(0, "Global configuration"), + + /** + * Default private namespace. + */ + PRIVATE(1, "Default private namespace"), + + /** + * Custom namespace. + */ + CUSTOM(2, "Custom namespace"); + + /** + * the namespace type. + */ + private final int type; + + /** + * the description. + */ + private final String description; + + NamespaceTypeEnum(int type, String description) { + this.type = type; + this.description = description; + } + + public int getType() { + return type; + } + + public String getDescription() { + return description; + } + +} diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java b/pigx-register/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java index b7c7fcdb5..9dd2b2f4a 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/exception/ConsoleExceptionHandler.java @@ -37,28 +37,29 @@ import javax.servlet.http.HttpServletRequest; */ @ControllerAdvice public class ConsoleExceptionHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleExceptionHandler.class); - - @ExceptionHandler(AccessException.class) - private ResponseEntity handleAccessException(AccessException e) { - LOGGER.error("got exception. {}", e.getErrMsg()); - return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg()); - } - - @ExceptionHandler(IllegalArgumentException.class) - private ResponseEntity handleIllegalArgumentException(IllegalArgumentException e) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ExceptionUtil.getAllExceptionMsg(e)); - } - - @ExceptionHandler(Exception.class) - private ResponseEntity handleException(HttpServletRequest request, Exception e) { - String uri = request.getRequestURI(); - LOGGER.error("CONSOLE {}", uri, e); - if (uri.contains(Commons.NACOS_SERVER_VERSION_V2)) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(RestResultUtils.failed(ExceptionUtil.getAllExceptionMsg(e))); - } - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getAllExceptionMsg(e)); - } + + private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleExceptionHandler.class); + + @ExceptionHandler(AccessException.class) + private ResponseEntity handleAccessException(AccessException e) { + LOGGER.error("got exception. {}", e.getErrMsg()); + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg()); + } + + @ExceptionHandler(IllegalArgumentException.class) + private ResponseEntity handleIllegalArgumentException(IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ExceptionUtil.getAllExceptionMsg(e)); + } + + @ExceptionHandler(Exception.class) + private ResponseEntity handleException(HttpServletRequest request, Exception e) { + String uri = request.getRequestURI(); + LOGGER.error("CONSOLE {}", uri, e); + if (uri.contains(Commons.NACOS_SERVER_VERSION_V2)) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(RestResultUtils.failed(ExceptionUtil.getAllExceptionMsg(e))); + } + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtil.getAllExceptionMsg(e)); + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandler.java b/pigx-register/src/main/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandler.java index df0c1f025..c73e8f8e0 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandler.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/exception/NacosApiExceptionHandler.java @@ -43,89 +43,92 @@ import java.io.IOException; /** * Exception Handler for Nacos API. + * * @author dongyafei * @date 2022/7/22 */ @Order(-1) -@ControllerAdvice(annotations = {NacosApi.class}) +@ControllerAdvice(annotations = { NacosApi.class }) @ResponseBody public class NacosApiExceptionHandler { - - private static final Logger LOGGER = LoggerFactory.getLogger(NacosApiExceptionHandler.class); - - @ExceptionHandler(NacosApiException.class) - public ResponseEntity> handleNacosApiException(NacosApiException e) { - LOGGER.error("got exception. {} {}", e.getErrAbstract(), e.getErrMsg()); - return ResponseEntity.status(e.getErrCode()).body(new Result<>(e.getDetailErrCode(), e.getErrAbstract(), e.getErrMsg())); - } - - @ExceptionHandler(NacosException.class) - public ResponseEntity> handleNacosException(NacosException e) { - LOGGER.error("got exception. {}", e.getErrMsg()); - return ResponseEntity.status(e.getErrCode()).body(Result.failure(ErrorCode.SERVER_ERROR, e.getErrMsg())); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(HttpMessageNotReadableException.class) - public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.PARAMETER_MISSING, e.getMessage()); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(HttpMessageConversionException.class) - public Result handleHttpMessageConversionException(HttpMessageConversionException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(NumberFormatException.class) - public Result handleNumberFormatException(NumberFormatException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(IllegalArgumentException.class) - public Result handleIllegalArgumentException(IllegalArgumentException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(MissingServletRequestParameterException.class) - public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.PARAMETER_MISSING, e.getMessage()); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(HttpMediaTypeException.class) - public Result handleHttpMediaTypeException(HttpMediaTypeException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.MEDIA_TYPE_ERROR, e.getMessage()); - } - - @ResponseStatus(HttpStatus.FORBIDDEN) - @ExceptionHandler(AccessException.class) - public Result handleAccessException(AccessException e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.ACCESS_DENIED, e.getErrMsg()); - } - - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - @ExceptionHandler(value = {DataAccessException.class, ServletException.class, IOException.class}) - public Result handleDataAccessException(Exception e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(ErrorCode.DATA_ACCESS_ERROR, e.getMessage()); - } - - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - @ExceptionHandler(Exception.class) - public Result handleOtherException(Exception e) { - LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); - return Result.failure(e.getMessage()); - } + + private static final Logger LOGGER = LoggerFactory.getLogger(NacosApiExceptionHandler.class); + + @ExceptionHandler(NacosApiException.class) + public ResponseEntity> handleNacosApiException(NacosApiException e) { + LOGGER.error("got exception. {} {}", e.getErrAbstract(), e.getErrMsg()); + return ResponseEntity.status(e.getErrCode()) + .body(new Result<>(e.getDetailErrCode(), e.getErrAbstract(), e.getErrMsg())); + } + + @ExceptionHandler(NacosException.class) + public ResponseEntity> handleNacosException(NacosException e) { + LOGGER.error("got exception. {}", e.getErrMsg()); + return ResponseEntity.status(e.getErrCode()).body(Result.failure(ErrorCode.SERVER_ERROR, e.getErrMsg())); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(HttpMessageNotReadableException.class) + public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.PARAMETER_MISSING, e.getMessage()); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(HttpMessageConversionException.class) + public Result handleHttpMessageConversionException(HttpMessageConversionException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(NumberFormatException.class) + public Result handleNumberFormatException(NumberFormatException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(IllegalArgumentException.class) + public Result handleIllegalArgumentException(IllegalArgumentException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MissingServletRequestParameterException.class) + public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.PARAMETER_MISSING, e.getMessage()); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(HttpMediaTypeException.class) + public Result handleHttpMediaTypeException(HttpMediaTypeException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.MEDIA_TYPE_ERROR, e.getMessage()); + } + + @ResponseStatus(HttpStatus.FORBIDDEN) + @ExceptionHandler(AccessException.class) + public Result handleAccessException(AccessException e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.ACCESS_DENIED, e.getErrMsg()); + } + + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(value = { DataAccessException.class, ServletException.class, IOException.class }) + public Result handleDataAccessException(Exception e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(ErrorCode.DATA_ACCESS_ERROR, e.getMessage()); + } + + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + public Result handleOtherException(Exception e) { + LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); + return Result.failure(e.getMessage()); + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/filter/XssFilter.java b/pigx-register/src/main/java/com/alibaba/nacos/console/filter/XssFilter.java index 192a609ac..7764ec8ab 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/filter/XssFilter.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/filter/XssFilter.java @@ -26,19 +26,21 @@ import java.io.IOException; /** * XSS filter. + * * @author onewe */ public class XssFilter extends OncePerRequestFilter { - - private static final String CONTENT_SECURITY_POLICY_HEADER = "Content-Security-Policy"; - - private static final String CONTENT_SECURITY_POLICY = "script-src 'self'"; - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) - throws ServletException, IOException { - - response.setHeader(CONTENT_SECURITY_POLICY_HEADER, CONTENT_SECURITY_POLICY); - filterChain.doFilter(request, response); - } + + private static final String CONTENT_SECURITY_POLICY_HEADER = "Content-Security-Policy"; + + private static final String CONTENT_SECURITY_POLICY = "script-src 'self'"; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws ServletException, IOException { + + response.setHeader(CONTENT_SECURITY_POLICY_HEADER, CONTENT_SECURITY_POLICY); + filterChain.doFilter(request, response); + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/model/Namespace.java b/pigx-register/src/main/java/com/alibaba/nacos/console/model/Namespace.java index 0a13166f6..0137dadee 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/model/Namespace.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/model/Namespace.java @@ -22,94 +22,94 @@ package com.alibaba.nacos.console.model; * @author diamond */ public class Namespace { - - private String namespace; - - private String namespaceShowName; - - private String namespaceDesc; - - private int quota; - - private int configCount; - - /** - * see {@link com.alibaba.nacos.console.enums.NamespaceTypeEnum}. - */ - private int type; - - public String getNamespaceShowName() { - return namespaceShowName; - } - - public void setNamespaceShowName(String namespaceShowName) { - this.namespaceShowName = namespaceShowName; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - public Namespace() { - } - - public Namespace(String namespace, String namespaceShowName) { - this.namespace = namespace; - this.namespaceShowName = namespaceShowName; - } - - public Namespace(String namespace, String namespaceShowName, int quota, int configCount, int type) { - this.namespace = namespace; - this.namespaceShowName = namespaceShowName; - this.quota = quota; - this.configCount = configCount; - this.type = type; - } - - public Namespace(String namespace, String namespaceShowName, String namespaceDesc, int quota, int configCount, - int type) { - this.namespace = namespace; - this.namespaceShowName = namespaceShowName; - this.quota = quota; - this.configCount = configCount; - this.type = type; - this.namespaceDesc = namespaceDesc; - } - - public String getNamespaceDesc() { - return namespaceDesc; - } - - public void setNamespaceDesc(String namespaceDesc) { - this.namespaceDesc = namespaceDesc; - } - - public int getQuota() { - return quota; - } - - public void setQuota(int quota) { - this.quota = quota; - } - - public int getConfigCount() { - return configCount; - } - - public void setConfigCount(int configCount) { - this.configCount = configCount; - } - - public int getType() { - return type; - } - - public void setType(int type) { - this.type = type; - } - + + private String namespace; + + private String namespaceShowName; + + private String namespaceDesc; + + private int quota; + + private int configCount; + + /** + * see {@link com.alibaba.nacos.console.enums.NamespaceTypeEnum}. + */ + private int type; + + public String getNamespaceShowName() { + return namespaceShowName; + } + + public void setNamespaceShowName(String namespaceShowName) { + this.namespaceShowName = namespaceShowName; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public Namespace() { + } + + public Namespace(String namespace, String namespaceShowName) { + this.namespace = namespace; + this.namespaceShowName = namespaceShowName; + } + + public Namespace(String namespace, String namespaceShowName, int quota, int configCount, int type) { + this.namespace = namespace; + this.namespaceShowName = namespaceShowName; + this.quota = quota; + this.configCount = configCount; + this.type = type; + } + + public Namespace(String namespace, String namespaceShowName, String namespaceDesc, int quota, int configCount, + int type) { + this.namespace = namespace; + this.namespaceShowName = namespaceShowName; + this.quota = quota; + this.configCount = configCount; + this.type = type; + this.namespaceDesc = namespaceDesc; + } + + public String getNamespaceDesc() { + return namespaceDesc; + } + + public void setNamespaceDesc(String namespaceDesc) { + this.namespaceDesc = namespaceDesc; + } + + public int getQuota() { + return quota; + } + + public void setQuota(int quota) { + this.quota = quota; + } + + public int getConfigCount() { + return configCount; + } + + public void setConfigCount(int configCount) { + this.configCount = configCount; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/model/NamespaceAllInfo.java b/pigx-register/src/main/java/com/alibaba/nacos/console/model/NamespaceAllInfo.java index 92d9c8fd5..3d5441888 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/model/NamespaceAllInfo.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/model/NamespaceAllInfo.java @@ -22,10 +22,10 @@ package com.alibaba.nacos.console.model; * @author Nacos */ public class NamespaceAllInfo extends Namespace { - - public NamespaceAllInfo(String namespace, String namespaceShowName, int quota, int configCount, int type, - String namespaceDesc) { - super(namespace, namespaceShowName, namespaceDesc, quota, configCount, type); - } - + + public NamespaceAllInfo(String namespace, String namespaceShowName, int quota, int configCount, int type, + String namespaceDesc) { + super(namespace, namespaceShowName, namespaceDesc, quota, configCount, type); + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/model/form/NamespaceForm.java b/pigx-register/src/main/java/com/alibaba/nacos/console/model/form/NamespaceForm.java index 851429a3d..952d6efbe 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/model/form/NamespaceForm.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/model/form/NamespaceForm.java @@ -26,86 +26,90 @@ import java.util.Objects; /** * NamespaceForm. + * * @author dongyafei * @date 2022/8/16 */ public class NamespaceForm implements Serializable { - - private static final long serialVersionUID = -1078976569495343487L; - - private String namespaceId; - - private String namespaceName; - - private String namespaceDesc; - - public NamespaceForm() { - } - - public NamespaceForm(String namespaceId, String namespaceName, String namespaceDesc) { - this.namespaceId = namespaceId; - this.namespaceName = namespaceName; - this.namespaceDesc = namespaceDesc; - } - - public String getNamespaceId() { - return namespaceId; - } - - public void setNamespaceId(String namespaceId) { - this.namespaceId = namespaceId; - } - - public String getNamespaceName() { - return namespaceName; - } - - public void setNamespaceName(String namespaceName) { - this.namespaceName = namespaceName; - } - - public String getNamespaceDesc() { - return namespaceDesc; - } - - public void setNamespaceDesc(String namespaceDesc) { - this.namespaceDesc = namespaceDesc; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - NamespaceForm that = (NamespaceForm) o; - return Objects.equals(namespaceId, that.namespaceId) && Objects.equals(namespaceName, that.namespaceName) - && Objects.equals(namespaceDesc, that.namespaceDesc); - } - - @Override - public int hashCode() { - return Objects.hash(namespaceId, namespaceName, namespaceDesc); - } - - @Override - public String toString() { - return "NamespaceVo{" + "namespaceId='" + namespaceId + '\'' + ", namespaceName='" + namespaceName + '\'' - + ", namespaceDesc='" + namespaceDesc + '\'' + '}'; - } - - /** - * check required param. - * @throws NacosException NacosException - */ - public void validate() throws NacosException { - if (null == namespaceId) { - throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING, "required parameter 'namespaceId' is missing"); - } - if (null == namespaceName) { - throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING, "required parameter 'namespaceName' is missing"); - } - } + + private static final long serialVersionUID = -1078976569495343487L; + + private String namespaceId; + + private String namespaceName; + + private String namespaceDesc; + + public NamespaceForm() { + } + + public NamespaceForm(String namespaceId, String namespaceName, String namespaceDesc) { + this.namespaceId = namespaceId; + this.namespaceName = namespaceName; + this.namespaceDesc = namespaceDesc; + } + + public String getNamespaceId() { + return namespaceId; + } + + public void setNamespaceId(String namespaceId) { + this.namespaceId = namespaceId; + } + + public String getNamespaceName() { + return namespaceName; + } + + public void setNamespaceName(String namespaceName) { + this.namespaceName = namespaceName; + } + + public String getNamespaceDesc() { + return namespaceDesc; + } + + public void setNamespaceDesc(String namespaceDesc) { + this.namespaceDesc = namespaceDesc; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + NamespaceForm that = (NamespaceForm) o; + return Objects.equals(namespaceId, that.namespaceId) && Objects.equals(namespaceName, that.namespaceName) + && Objects.equals(namespaceDesc, that.namespaceDesc); + } + + @Override + public int hashCode() { + return Objects.hash(namespaceId, namespaceName, namespaceDesc); + } + + @Override + public String toString() { + return "NamespaceVo{" + "namespaceId='" + namespaceId + '\'' + ", namespaceName='" + namespaceName + '\'' + + ", namespaceDesc='" + namespaceDesc + '\'' + '}'; + } + + /** + * check required param. + * @throws NacosException NacosException + */ + public void validate() throws NacosException { + if (null == namespaceId) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING, + "required parameter 'namespaceId' is missing"); + } + if (null == namespaceName) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING, + "required parameter 'namespaceName' is missing"); + } + } + } diff --git a/pigx-register/src/main/java/com/alibaba/nacos/console/service/NamespaceOperationService.java b/pigx-register/src/main/java/com/alibaba/nacos/console/service/NamespaceOperationService.java index 199261eff..bfac3a302 100644 --- a/pigx-register/src/main/java/com/alibaba/nacos/console/service/NamespaceOperationService.java +++ b/pigx-register/src/main/java/com/alibaba/nacos/console/service/NamespaceOperationService.java @@ -42,109 +42,108 @@ import java.util.List; @Service public class NamespaceOperationService { - - private final ConfigInfoPersistService configInfoPersistService; - - private final CommonPersistService commonPersistService; - - private static final String DEFAULT_NAMESPACE = "public"; - - private static final String DEFAULT_NAMESPACE_SHOW_NAME = "Public"; - - private static final String DEFAULT_NAMESPACE_DESCRIPTION = "Public Namespace"; - - private static final int DEFAULT_QUOTA = 200; - - private static final String DEFAULT_CREATE_SOURCE = "nacos"; - - private static final String DEFAULT_TENANT = ""; - - private static final String DEFAULT_KP = "1"; - - public NamespaceOperationService(ConfigInfoPersistService configInfoPersistService, - CommonPersistService commonPersistService) { - this.configInfoPersistService = configInfoPersistService; - this.commonPersistService = commonPersistService; - } - - public List getNamespaceList() { - // TODO 获取用kp - List tenantInfos = commonPersistService.findTenantByKp(DEFAULT_KP); - - Namespace namespace0 = new Namespace(NamespaceUtil.getNamespaceDefaultId(), DEFAULT_NAMESPACE, DEFAULT_QUOTA, - configInfoPersistService.configInfoCount(DEFAULT_TENANT), NamespaceTypeEnum.GLOBAL.getType()); - List namespaceList = new ArrayList<>(); - namespaceList.add(namespace0); - - for (TenantInfo tenantInfo : tenantInfos) { - int configCount = configInfoPersistService.configInfoCount(tenantInfo.getTenantId()); - Namespace namespaceTmp = new Namespace(tenantInfo.getTenantId(), tenantInfo.getTenantName(), - tenantInfo.getTenantDesc(), DEFAULT_QUOTA, configCount, NamespaceTypeEnum.CUSTOM.getType()); - namespaceList.add(namespaceTmp); - } - return namespaceList; - } - - /** - * query namespace by namespace id. - * - * @param namespaceId namespace Id. - * @return NamespaceAllInfo. - */ - public NamespaceAllInfo getNamespace(String namespaceId) throws NacosException { - // TODO 获取用kp - if (StringUtils.isBlank(namespaceId) || namespaceId.equals(NamespaceUtil.getNamespaceDefaultId())) { - return new NamespaceAllInfo(namespaceId, DEFAULT_NAMESPACE_SHOW_NAME, DEFAULT_QUOTA, - configInfoPersistService.configInfoCount(DEFAULT_TENANT), NamespaceTypeEnum.GLOBAL.getType(), - DEFAULT_NAMESPACE_DESCRIPTION); - } else { - TenantInfo tenantInfo = commonPersistService.findTenantByKp(DEFAULT_KP, namespaceId); - if (null == tenantInfo) { - throw new NacosApiException(HttpStatus.NOT_FOUND.value(), ErrorCode.NAMESPACE_NOT_EXIST, - "namespaceId [ " + namespaceId + " ] not exist"); - } - int configCount = configInfoPersistService.configInfoCount(namespaceId); - return new NamespaceAllInfo(namespaceId, tenantInfo.getTenantName(), DEFAULT_QUOTA, configCount, - NamespaceTypeEnum.CUSTOM.getType(), tenantInfo.getTenantDesc()); - } - } - - /** - * create namespace. - * - * @param namespaceId namespace ID - * @param namespaceName namespace Name - * @param namespaceDesc namespace Desc - * @return whether create ok - */ - public Boolean createNamespace(String namespaceId, String namespaceName, String namespaceDesc) - throws NacosException { - // TODO 获取用kp - if (commonPersistService.tenantInfoCountByTenantId(namespaceId) > 0) { - throw new NacosApiException(HttpStatus.INTERNAL_SERVER_ERROR.value(), ErrorCode.NAMESPACE_ALREADY_EXIST, - "namespaceId [" + namespaceId + "] already exist"); - } - - commonPersistService - .insertTenantInfoAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc, DEFAULT_CREATE_SOURCE, - System.currentTimeMillis()); - return true; - } - - /** - * edit namespace. - */ - public Boolean editNamespace(String namespaceId, String namespaceName, String namespaceDesc) { - // TODO 获取用kp - commonPersistService.updateTenantNameAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc); - return true; - } - - /** - * remove namespace. - */ - public Boolean removeNamespace(String namespaceId) { - commonPersistService.removeTenantInfoAtomic(DEFAULT_KP, namespaceId); - return true; - } + + private final ConfigInfoPersistService configInfoPersistService; + + private final CommonPersistService commonPersistService; + + private static final String DEFAULT_NAMESPACE = "public"; + + private static final String DEFAULT_NAMESPACE_SHOW_NAME = "Public"; + + private static final String DEFAULT_NAMESPACE_DESCRIPTION = "Public Namespace"; + + private static final int DEFAULT_QUOTA = 200; + + private static final String DEFAULT_CREATE_SOURCE = "nacos"; + + private static final String DEFAULT_TENANT = ""; + + private static final String DEFAULT_KP = "1"; + + public NamespaceOperationService(ConfigInfoPersistService configInfoPersistService, + CommonPersistService commonPersistService) { + this.configInfoPersistService = configInfoPersistService; + this.commonPersistService = commonPersistService; + } + + public List getNamespaceList() { + // TODO 获取用kp + List tenantInfos = commonPersistService.findTenantByKp(DEFAULT_KP); + + Namespace namespace0 = new Namespace(NamespaceUtil.getNamespaceDefaultId(), DEFAULT_NAMESPACE, DEFAULT_QUOTA, + configInfoPersistService.configInfoCount(DEFAULT_TENANT), NamespaceTypeEnum.GLOBAL.getType()); + List namespaceList = new ArrayList<>(); + namespaceList.add(namespace0); + + for (TenantInfo tenantInfo : tenantInfos) { + int configCount = configInfoPersistService.configInfoCount(tenantInfo.getTenantId()); + Namespace namespaceTmp = new Namespace(tenantInfo.getTenantId(), tenantInfo.getTenantName(), + tenantInfo.getTenantDesc(), DEFAULT_QUOTA, configCount, NamespaceTypeEnum.CUSTOM.getType()); + namespaceList.add(namespaceTmp); + } + return namespaceList; + } + + /** + * query namespace by namespace id. + * @param namespaceId namespace Id. + * @return NamespaceAllInfo. + */ + public NamespaceAllInfo getNamespace(String namespaceId) throws NacosException { + // TODO 获取用kp + if (StringUtils.isBlank(namespaceId) || namespaceId.equals(NamespaceUtil.getNamespaceDefaultId())) { + return new NamespaceAllInfo(namespaceId, DEFAULT_NAMESPACE_SHOW_NAME, DEFAULT_QUOTA, + configInfoPersistService.configInfoCount(DEFAULT_TENANT), NamespaceTypeEnum.GLOBAL.getType(), + DEFAULT_NAMESPACE_DESCRIPTION); + } + else { + TenantInfo tenantInfo = commonPersistService.findTenantByKp(DEFAULT_KP, namespaceId); + if (null == tenantInfo) { + throw new NacosApiException(HttpStatus.NOT_FOUND.value(), ErrorCode.NAMESPACE_NOT_EXIST, + "namespaceId [ " + namespaceId + " ] not exist"); + } + int configCount = configInfoPersistService.configInfoCount(namespaceId); + return new NamespaceAllInfo(namespaceId, tenantInfo.getTenantName(), DEFAULT_QUOTA, configCount, + NamespaceTypeEnum.CUSTOM.getType(), tenantInfo.getTenantDesc()); + } + } + + /** + * create namespace. + * @param namespaceId namespace ID + * @param namespaceName namespace Name + * @param namespaceDesc namespace Desc + * @return whether create ok + */ + public Boolean createNamespace(String namespaceId, String namespaceName, String namespaceDesc) + throws NacosException { + // TODO 获取用kp + if (commonPersistService.tenantInfoCountByTenantId(namespaceId) > 0) { + throw new NacosApiException(HttpStatus.INTERNAL_SERVER_ERROR.value(), ErrorCode.NAMESPACE_ALREADY_EXIST, + "namespaceId [" + namespaceId + "] already exist"); + } + + commonPersistService.insertTenantInfoAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc, + DEFAULT_CREATE_SOURCE, System.currentTimeMillis()); + return true; + } + + /** + * edit namespace. + */ + public Boolean editNamespace(String namespaceId, String namespaceName, String namespaceDesc) { + // TODO 获取用kp + commonPersistService.updateTenantNameAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc); + return true; + } + + /** + * remove namespace. + */ + public Boolean removeNamespace(String namespaceId) { + commonPersistService.removeTenantInfoAtomic(DEFAULT_KP, namespaceId); + return true; + } + } diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java index 4011dc524..8133f21d4 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java @@ -100,7 +100,7 @@ public class SysUserServiceImpl extends ServiceImpl impl SysUser sysUser = new SysUser(); BeanUtils.copyProperties(userDto, sysUser); sysUser.setLockFlag( - StrUtil.isBlank(userDto.getLockFlag()) ? UserStateEnum.NORMAL.getCode() :userDto.getLockFlag()); + StrUtil.isBlank(userDto.getLockFlag()) ? UserStateEnum.NORMAL.getCode() : userDto.getLockFlag()); sysUser.setCreateBy(userDto.getUsername()); sysUser.setUpdateBy(userDto.getUsername()); sysUser.setPassword(ENCODER.encode(userDto.getPassword()));