Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ZonedDateTime with valid String representation #80

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
<version>3.10.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<properties>
Expand Down
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;
}
}
}
9 changes: 1 addition & 8 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,13 +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();
module.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer());
module.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());
MAPPER.registerModule(module);
}

/**
Expand Down

This file was deleted.

46 changes: 0 additions & 46 deletions api/src/main/java/io/cloudevents/json/ZonedDateTimeSerializer.java

This file was deleted.

34 changes: 21 additions & 13 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,8 @@

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

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.cloudevents.format.DateTimeFormat;
import java.net.URI;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -33,14 +35,13 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import io.cloudevents.Attributes;
import io.cloudevents.json.ZonedDateTimeDeserializer;

/**
*
* @author fabiojose
* @author dturanski
* @version 0.2
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand All @@ -58,13 +59,16 @@ public class AttributesImpl implements Attributes {

@NotBlank
private final String id;

private final ZonedDateTime time;

@DateTimeFormat
@JsonProperty("time")
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 @@ -113,9 +117,9 @@ public URI getSource() {
/**
* Timestamp of when the event happened.
*/
@JsonDeserialize(using = ZonedDateTimeDeserializer.class)
@JsonIgnore
public Optional<ZonedDateTime> getTime() {
return Optional.ofNullable(time);
return Optional.ofNullable(parseZonedDateTime(time));
}

/**
Expand Down Expand Up @@ -145,7 +149,7 @@ public static AttributesImpl build(
@JsonProperty("source") URI source,
@JsonProperty("specversion") String specversion,
@JsonProperty("type") String type,
@JsonProperty("time") ZonedDateTime time,
@JsonProperty("time") String time,
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("contenttype") String contenttype) {

Expand All @@ -159,11 +163,7 @@ public static AttributesImpl build(
*/
public static AttributesImpl unmarshal(Map<String, String> attributes) {
String type = attributes.get(ContextAttributes.type.name());
ZonedDateTime time =
Optional.ofNullable(attributes.get(ContextAttributes.time.name()))
.map((t) -> ZonedDateTime.parse(t,
ISO_ZONED_DATE_TIME))
.orElse(null);
String time = attributes.get(ContextAttributes.time.name());

String specversion = attributes.get(ContextAttributes.specversion.name());
URI source = URI.create(attributes.get(ContextAttributes.source.name()));
Expand Down Expand Up @@ -209,4 +209,12 @@ public static Map<String, String> marshal(AttributesImpl attributes) {

return result;
}

static String formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? null :zonedDateTime.format(ISO_ZONED_DATE_TIME);
}

static ZonedDateTime parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? null : ZonedDateTime.parse(zonedDateTime);
}
}
3 changes: 2 additions & 1 deletion api/src/main/java/io/cloudevents/v02/CloudEventBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* CloudEvent instances builder
*
* @author fabiojose
* @author dturanski
* @version 0.2
*/
public class CloudEventBuilder<T> implements EventBuilder<T, AttributesImpl>,
Expand Down Expand Up @@ -152,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
7 changes: 4 additions & 3 deletions api/src/main/java/io/cloudevents/v02/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.cloudevents.v02;

import java.net.URI;
import java.time.ZonedDateTime;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
Expand All @@ -42,6 +42,7 @@
* The event implementation
*
* @author fabiojose
* @author dturanski
*
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand Down Expand Up @@ -118,7 +119,7 @@ public static <T> CloudEventImpl<T> build(
@JsonProperty("id") String id,
@JsonProperty("source") URI source,
@JsonProperty("type") String type,
@JsonProperty("time") ZonedDateTime time,
@JsonProperty("time") String time,
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("contenttype") String contenttype,
@JsonProperty("data") T data) {
Expand All @@ -127,7 +128,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(time)
.withTime(AttributesImpl.parseZonedDateTime(time))
.withSchemaurl(schemaurl)
.withContenttype(contenttype)
.withData(data)
Expand Down
Loading