-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #885 from terasolunaorg/issues/761_create-byte-siz…
- Loading branch information
Showing
12 changed files
with
697 additions
and
45 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
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
114 changes: 114 additions & 0 deletions
114
...gfw-validator/src/main/java/org/terasoluna/gfw/common/validator/constraints/ByteSize.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,114 @@ | ||
/* | ||
* Copyright (C) 2013-2017 NTT DATA Corporation | ||
* | ||
* Licensed 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.terasoluna.gfw.common.validator.constraints; | ||
|
||
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.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import javax.validation.ValidationException; | ||
|
||
import org.terasoluna.gfw.common.validator.constraintvalidators.ByteSizeValidator; | ||
|
||
/** | ||
* The annotated element must be a {@link CharSequence}({@link String}, {@link StringBuilder}, etc ...) whose byte length must | ||
* be between the specified minimum and maximum. | ||
* <p> | ||
* This is an annotation combining the functions {@link ByteMin} and {@link ByteMax}. Compared to using two annotations, | ||
* the advantage is that overhead can be reduced by getting byte length at a time. | ||
* </p> | ||
* <p> | ||
* Supported types are: | ||
* </p> | ||
* <ul> | ||
* <li>{@code CharSequence}</li> | ||
* </ul> | ||
* <p> | ||
* {@code null} elements are considered valid. Determine the byte length By encoding the string in the specified | ||
* {@link ByteSize#charset()}. If not specify, encode with charset {@code "UTF-8"}. | ||
* An {@link IllegalArgumentException}(wrapped in {@link ValidationException}) is thrown if specify | ||
* {@link ByteSize#charset()} that can not be used or specify {@link ByteSize#min()} or {@link ByteSize#max()} | ||
* that is negative or specify {@link ByteSize#max()} that lower than {@link ByteSize#min()} value. | ||
* </p> | ||
* @since 5.4.2 | ||
* @see ByteSizeValidator | ||
* @see ByteMin | ||
* @see ByteMax | ||
*/ | ||
@Documented | ||
@Constraint(validatedBy = { ByteSizeValidator.class }) | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) | ||
@Retention(RUNTIME) | ||
public @interface ByteSize { | ||
|
||
/** | ||
* Error message or message key | ||
* @return error message or message key | ||
*/ | ||
String message() default "{org.terasoluna.gfw.common.validator.constraints.ByteSize.message}"; | ||
|
||
/** | ||
* Constraint groups | ||
* @return constraint groups | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* Payload | ||
* @return payload | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
|
||
/** | ||
* @return value the element's byte length must be higher or equal to | ||
*/ | ||
long min() default 0; | ||
|
||
/** | ||
* @return value the element's byte length must be lower or equal to | ||
*/ | ||
long max() default Long.MAX_VALUE; | ||
|
||
/** | ||
* @return the charset name used in parse to a string | ||
*/ | ||
String charset() default "UTF-8"; | ||
|
||
/** | ||
* Defines several {@link ByteSize} annotations on the same element. | ||
* @see ByteSize | ||
* @since 5.4.2 | ||
*/ | ||
@Documented | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) | ||
@Retention(RUNTIME) | ||
@interface List { | ||
/** | ||
* <code>@ByteSize</code> annotations | ||
* @return annotations | ||
*/ | ||
ByteSize[] value(); | ||
} | ||
} |
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
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
101 changes: 101 additions & 0 deletions
101
...main/java/org/terasoluna/gfw/common/validator/constraintvalidators/ByteSizeValidator.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,101 @@ | ||
/* | ||
* Copyright (C) 2013-2017 NTT DATA Corporation | ||
* | ||
* Licensed 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.terasoluna.gfw.common.validator.constraintvalidators; | ||
|
||
import static org.terasoluna.gfw.common.validator.constraintvalidators.ConstraintValidatorsUtils.reportFailedToInitialize; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import org.terasoluna.gfw.common.validator.constraints.ByteSize; | ||
|
||
/** | ||
* Constraint validator class of {@link ByteSize} annotation. | ||
* <p> | ||
* Validate the {@link CharSequence}({@link String}, {@link StringBuilder}, etc ...) whose byte length must be between the | ||
* specified minimum and maximum. Determine the byte length By encoding the string in the specified charset. | ||
* </p> | ||
* @since 5.4.2 | ||
* @see ConstraintValidator | ||
* @see ByteSize | ||
*/ | ||
public class ByteSizeValidator implements | ||
ConstraintValidator<ByteSize, CharSequence> { | ||
|
||
/** | ||
* The charset used in parse to a string. | ||
*/ | ||
private Charset charset; | ||
|
||
/** | ||
* Byte length must be higher or equal to. | ||
*/ | ||
private long min; | ||
|
||
/** | ||
* Byte length must be lower or equal to. | ||
*/ | ||
private long max; | ||
|
||
/** | ||
* Initialize validator. | ||
* @param constraintAnnotation annotation instance for a given constraint declaration | ||
* @throws IllegalArgumentException failed to get a charset by name, or min and max are invalid. | ||
* @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation) | ||
*/ | ||
@Override | ||
public void initialize(ByteSize constraintAnnotation) { | ||
try { | ||
charset = Charset.forName(constraintAnnotation.charset()); | ||
} catch (IllegalArgumentException e) { | ||
throw reportFailedToInitialize(e); | ||
} | ||
min = constraintAnnotation.min(); | ||
max = constraintAnnotation.max(); | ||
if (min < 0) { | ||
throw reportFailedToInitialize(new IllegalArgumentException("min[" | ||
+ min + "] must not be negative value.")); | ||
} | ||
if (max < 0) { | ||
throw reportFailedToInitialize(new IllegalArgumentException("max[" | ||
+ max + "] must not be negative value.")); | ||
} | ||
if (max < min) { | ||
throw reportFailedToInitialize(new IllegalArgumentException("max[" | ||
+ max + "] must be higher or equal to min[" + min + "].")); | ||
} | ||
} | ||
|
||
/** | ||
* Validate execute. | ||
* @param value object to validate | ||
* @param context context in which the constraint is evaluated | ||
* @return {@code true} if {@code value} length is between the specified minimum and maximum, or null. otherwise {@code false}. | ||
* @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext) | ||
*/ | ||
@Override | ||
public boolean isValid(CharSequence value, | ||
ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
long byteLength = value.toString().getBytes(charset).length; | ||
return min <= byteLength && byteLength <= max; | ||
} | ||
} |
Oops, something went wrong.