Skip to content

Commit

Permalink
[ADD] 新增网关过滤器管理及对应的单元测试
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Yeh <[email protected]>
  • Loading branch information
alan-yeh committed Nov 15, 2024
1 parent 51c6b8d commit 962966c
Show file tree
Hide file tree
Showing 7 changed files with 773 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.gateway.controller;

import central.bean.Page;
import central.data.gateway.GatewayFilter;
import central.starter.web.param.IdsParams;
import central.starter.web.query.IdQuery;
import central.studio.dashboard.controller.gateway.params.FilterParams;
import central.studio.dashboard.controller.gateway.query.FilterPageQuery;
import central.studio.dashboard.logic.gateway.GatewayLogic;
import central.validation.group.Insert;
import central.validation.group.Update;
import central.web.XForwardedHeaders;
import jakarta.validation.groups.Default;
import lombok.Setter;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* Gateway Filter Controller
* <p>
* 网关过滤器管理
*
* @author Alan Yeh
* @since 2024/11/15
*/
@RestController
@RequiresAuthentication
@RequestMapping("/dashboard/api/gateway/filters")
public class FilterController {

public interface Permissions {
String VIEW = "identity:strategy:view";
String ADD = "identity:strategy:add";
String EDIT = "identity:strategy:edit";
String DELETE = "identity:strategy:delete";
String ENABLE = "identity:strategy:enable";
String DISABLE = "identity:strategy:disable";
}

@Setter(onMethod_ = @Autowired)
private GatewayLogic logic;

/**
* 按条件分页查询列表
*
* @param query 查询
* @param tenant 租户标识
* @return 分页结果
*/
@GetMapping("/page")
public Page<GatewayFilter> page(@Validated FilterPageQuery query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.pageBy(query.getPageIndex(), query.getPageSize(), query.build(), null, tenant);
}

/**
* 根据主键查询详情
*
* @param query 查询
* @param tenant 租户标识
* @return 详情
*/
@GetMapping("/details")
public GatewayFilter details(@Validated IdQuery<GatewayFilter> query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.findById(query.getId(), tenant);
}

/**
* 更新行政数据
*
* @param params 数据入参
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 新增后的数据
*/
@PostMapping
public GatewayFilter add(@RequestBody @Validated({Insert.class, Default.class}) FilterParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.insert(params.toInput(), accountId, tenant);
}

/**
* 更新数据
*
* @param params 数据入参
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 更新后的数据
*/
@PutMapping
public GatewayFilter update(@RequestBody @Validated({Update.class, Default.class}) FilterParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.update(params.toInput(), accountId, tenant);
}

/**
* 根据主键删除数据
*
* @param params 待删除主键列表
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 受影响数据行数
*/
@DeleteMapping
public long delete(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deleteByIds(params.getIds(), accountId, tenant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.gateway.params;

import central.data.gateway.GatewayFilterInput;
import central.util.Listx;
import central.validation.Label;
import central.validation.group.Insert;
import central.validation.group.Update;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* Gateway Filter Params
* <p>
* 网关过滤器入参
*
* @author Alan Yeh
* @since 2024/11/15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FilterParams {

@Label("主键")
@Null(groups = Insert.class)
@NotBlank(groups = Update.class)
@Size(min = 1, max = 32, groups = Insert.class)
private String id;

@Label("类型")
@NotBlank
@Size(min = 1, max = 32)
private String type;

@Label("匹配路径")
@NotBlank
@Size(min = 1, max = 255)
private String path;

@Label("排序号")
@NotNull
@Min(Integer.MIN_VALUE)
@Max(Integer.MAX_VALUE)
private Integer order;

@Label("是否启用")
@NotNull
private Boolean enabled;

@Label("备注")
@Size(max = 1024)
private String remark;

@Label("初始化参数")
@NotNull
@Size(min = 1, max = 5 * 1024 * 1024)
private String params;

@Label("断言")
@Valid
private List<PredicateParams> predicates;

public GatewayFilterInput toInput() {
return GatewayFilterInput.builder()
.id(this.getId())
.type(this.getType())
.path(this.getPath())
.order(this.getOrder())
.enabled(this.getEnabled())
.remark(this.getRemark())
.params(this.getParams())
.predicates(Listx.asStream(this.getPredicates()).map(PredicateParams::toInput).toList())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.gateway.params;

import central.data.gateway.GatewayPredicateInput;
import central.validation.Label;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Gateway Predicate Params
* <p>
* 网关断言入参
*
* @author Alan Yeh
* @since 2024/11/15
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PredicateParams {

@Label("类型")
@NotBlank
@Size(min = 1, max = 50)
private String type;

@Label("参数")
@NotBlank
@Size(min = 1, max = 5 * 1024 * 1024)
private String params;

public GatewayPredicateInput toInput() {
return GatewayPredicateInput.builder()
.type(this.getType())
.params(this.getParams())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.gateway.query;

import central.data.gateway.GatewayFilter;
import central.sql.query.Conditions;
import central.starter.web.query.PageQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;

/**
* Gateway Filter Page Query
* <p>
* 网关过滤器分页查询
*
* @author Alan Yeh
* @since 2024/11/15
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class FilterPageQuery extends PageQuery<GatewayFilter> {

Check warning on line 45 in central-dashboard/src/main/java/central/studio/dashboard/controller/gateway/query/FilterPageQuery.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Non-serializable class with 'serialVersionUID'

Non-serializable class `FilterPageQuery` defines a 'serialVersionUID' field
@Serial
private static final long serialVersionUID = -2802529539967982307L;

@Override
public Conditions<GatewayFilter> build() {
return null;
}
}
Loading

0 comments on commit 962966c

Please sign in to comment.