Support for first/last profile name length

This commit is contained in:
Moxie Marlinspike
2020-01-13 18:55:04 -08:00
parent 4468b5a2e4
commit 8a9fed64f2
4 changed files with 108 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
package org.whispersystems.textsecuregcm.util;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = ExactlySizeValidator.class)
@Documented
public @interface ExactlySize {
String message() default "{org.whispersystems.textsecuregcm.util.ExactlySize." +
"message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
int[] value();
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
@interface List {
ExactlySize[] value();
}
}

View File

@@ -0,0 +1,29 @@
package org.whispersystems.textsecuregcm.util;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class ExactlySizeValidator implements ConstraintValidator<ExactlySize, String> {
private int[] permittedSizes;
@Override
public void initialize(ExactlySize exactlySize) {
this.permittedSizes = exactlySize.value();
}
@Override
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
int objectLength;
if (object == null) objectLength = 0;
else objectLength = object.length();
for (int permittedSize : permittedSizes) {
if (permittedSize == objectLength) return true;
}
return false;
}
}