-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e400dec
commit bb4b964
Showing
6 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/cheung/shadowsocks/common/MessageConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.cheung.shadowsocks.common; | ||
|
||
import com.cheung.shadowsocks.model.SSModel; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
class MessageConstraintValidator implements ConstraintValidator<SSModelValidator, SSModel> { | ||
|
||
@Override | ||
public boolean isValid(SSModel value, ConstraintValidatorContext context) { | ||
return (value.getHost() != null) && (value.getPort() != 0); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/cheung/shadowsocks/common/SSModelValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* $Header$ | ||
* $Revision$ | ||
* $Date$ | ||
* | ||
*============================================================================== | ||
* | ||
* Copyright (c) 2016-2026 Primeton Technologies, Ltd. | ||
* All rights reserved. | ||
* | ||
* Created on 2020��4��28�� | ||
*******************************************************************************/ | ||
|
||
|
||
package com.cheung.shadowsocks.common; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = {MessageConstraintValidator.class}) | ||
public @interface SSModelValidator { | ||
|
||
String message() default "error"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/cheung/shadowsocks/utils/ValidateUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.cheung.shadowsocks.utils; | ||
|
||
import org.hibernate.validator.HibernateValidator; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import java.util.Set; | ||
|
||
public enum ValidateUtils { | ||
utils; | ||
|
||
private Validator validator = Validation.byProvider(HibernateValidator.class) | ||
.configure() | ||
.failFast(true) | ||
.buildValidatorFactory() | ||
.getValidator(); | ||
|
||
public <T> String validate(T object) { | ||
Set<ConstraintViolation<T>> validate = validator.validate(object); | ||
return validate.stream().findFirst().map(ConstraintViolation::getMessage).orElse(""); | ||
} | ||
} |