This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 7997325 [ISSUE 2956]extend validation existed (#2962)
7997325 is described below
commit 79973253c5314eb723d8612afc103cc5ce590706
Author: likeguo <[email protected]>
AuthorDate: Wed Mar 2 14:08:22 2022 +0800
[ISSUE 2956]extend validation existed (#2962)
* extend validation existed
* extend validation existed
* extend validation existed
* extend validation existed
---
.../admin/exception/ResourceNotFoundException.java | 38 ++++++++++
.../admin/exception/ShenyuAdminException.java | 39 ++++++++++
.../shenyu/admin/validation/ExistProvider.java | 45 ++++++++++++
.../admin/validation/annotation/Existed.java | 84 ++++++++++++++++++++++
.../validation/validator/ExistedValidator.java | 69 ++++++++++++++++++
5 files changed, 275 insertions(+)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/exception/ResourceNotFoundException.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/exception/ResourceNotFoundException.java
new file mode 100644
index 0000000..062514a
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/exception/ResourceNotFoundException.java
@@ -0,0 +1,38 @@
+/*
+ * 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.exception;
+
+/**
+ * ResourceNotFoundException.
+ * <p>resource not found.</p>
+ * <p>the resource maybe is bean,is file,is metadata,is rule ......</p>
+ */
+public class ResourceNotFoundException extends ShenyuAdminException {
+
+ public ResourceNotFoundException(final Throwable e) {
+ super(e);
+ }
+
+ public ResourceNotFoundException(final String message) {
+ super(message);
+ }
+
+ public ResourceNotFoundException(final String message, final Throwable
throwable) {
+ super(message, throwable);
+ }
+}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/exception/ShenyuAdminException.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/exception/ShenyuAdminException.java
new file mode 100644
index 0000000..f8e7cc9
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/exception/ShenyuAdminException.java
@@ -0,0 +1,39 @@
+/*
+ * 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.exception;
+
+import org.apache.shenyu.common.exception.ShenyuException;
+
+/**
+ * ShenyuAdminException.
+ * <p>shenyu admin module exception root.</p>
+ */
+public class ShenyuAdminException extends ShenyuException {
+
+ public ShenyuAdminException(final Throwable e) {
+ super(e);
+ }
+
+ public ShenyuAdminException(final String message) {
+ super(message);
+ }
+
+ public ShenyuAdminException(final String message, final Throwable
throwable) {
+ super(message, throwable);
+ }
+}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/ExistProvider.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/ExistProvider.java
new file mode 100644
index 0000000..7dff44f
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/ExistProvider.java
@@ -0,0 +1,45 @@
+/*
+ * 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.validation;
+
+import java.io.Serializable;
+
+/**
+ * ExistProvider.
+ */
+public interface ExistProvider {
+
+ /**
+ * not existed.
+ *
+ * @param key key
+ * @return not exited
+ */
+ default boolean notExisted(final Serializable key) {
+ return !Boolean.TRUE.equals(existed(key));
+ }
+
+
+ /**
+ * existed.
+ *
+ * @param key key
+ * @return existed, if not existed nullable
+ */
+ Boolean existed(Serializable key);
+}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/annotation/Existed.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/annotation/Existed.java
new file mode 100644
index 0000000..ecadf19
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/annotation/Existed.java
@@ -0,0 +1,84 @@
+/*
+ * 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.validation.annotation;
+
+import org.apache.shenyu.admin.validation.ExistProvider;
+import org.apache.shenyu.admin.validation.validator.ExistedValidator;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Repeatable;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE_USE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Existed.
+ */
+@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
+@Retention(RUNTIME)
+@Repeatable(Existed.List.class)
+@Documented
+@Constraint(validatedBy = ExistedValidator.class)
+public @interface Existed {
+
+ /**
+ * message.
+ *
+ * @return string
+ */
+ String message() default "the key is not existed!";
+
+ /**
+ * existed provider.
+ *
+ * @return class
+ */
+ Class<? extends ExistProvider> provider();
+
+ /**
+ * support groups.
+ *
+ * @return class array.
+ */
+ Class<?>[] groups() default {};
+
+ /**
+ * payload.
+ *
+ * @return Payload class array
+ */
+ Class<? extends Payload>[] payload() default {};
+
+
+ @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
+ @Retention(RUNTIME)
+ @Documented
+ @interface List {
+
+ Existed[] value();
+ }
+}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/validator/ExistedValidator.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/validator/ExistedValidator.java
new file mode 100644
index 0000000..b114a60
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/validation/validator/ExistedValidator.java
@@ -0,0 +1,69 @@
+/*
+ * 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.validation.validator;
+
+import org.apache.shenyu.admin.exception.ResourceNotFoundException;
+import org.apache.shenyu.admin.spring.SpringBeanUtils;
+import org.apache.shenyu.admin.validation.ExistProvider;
+import org.apache.shenyu.admin.validation.annotation.Existed;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * ExistedValidator.
+ */
+public class ExistedValidator implements ConstraintValidator<Existed,
Serializable> {
+
+ /**
+ * target annotation.
+ */
+ private Existed annotation;
+
+ /**
+ * provider cache.
+ */
+ private final Map<String, ExistProvider> providerCacheMap = new
ConcurrentHashMap<>();
+
+ @Override
+ public void initialize(final Existed constraintAnnotation) {
+ annotation = constraintAnnotation;
+ }
+
+ @Override
+ public boolean isValid(final Serializable value, final
ConstraintValidatorContext context) {
+ if (Objects.isNull(annotation.provider())) {
+ throw new ResourceNotFoundException("the validation ExistProvider
is not found");
+ }
+ return !getExistProvider().notExisted(value);
+ }
+
+ private ExistProvider getExistProvider() {
+ ExistProvider provider =
providerCacheMap.get(annotation.provider().getName());
+ if (!Objects.isNull(provider)) {
+ return provider;
+ }
+ provider =
SpringBeanUtils.getInstance().getBean(annotation.provider());
+ providerCacheMap.put(annotation.provider().getName(), provider);
+ return provider;
+ }
+}