-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Turanski <dturanski@pivotal.io>
- Loading branch information
David Turanski
committed
Dec 7, 2019
1 parent
a0fea8f
commit 62a9fd9
Showing
16 changed files
with
430 additions
and
63 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
api/src/main/java/io/cloudevents/format/DateTimeFormat.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,42 @@ | ||
/** | ||
* Copyright 2019 The CloudEvents Authors | ||
* | ||
* 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 io.cloudevents.format; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = DateTimeValidator.class) | ||
@Documented | ||
/** | ||
* @author dturanski | ||
*/ | ||
public @interface DateTimeFormat { | ||
String message() default "Invalid DateTime format"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
String pattern() default ""; | ||
} |
50 changes: 50 additions & 0 deletions
50
api/src/main/java/io/cloudevents/format/DateTimeValidator.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,50 @@ | ||
/** | ||
* Copyright 2019 The CloudEvents Authors | ||
* | ||
* 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 io.cloudevents.format; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
/** | ||
* @author dturanski | ||
*/ | ||
public class DateTimeValidator implements ConstraintValidator<DateTimeFormat, String> { | ||
private String pattern; | ||
|
||
@Override | ||
public void initialize(DateTimeFormat constraintAnnotation) { | ||
this.pattern = constraintAnnotation.pattern(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String object, ConstraintValidatorContext constraintContext) { | ||
if ( object == null ) { | ||
return true; | ||
} | ||
|
||
try { | ||
if (pattern != null && pattern.length() > 0) { | ||
DateTimeFormatter.ofPattern(pattern).parse(object); | ||
} else { | ||
DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(object); | ||
} | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
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
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
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.