moremind commented on code in PR #5584: URL: https://github.com/apache/shenyu/pull/5584#discussion_r1676840973
########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/NamespacePluginController.java: ########## @@ -0,0 +1,235 @@ +/* + * 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.NamespacePluginRelMapper; +import org.apache.shenyu.admin.mapper.PluginMapper; +import org.apache.shenyu.admin.model.dto.BatchCommonDTO; +import org.apache.shenyu.admin.model.dto.BatchNamespaceCommonDTO; +import org.apache.shenyu.admin.model.dto.NamespacePluginDTO; +import org.apache.shenyu.admin.model.page.CommonPager; +import org.apache.shenyu.admin.model.page.PageParameter; +import org.apache.shenyu.admin.model.query.NamespacePluginQuery; +import org.apache.shenyu.admin.model.query.NamespacePluginQueryCondition; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.NamespacePluginVO; +import org.apache.shenyu.admin.service.NamespacePluginService; +import org.apache.shenyu.admin.service.PageService; +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 namespace plugin controller. + */ +@RestApi("/namespacePlugin") +public class NamespacePluginController implements PagedController<NamespacePluginQueryCondition, NamespacePluginVO> { + + private final NamespacePluginService namespacePluginService; + + private final SyncDataService syncDataService; + + public NamespacePluginController(final NamespacePluginService namespacePluginService, final SyncDataService syncDataService) { + this.namespacePluginService = namespacePluginService; + this.syncDataService = syncDataService; + } + + /** + * query plugins of namespace. + * + * @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<NamespacePluginVO> commonPager = namespacePluginService.listByPage(new NamespacePluginQuery(name, enabled, new PageParameter(currentPage, pageSize), namespaceId)); + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, commonPager); + } + + /** + * query All plugins of namespace. + * + * @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 = namespacePluginService.listAll(namespaceId); + return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, pluginDataList); + } + + /** + * detail plugin of namespace. + * + * @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 = NamespacePluginRelMapper.class) final String id) { + NamespacePluginVO namespacePluginVO = namespacePluginService.findById(id, namespaceId); + return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, namespacePluginVO); + } + +// /** +// * create plugin. +// * +// * @param pluginDTO plugin. +// * @return {@linkplain ShenyuAdminResult} +// */ +// @PostMapping("") +// @RequiresPermissions("system:plugin:add") +// public ShenyuAdminResult createPlugin(@Valid @ModelAttribute final PluginDTO pluginDTO) { +// return ShenyuAdminResult.success(pluginService.createOrUpdate(pluginDTO)); +// } + + + /** + * update plugin of namespace. + * + * @param namespaceId namespace id. + * @param pluginId primary key. + * @param namespacePluginDTO plugin namespace. + * @return {@linkplain ShenyuAdminResult} + */ + @PutMapping("/pluginId={pluginId}&namespaceId={namespaceId}") + @RequiresPermissions("system:plugin:edit") + public ShenyuAdminResult updatePlugin(@PathVariable("namespaceId") + @Existed(message = "namespace is not existed", provider = NamespaceMapper.class) final String namespaceId, + @PathVariable("pluginId") + @Existed(message = "PluginMapper is not existed", provider = PluginMapper.class) final String pluginId, + @Valid @ModelAttribute final NamespacePluginDTO namespacePluginDTO) { + namespacePluginDTO.setPluginId(pluginId); + namespacePluginDTO.setNamespaceId(namespaceId); + return ShenyuAdminResult.success(namespacePluginService.update(namespacePluginDTO)); + } + + + /** + * delete plugins of namespace. + * + * @param batchNamespaceCommonDTO the batch namespace common dto + * @return {@linkplain ShenyuAdminResult} + */ + @DeleteMapping("/batch") + @RequiresPermissions("system:plugin:delete") + public ShenyuAdminResult deletePlugins(@Valid @RequestBody final BatchNamespaceCommonDTO batchNamespaceCommonDTO) { + final String result = namespacePluginService.delete(batchNamespaceCommonDTO.getIds(), batchNamespaceCommonDTO.getNamespaceId()); + if (StringUtils.isNoneBlank(result)) { + return ShenyuAdminResult.error(result); + } + return ShenyuAdminResult.success(ShenyuResultMessage.DELETE_SUCCESS); + } + + /** + * Enable plugins of namespace. + * + * @param batchCommonDTO the batch common dto + * @return the mono + */ + @PostMapping("/enabled") + @RequiresPermissions("system:plugin:disable") + public ShenyuAdminResult enabled(@Valid @RequestBody final BatchCommonDTO batchCommonDTO) { + if (batchCommonDTO.getEnabled() == null) { Review Comment: check this in dto by validation. ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/NamespacePluginEventPublisher.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.publish; + +import org.apache.shenyu.admin.listener.DataChangedEvent; +import org.apache.shenyu.admin.model.enums.EventTypeEnum; +import org.apache.shenyu.admin.model.event.AdminDataModelChangedEvent; +import org.apache.shenyu.admin.model.event.plugin.BatchNamespacePluginChangedEvent; +import org.apache.shenyu.admin.model.event.plugin.NamespacePluginChangedEvent; +import org.apache.shenyu.admin.model.event.plugin.NamespacePluginCreatedEvent; +import org.apache.shenyu.admin.model.vo.NamespacePluginVO; +import org.apache.shenyu.admin.transfer.PluginTransfer; +import org.apache.shenyu.admin.utils.SessionUtil; +import org.apache.shenyu.common.enums.ConfigGroupEnum; +import org.apache.shenyu.common.enums.DataEventTypeEnum; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; + +import java.util.Collection; +import java.util.Collections; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * NamespacePluginEventPublisher. + */ +@Component +public class NamespacePluginEventPublisher implements AdminDataModelChangedEventPublisher<NamespacePluginVO> { + + private final ApplicationEventPublisher publisher; + + public NamespacePluginEventPublisher(final ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + + + /** + * on plugin created. + * + * @param namespacePluginVO namespacePluginVO + */ + @Override + public void onCreated(final NamespacePluginVO namespacePluginVO) { + publisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.CREATE, + Collections.singletonList(PluginTransfer.INSTANCE.mapToData(namespacePluginVO)))); + publish(new NamespacePluginCreatedEvent(namespacePluginVO, SessionUtil.visitorName())); + } + + @Override + public void onUpdated(final NamespacePluginVO namespacePluginVO, final NamespacePluginVO before) { + publisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.UPDATE, + Collections.singletonList(PluginTransfer.INSTANCE.mapToData(namespacePluginVO)))); + publish(new NamespacePluginChangedEvent(namespacePluginVO, before, EventTypeEnum.PLUGIN_UPDATE, SessionUtil.visitorName())); + } + + @Override + public void onDeleted(final NamespacePluginVO namespacePluginVO) { + publish(new NamespacePluginChangedEvent(namespacePluginVO, null, EventTypeEnum.PLUGIN_DELETE, SessionUtil.visitorName())); + publisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.DELETE, + Stream.of(namespacePluginVO).map(PluginTransfer.INSTANCE::mapToData).collect(Collectors.toList()))); + } + + @Override + public void onDeleted(final Collection<NamespacePluginVO> namespacePlugin) { + publish(new BatchNamespacePluginChangedEvent(namespacePlugin, null, EventTypeEnum.PLUGIN_UPDATE, SessionUtil.visitorName())); + publisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.UPDATE, + namespacePlugin.stream().map(PluginTransfer.INSTANCE::mapToData).collect(Collectors.toList()))); + } + + Review Comment: too much blank ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.NamespacePluginRelMapper; +import org.apache.shenyu.admin.model.dto.NamespaceDTO; +import org.apache.shenyu.admin.model.entity.NamespaceDO; +import org.apache.shenyu.admin.model.entity.NamespacePluginRelDO; +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.NamespaceIDUtils; +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.stream.Collectors; + +@Service +public class NamespaceServiceImpl implements NamespaceService { + + private NamespaceMapper namespaceMapper; + + private NamespacePluginRelMapper namespacePluginRelMapper; + + private PluginService pluginService; + + public NamespaceServiceImpl(final NamespaceMapper namespaceMapper, + final NamespacePluginRelMapper namespacePluginRelMapper, + final PluginService pluginService) { + this.namespaceMapper = namespaceMapper; + this.namespacePluginRelMapper = namespacePluginRelMapper; + 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(final String name) { + List<NamespaceDO> namespaceDOS = namespaceMapper.selectAll(name); + return namespaceDOS.stream().map(NamespaceTransfer.INSTANCE::mapToVo).collect(Collectors.toList()); + } + + private NamespaceVO create(final NamespaceDTO namespaceDTO) { + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + String id = UUIDUtils.getInstance().generateShortUuid(); + String namespaceId = NamespaceIDUtils.getInstance().generateNamespaceID(); + NamespaceDO namespaceDO = NamespaceDO.builder() + .id(id) + .namespaceId(namespaceId) + .name(namespaceDTO.getName()) + .description(namespaceDTO.getDescription()) + .dateCreated(currentTime) + .dateUpdated(currentTime) + .build(); + List<PluginData> pluginData = pluginService.listAll(); + List<NamespacePluginRelDO> pluginNsRelList = pluginData.stream().map(s -> NamespacePluginRelDO.builder() + .id(UUIDUtils.getInstance().generateShortUuid()) + .pluginId(s.getId()) + .config(s.getConfig()) + .sort(s.getSort()) + .enabled(s.getEnabled()) + .namespaceId(namespaceId) + .dateCreated(currentTime) + .dateUpdated(currentTime) + .build()).collect(Collectors.toList()); + namespaceMapper.insert(namespaceDO); + namespacePluginRelMapper.batchSave(pluginNsRelList); + return NamespaceTransfer.INSTANCE.mapToVo(namespaceDO); + } + + private NamespaceVO update(final NamespaceDTO namespaceDTO) { + if (Objects.isNull(namespaceDTO) || Objects.isNull(namespaceDTO.getNamespaceId())) { Review Comment: remove this or check this by validation ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/NamespacePluginQuery.java: ########## @@ -0,0 +1,80 @@ +/* + * 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.model.query; + +import org.apache.shenyu.admin.model.page.PageParameter; + +import java.io.Serializable; +import java.util.Objects; + +/** + * this is namespace plugin query. + */ +public class NamespacePluginQuery extends PluginQuery implements Serializable { + Review Comment: add serial id -- 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