moremind commented on code in PR #5584: URL: https://github.com/apache/shenyu/pull/5584#discussion_r1669736499
########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/PluginNamespaceController.java: ########## @@ -0,0 +1,239 @@ +/* + * 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.shenyu.admin.controller; + +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.NamespaceMapper; +import org.apache.shenyu.admin.mapper.PluginMapper; +import org.apache.shenyu.admin.mapper.PluginNsRelMapper; +import org.apache.shenyu.admin.model.dto.BatchCommonDTO; +import org.apache.shenyu.admin.model.dto.BatchNamespaceCommonDTO; +import org.apache.shenyu.admin.model.dto.PluginNamespaceDTO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageParameter; +import org.apache.shenyu.admin.model.query.PluginNamespaceQuery; +import org.apache.shenyu.admin.model.query.PluginNamespaceQueryCondition; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.PluginNamespaceVO; +import org.apache.shenyu.admin.service.PageService; +import org.apache.shenyu.admin.service.PluginNamespaceService; +import org.apache.shenyu.admin.service.SyncDataService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.validation.annotation.Existed; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.common.enums.DataEventTypeEnum; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import java.util.List; + +/** + * this is plugin controller. + */ +@RestApi("/pluginNamespace") +public class PluginNamespaceController implements PagedController<PluginNamespaceQueryCondition, PluginNamespaceVO> { + + private final PluginNamespaceService pluginNamespaceService; + + private final SyncDataService syncDataService; + + public PluginNamespaceController(final PluginNamespaceService pluginNamespaceService, final SyncDataService syncDataService) { + this.pluginNamespaceService = pluginNamespaceService; + this.syncDataService = syncDataService; + } + + /** + * query plugins. + * + * @param name plugin name. + * @param enabled plugin enabled. + * @param namespaceId namespace id. + * @param currentPage current page. + * @param pageSize page size. + * @return {@linkplain ShenyuAdminResult} + */ + @GetMapping("") + public ShenyuAdminResult queryPlugins(final String name, final Integer enabled, + @Existed(message = "namespace is not existed", + provider = NamespaceMapper.class) final String namespaceId, + @NotNull final Integer currentPage, + @NotNull final Integer pageSize) { + CommonPager<PluginNamespaceVO> commonPager = pluginNamespaceService.listByPage(new PluginNamespaceQuery(name, enabled, new PageParameter(currentPage, pageSize), namespaceId)); + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, commonPager); + } + + /** + * query All plugins. + * + * @param namespaceId namespace id. + * @return {@linkplain ShenyuAdminResult} + */ + @GetMapping("/all/{namespaceId}") + public ShenyuAdminResult queryAllPlugins( + @PathVariable("namespaceId") + @Existed(message = "namespace is not existed", provider = NamespaceMapper.class) final String namespaceId) { + List<PluginData> pluginDataList = pluginNamespaceService.listAll(namespaceId); + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, pluginDataList); + } + + /** + * detail plugin. + * + * @param namespaceId namespace id. + * @param id id. + * @return {@linkplain ShenyuAdminResult} + */ + @GetMapping("/id={id}&namespaceId={namespaceId}") + @RequiresPermissions("system:plugin:edit") + public ShenyuAdminResult detailPlugin( + @PathVariable("namespaceId") + @Existed(message = "namespace is not existed", provider = NamespaceMapper.class) final String namespaceId, + @PathVariable("id") + @Existed(message = "id is not existed", + provider = PluginNsRelMapper.class) final String id) { + PluginNamespaceVO pluginNamespaceVO = pluginNamespaceService.findById(id, namespaceId); + return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, pluginNamespaceVO); + } + +// /** +// * create plugin. +// * +// * @param pluginDTO plugin. +// * @return {@linkplain ShenyuAdminResult} +// */ +// @PostMapping("") +// @RequiresPermissions("system:plugin:add") Review Comment: remove this ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.shenyu.admin.service.impl; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.mapper.NamespaceMapper; +import org.apache.shenyu.admin.mapper.PluginNsRelMapper; +import org.apache.shenyu.admin.model.dto.NamespaceDTO; +import org.apache.shenyu.admin.model.entity.NamespaceDO; +import org.apache.shenyu.admin.model.entity.PluginNsRelDO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageResultUtils; +import org.apache.shenyu.admin.model.query.NamespaceQuery; +import org.apache.shenyu.admin.model.vo.NamespaceVO; +import org.apache.shenyu.admin.service.NamespaceService; +import org.apache.shenyu.admin.service.PluginService; +import org.apache.shenyu.admin.transfer.NamespaceTransfer; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.common.constant.AdminConstants; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.common.utils.UUIDUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.sql.Timestamp; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.stream.Collectors; + +@Service +public class NamespaceServiceImpl implements NamespaceService { + + private NamespaceMapper namespaceMapper; + + private PluginNsRelMapper pluginNsRelMapper; + + private PluginService pluginService; + + public NamespaceServiceImpl(final NamespaceMapper namespaceMapper, + final PluginNsRelMapper pluginNsRelMapper, + final PluginService pluginService) { + this.namespaceMapper = namespaceMapper; + this.pluginNsRelMapper = pluginNsRelMapper; + this.pluginService = pluginService; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public NamespaceVO createOrUpdate(final NamespaceDTO namespaceDTO) { + return StringUtils.isBlank(namespaceDTO.getId()) + && StringUtils.isBlank(namespaceDTO.getNamespaceId()) + ? this.create(namespaceDTO) : this.update(namespaceDTO); + } + + @Override + public CommonPager<NamespaceVO> listByPage(final NamespaceQuery namespaceQuery) { + return PageResultUtils.result(namespaceQuery.getPageParameter(), () -> namespaceMapper.countByQuery(namespaceQuery), () -> namespaceMapper.selectByQuery(namespaceQuery) + .stream() + .map(NamespaceTransfer.INSTANCE::mapToVo) + .collect(Collectors.toList())); + } + + @Override + public String delete(final List<String> ids) { + if (ids.contains(AdminConstants.SYS_DEFAULT_NAMESPACE_ID)) { + return AdminConstants.SYS_DEFAULT_NAMESPACE_ID_DELETE; + } + List<NamespaceDO> namespaceDOS = namespaceMapper.selectByIds(ids); + if (CollectionUtils.isEmpty(namespaceDOS)) { + return AdminConstants.SYS_NAMESPACE_ID_NOT_EXIST; + } + namespaceMapper.deleteByIds(ids); + return ShenyuResultMessage.DELETE_SUCCESS; + } + + @Override + public NamespaceVO findById(final String namespaceId) { + return NamespaceTransfer.INSTANCE.mapToVo(namespaceMapper.selectById(namespaceId)); + } + + @Override + public List<NamespaceVO> list() { + List<NamespaceDO> namespaceDOS = namespaceMapper.selectAll(); + return namespaceDOS.stream().map(NamespaceTransfer.INSTANCE::mapToVo).collect(Collectors.toList()); + } + + private NamespaceVO create(final NamespaceDTO namespaceDTO) { + if (namespaceDTO == null) { + return null; + } + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + String id = UUIDUtils.getInstance().generateShortUuid(); + //todo:封装到shenyu-common包下 + String namespaceId = UUID.randomUUID().toString().replace("-", ""); + NamespaceDO namespaceDO = NamespaceDO.builder() + .id(id) + .namespaceId(namespaceId) + .name(namespaceDTO.getName()) + .description(namespaceDTO.getDescription()) + .dateCreated(currentTime) + .dateUpdated(currentTime) + .build(); + List<PluginData> pluginData = pluginService.listAll(); + //创建新命名空间下的默认插件组配置 Review Comment: use english ########## shenyu-admin/src/main/resources/application-mysql.yml: ########## @@ -22,7 +22,7 @@ spring: datasource: url: jdbc:mysql://localhost:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull username: root - password: 12345678 + password: 123 Review Comment: not change this ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/PluginController.java: ########## @@ -185,36 +185,7 @@ public ShenyuAdminResult enabled(@Valid @RequestBody final BatchCommonDTO batchC } return ShenyuAdminResult.success(ShenyuResultMessage.ENABLE_SUCCESS); } - - /** - * sync plugins. - * - * @return {@linkplain ShenyuAdminResult} - */ - @PostMapping("/syncPluginAll") - @RequiresPermissions("system:plugin:modify") - public ShenyuAdminResult syncPluginAll() { - boolean success = syncDataService.syncAll(DataEventTypeEnum.REFRESH); - if (success) { Review Comment: why remove this? ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.shenyu.admin.service.impl; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.mapper.NamespaceMapper; +import org.apache.shenyu.admin.mapper.PluginNsRelMapper; +import org.apache.shenyu.admin.model.dto.NamespaceDTO; +import org.apache.shenyu.admin.model.entity.NamespaceDO; +import org.apache.shenyu.admin.model.entity.PluginNsRelDO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageResultUtils; +import org.apache.shenyu.admin.model.query.NamespaceQuery; +import org.apache.shenyu.admin.model.vo.NamespaceVO; +import org.apache.shenyu.admin.service.NamespaceService; +import org.apache.shenyu.admin.service.PluginService; +import org.apache.shenyu.admin.transfer.NamespaceTransfer; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.common.constant.AdminConstants; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.common.utils.UUIDUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.sql.Timestamp; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.stream.Collectors; + +@Service +public class NamespaceServiceImpl implements NamespaceService { + + private NamespaceMapper namespaceMapper; + + private PluginNsRelMapper pluginNsRelMapper; + + private PluginService pluginService; + + public NamespaceServiceImpl(final NamespaceMapper namespaceMapper, + final PluginNsRelMapper pluginNsRelMapper, + final PluginService pluginService) { + this.namespaceMapper = namespaceMapper; + this.pluginNsRelMapper = pluginNsRelMapper; + this.pluginService = pluginService; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public NamespaceVO createOrUpdate(final NamespaceDTO namespaceDTO) { + return StringUtils.isBlank(namespaceDTO.getId()) + && StringUtils.isBlank(namespaceDTO.getNamespaceId()) + ? this.create(namespaceDTO) : this.update(namespaceDTO); + } + + @Override + public CommonPager<NamespaceVO> listByPage(final NamespaceQuery namespaceQuery) { + return PageResultUtils.result(namespaceQuery.getPageParameter(), () -> namespaceMapper.countByQuery(namespaceQuery), () -> namespaceMapper.selectByQuery(namespaceQuery) + .stream() + .map(NamespaceTransfer.INSTANCE::mapToVo) + .collect(Collectors.toList())); + } + + @Override + public String delete(final List<String> ids) { + if (ids.contains(AdminConstants.SYS_DEFAULT_NAMESPACE_ID)) { + return AdminConstants.SYS_DEFAULT_NAMESPACE_ID_DELETE; + } + List<NamespaceDO> namespaceDOS = namespaceMapper.selectByIds(ids); + if (CollectionUtils.isEmpty(namespaceDOS)) { + return AdminConstants.SYS_NAMESPACE_ID_NOT_EXIST; + } + namespaceMapper.deleteByIds(ids); + return ShenyuResultMessage.DELETE_SUCCESS; + } + + @Override + public NamespaceVO findById(final String namespaceId) { + return NamespaceTransfer.INSTANCE.mapToVo(namespaceMapper.selectById(namespaceId)); + } + + @Override + public List<NamespaceVO> list() { + List<NamespaceDO> namespaceDOS = namespaceMapper.selectAll(); + return namespaceDOS.stream().map(NamespaceTransfer.INSTANCE::mapToVo).collect(Collectors.toList()); + } + + private NamespaceVO create(final NamespaceDTO namespaceDTO) { + if (namespaceDTO == null) { + return null; + } + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + String id = UUIDUtils.getInstance().generateShortUuid(); + //todo:封装到shenyu-common包下 Review Comment: use english ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java: ########## @@ -90,12 +100,15 @@ public SyncDataServiceImpl(final AppAuthService appAuthService, this.discoveryService = discoveryService; } + //todo:[命名空间待改造]只做根据namespaceId做同步 @Override public boolean syncAll(final DataEventTypeEnum type) { appAuthService.syncData(); - List<PluginData> pluginDataList = pluginService.listAll(); - eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataList)); + List<PluginData> pluginDataList = pluginNamespaceService.listAll(); + //todo:[命名空间待改造]暂时只同步默认命名空间的插件数据 Review Comment: use english ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/ShenyuResultMessage.java: ########## @@ -86,6 +86,10 @@ public final class ShenyuResultMessage { public static final String PASSWORD_USED_FOR_LONG_TIME = "If the password has not been changed for a long time, " + "please use it after changing it to ensure the security of the super administrator account"; + + public static final String NAMESPACE_ID_NOT_NULL = "namespace is not null"; + + public static final String ENABLED_NOT_NULL = "enabled is not null"; Review Comment: the message is specific -- 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: notifications-unsubscr...@shenyu.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org