Skip to content

Commit

Permalink
Change AttributeImpl.time to String
Browse files Browse the repository at this point in the history
Clean up

Clean up
  • Loading branch information
David Turanski committed Dec 7, 2019
1 parent a0fea8f commit c5fd5f3
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 63 deletions.
42 changes: 42 additions & 0 deletions api/src/main/java/io/cloudevents/format/DateTimeFormat.java
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 api/src/main/java/io/cloudevents/format/DateTimeValidator.java
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;
}
}
}
7 changes: 1 addition & 6 deletions api/src/main/java/io/cloudevents/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
package io.cloudevents.json;

import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

Expand All @@ -35,11 +33,8 @@ public final class Json {
public static final ObjectMapper MAPPER = new ObjectMapper();

static {
// add Jackson datatype for ZonedDateTime
// add Jackson module to support Optional fields
MAPPER.registerModule(new Jdk8Module());

final SimpleModule module = new SimpleModule();
MAPPER.registerModule(module);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions api/src/main/java/io/cloudevents/v02/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;

import io.cloudevents.format.DateTimeFormat;
import java.net.URI;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -58,13 +59,14 @@ public class AttributesImpl implements Attributes {
@NotBlank
private final String id;

private final ZonedDateTime time;
@DateTimeFormat
private final String time;
private final URI schemaurl;
private final String contenttype;


AttributesImpl(String type, String specversion, URI source,
String id, ZonedDateTime time, URI schemaurl, String contenttype) {
String id, String time, URI schemaurl, String contenttype) {
this.type = type;
this.specversion = specversion;
this.source = source;
Expand Down Expand Up @@ -114,7 +116,7 @@ public URI getSource() {
* Timestamp of when the event happened.
*/
public Optional<ZonedDateTime> getTime() {
return Optional.ofNullable(time);
return Optional.ofNullable(parseZonedDateTime(time));
}

/**
Expand Down Expand Up @@ -148,7 +150,7 @@ public static AttributesImpl build(
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("contenttype") String contenttype) {

return new AttributesImpl(type, specversion, source, id, parseZonedDateTime(time).orElse(null),
return new AttributesImpl(type, specversion, source, id, time,
schemaurl, contenttype);
}

Expand Down Expand Up @@ -205,11 +207,11 @@ public static Map<String, String> marshal(AttributesImpl attributes) {
return result;
}

static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? Optional.empty() : Optional.of(zonedDateTime.format(ISO_ZONED_DATE_TIME));
static String formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? null :zonedDateTime.format(ISO_ZONED_DATE_TIME);
}

static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? Optional.empty(): Optional.of(ZonedDateTime.parse(zonedDateTime));
static ZonedDateTime parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? null : ZonedDateTime.parse(zonedDateTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public CloudEvent<AttributesImpl, T> build(T data, AttributesImpl attributes,
@Override
public CloudEventImpl<T> build() {
AttributesImpl attributes = new AttributesImpl(type, SPEC_VERSION,
source, id, time, schemaurl, contenttype);
source, id, AttributesImpl.formatZonedDateTime(time), schemaurl, contenttype);

CloudEventImpl<T> event = new CloudEventImpl<>(attributes, data, extensions);

Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/cloudevents/v02/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(AttributesImpl.parseZonedDateTime(time).orElse(null))
.withTime(AttributesImpl.parseZonedDateTime(time))
.withSchemaurl(schemaurl)
.withContenttype(contenttype)
.withData(data)
Expand Down
32 changes: 16 additions & 16 deletions api/src/main/java/io/cloudevents/v03/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
*/
package io.cloudevents.v03;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;

import java.net.URI;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -35,9 +30,13 @@
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import io.cloudevents.Attributes;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;
import io.cloudevents.Attributes;
import io.cloudevents.format.DateTimeFormat;

/**
* The event attributes implementation for v0.3
Expand All @@ -62,7 +61,8 @@ public class AttributesImpl implements Attributes {
@NotBlank
private final String type;

private final ZonedDateTime time;
@DateTimeFormat
private final String time;
private final URI schemaurl;

@Pattern(regexp = "base64")
Expand All @@ -73,7 +73,7 @@ public class AttributesImpl implements Attributes {
private final String subject;

AttributesImpl(String id, URI source, String specversion, String type,
ZonedDateTime time, URI schemaurl, String datacontentencoding,
String time, URI schemaurl, String datacontentencoding,
String datacontenttype, String subject) {
this.id = id;
this.source = source;
Expand All @@ -100,7 +100,7 @@ public String getType() {
return type;
}
public Optional<ZonedDateTime> getTime() {
return Optional.ofNullable(time);
return Optional.ofNullable(parseZonedDateTime(time));
}
public Optional<URI> getSchemaurl() {
return Optional.ofNullable(schemaurl);
Expand Down Expand Up @@ -146,7 +146,7 @@ public static AttributesImpl build(
@JsonProperty("datacontenttype") String datacontenttype,
@JsonProperty("subject") String subject) {

return new AttributesImpl(id, source, specversion, type, parseZonedDateTime(time).orElse(null),
return new AttributesImpl(id, source, specversion, type, time,
schemaurl, datacontentencoding, datacontenttype, subject);
}

Expand Down Expand Up @@ -210,11 +210,11 @@ public static AttributesImpl unmarshal(Map<String, String> attributes) {
datacontenttype, subject);
}

static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? Optional.empty() : Optional.of(zonedDateTime.format(ISO_ZONED_DATE_TIME));
static String formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? null :zonedDateTime.format(ISO_ZONED_DATE_TIME);
}

static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? Optional.empty(): Optional.of(ZonedDateTime.parse(zonedDateTime));
static ZonedDateTime parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? null : ZonedDateTime.parse(zonedDateTime);
}
}
3 changes: 2 additions & 1 deletion api/src/main/java/io/cloudevents/v03/CloudEventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* The event builder.
*
* @author fabiojose
* @author dturanski
*
*/
public final class CloudEventBuilder<T> implements
Expand Down Expand Up @@ -167,7 +168,7 @@ public CloudEvent<AttributesImpl, T> build(T data, AttributesImpl attributes,
public CloudEventImpl<T> build() {

AttributesImpl attributes = new AttributesImpl(id, source, SPEC_VERSION,
type, time, schemaurl, datacontentencoding, datacontenttype,
type, AttributesImpl.formatZonedDateTime(time), schemaurl, datacontentencoding, datacontenttype,
subject);

CloudEventImpl<T> cloudEvent =
Expand Down
6 changes: 3 additions & 3 deletions api/src/main/java/io/cloudevents/v03/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.cloudevents.v03;

import java.net.URI;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
Expand All @@ -30,9 +29,9 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import io.cloudevents.CloudEvent;
import io.cloudevents.extensions.ExtensionFormat;
Expand All @@ -42,6 +41,7 @@
* The event implementation
*
* @author fabiojose
* @author dturanski
*
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand Down Expand Up @@ -126,7 +126,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(AttributesImpl.parseZonedDateTime(time).orElse(null))
.withTime(AttributesImpl.parseZonedDateTime(time))
.withSchemaurl(schemaurl)
.withDatacontentencoding(datacontentencoding)
.withDatacontenttype(datacontenttype)
Expand Down
Loading

0 comments on commit c5fd5f3

Please sign in to comment.