-
Notifications
You must be signed in to change notification settings - Fork 102
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 #729 from ChandraAddala/json-pojo-validate
Json Validation and Mapping - part 1
- Loading branch information
Showing
16 changed files
with
988 additions
and
197 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
60 changes: 60 additions & 0 deletions
60
blueflood-core/src/main/java/com/rackspacecloud/blueflood/inputs/constraints/EpochRange.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,60 @@ | ||
package com.rackspacecloud.blueflood.inputs.constraints; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import javax.validation.ReportAsSingleViolation; | ||
import javax.validation.constraints.Min; | ||
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; | ||
|
||
|
||
/** | ||
* | ||
* The annotated element has to be in the appropriate range relative to the current time. | ||
* Applicable for values of type long which represent time in epoch. The range of the | ||
* element is defined as below. | ||
* | ||
* (currentTime - maxPast) <= element <= (currentTime + maxFuture) | ||
* | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = { EpochRangeValidator.class }) | ||
@ReportAsSingleViolation | ||
public @interface EpochRange { | ||
|
||
String message() default "{com.rackspacecloud.blueflood.inputs.constraints.EpochRange.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
/** | ||
* @return the element cannot be older than (current time - maxPast) milli seconds in the past | ||
*/ | ||
EpochRangeLimits maxPast(); | ||
|
||
/** | ||
* @return the element cannot be earlier tha (current time + maxFuture) milli seconds in the future | ||
*/ | ||
EpochRangeLimits maxFuture(); | ||
|
||
/** | ||
* Defines several {@link Min} annotations on the same element. | ||
* | ||
* @see Min | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface List { | ||
|
||
EpochRange[] value(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...-core/src/main/java/com/rackspacecloud/blueflood/inputs/constraints/EpochRangeLimits.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,25 @@ | ||
package com.rackspacecloud.blueflood.inputs.constraints; | ||
|
||
import com.rackspacecloud.blueflood.service.Configuration; | ||
import com.rackspacecloud.blueflood.service.CoreConfig; | ||
|
||
/** | ||
* Constants to set limits for allowed epoch ranges. | ||
*/ | ||
public enum EpochRangeLimits { | ||
|
||
BEFORE_CURRENT_TIME_MS(Configuration.getInstance().getLongProperty(CoreConfig.BEFORE_CURRENT_COLLECTIONTIME_MS)), | ||
AFTER_CURRENT_TIME_MS(Configuration.getInstance().getLongProperty(CoreConfig.AFTER_CURRENT_COLLECTIONTIME_MS)); | ||
|
||
private long value; | ||
|
||
EpochRangeLimits(long value) { | ||
this.value = value; | ||
} | ||
|
||
public long getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
|
37 changes: 37 additions & 0 deletions
37
...re/src/main/java/com/rackspacecloud/blueflood/inputs/constraints/EpochRangeValidator.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,37 @@ | ||
package com.rackspacecloud.blueflood.inputs.constraints; | ||
|
||
import com.rackspacecloud.blueflood.utils.Clock; | ||
import com.rackspacecloud.blueflood.utils.DefaultClockImpl; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class EpochRangeValidator implements ConstraintValidator<EpochRange, Long> { | ||
|
||
private final Clock clock = new DefaultClockImpl(); | ||
|
||
private long maxPast; | ||
private long maxFuture; | ||
|
||
@Override | ||
public void initialize(EpochRange constraintAnnotation) { | ||
this.maxPast = constraintAnnotation.maxPast().getValue(); | ||
this.maxFuture = constraintAnnotation.maxFuture().getValue(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Long value, ConstraintValidatorContext context) { | ||
|
||
long currentTime = clock.now().getMillis(); | ||
|
||
if ( value < currentTime - maxPast ) { | ||
// collectionTime is too far in the past | ||
return false; | ||
} else if ( value > currentTime + maxFuture ) { | ||
// collectionTime is too far in the future | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
Oops, something went wrong.