Skip to content

Commit

Permalink
Merge pull request #66 from cloudevents/v1
Browse files Browse the repository at this point in the history
Support for Spec v1.0
  • Loading branch information
fabiojose authored Nov 1, 2019
2 parents f35dc6b + d8fc237 commit f641cb1
Show file tree
Hide file tree
Showing 45 changed files with 3,603 additions and 52 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

TODO

## [1.0.0]

### Added

- Support for [Spec v1.0](https://github.com/cloudevents/spec/tree/v1.0)
- Improved readme to end request for vertx sample [PR 62](https://github.com/cloudevents/sdk-java/pull/62)
- Allow providing an external validator [PR 65](https://github.com/cloudevents/sdk-java/pull/65)

## [0.3.1]

### Fixed
Expand Down Expand Up @@ -56,6 +64,7 @@ abstract envelope signature
- interface: `io.cloudevents.Extension` in favor of
`io.cloudevents.extensions.ExtensionFormat`

[Unreleased]: https://github.com/cloudevents/sdk-java/compare/v0.3.1...HEAD
[Unreleased]: https://github.com/cloudevents/sdk-java/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/cloudevents/sdk-java/compare/v0.3.1...v1.0.0
[0.3.1]: https://github.com/cloudevents/sdk-java/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/cloudevents/sdk-java/compare/v0.2.1...v0.3.0
40 changes: 35 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ For Maven based projects, use the following to configure the CloudEvents Java SD
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-api</artifactId>
<version>0.3.1</version>
<version>1.0.0</version>
</dependency>
```

Application developers can now create strongly-typed CloudEvents, such as:

```java
import io.cloudevents.v02.CloudEventBuilder;
import io.cloudevents.v02.CloudEventImpl;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.CloudEventImpl;
import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.json.Json;
import io.cloudevents.extensions.DistributedTracingExtension;
Expand Down Expand Up @@ -61,11 +61,41 @@ final CloudEventImpl<MyCustomEvent> cloudEvent =
final String json = Json.encode(cloudEvent);
```

There are [other detailed ways](./api/README.md) of how to use the marshallers and unmarshallers with HTTP transport binding.
Or, an event with binary event data:

```java
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.CloudEventImpl;
import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.json.Json;
import io.cloudevents.extensions.DistributedTracingExtension;

// given
final String eventId = UUID.randomUUID().toString();
final URI src = URI.create("/trigger");
final String eventType = "My.Cloud.Event.Type";
final byte[] payload = "a-binary-event-data".getBytes();

// passing in the given attributes
final CloudEventImpl<byte[]> cloudEvent =
CloudEventBuilder.<byte[]>builder()
.withType(eventType)
.withId(eventId)
.withSource(src)
.withData(payload)
.build();

// marshalling as json that will have the data_base64
final String json = Json.encode(cloudEvent);
```

There are [other detailed ways](./api/README.md) of how to use the marshallers
and unmarshallers with HTTP transport binding.

## Kafka

The support for kafka transport binding is available. Read the [documentation and examples](./kafka/README.md) of use.
The support for kafka transport binding is available. Read the
[documentation and examples](./kafka/README.md) of use.

## Possible Integrations

Expand Down
46 changes: 23 additions & 23 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import java.time.ZonedDateTime;
import io.cloudevents.extensions.DistributedTracingExtension;
import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.format.Wire;
import io.cloudevents.v02.CloudEventBuilder;
import io.cloudevents.v02.CloudEventImpl;
import io.cloudevents.v02.http.Marshallers;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.CloudEventImpl;
import io.cloudevents.v1.http.Marshallers;

//...

Expand All @@ -40,9 +40,9 @@ CloudEventImpl<String> ce =
.withType("com.github.pull.create")
.withSource(URI.create("https://github.com/cloudevents/spec/pull"))
.withId("A234-1234-1234")
.withSchemaurl(URI.create("http://my.br"))
.withDataschema(URI.create("http://my.br"))
.withTime(ZonedDateTime.now())
.withContenttype("text/plain")
.withDataContentType("text/plain")
.withData("my-data")
.withExtension(tracing)
.build();
Expand Down Expand Up @@ -74,9 +74,9 @@ import java.util.Map;

import io.cloudevents.CloudEvent;
import io.cloudevents.extensions.DistributedTracingExtension;
import io.cloudevents.v02.AttributesImpl;
import io.cloudevents.v02.CloudEventBuilder;
import io.cloudevents.v02.http.Unmarshallers;
import io.cloudevents.v1.AttributesImpl;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.http.Unmarshallers;

// . . .

Expand All @@ -87,7 +87,7 @@ httpHeaders.put("ce-type", "com.github.pull.create");
httpHeaders.put("ce-source", "https://github.com/cloudevents/spec/pull");
httpHeaders.put("ce-id", "A234-1234-1234");
httpHeaders.put("ce-time", "2018-04-05T17:31:00Z");
httpHeaders.put("ce-schemaurl", "http://my.br");
httpHeaders.put("ce-dataschema", "http://my.br");
httpHeaders.put("my-ext", "my-custom extension");
httpHeaders.put("traceparent", "0");
httpHeaders.put("tracestate", "congo=4");
Expand Down Expand Up @@ -119,9 +119,9 @@ import java.time.ZonedDateTime;

import io.cloudevents.extensions.DistributedTracingExtension;
import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.v02.CloudEventBuilder;
import io.cloudevents.v02.CloudEventImpl;
import io.cloudevents.v02.http.Marshallers;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.CloudEventImpl;
import io.cloudevents.v1.http.Marshallers;

// . . .

Expand All @@ -138,9 +138,9 @@ final CloudEventImpl<String> ce =
.withType("com.github.pull.create")
.withSource(URI.create("https://github.com/cloudevents/spec/pull"))
.withId("A234-1234-1234")
.withSchemaurl(URI.create("http://my.br"))
.withDataschema(URI.create("http://my.br"))
.withTime(ZonedDateTime.now())
.withContenttype("text/plain")
.withDataContentType("text/plain")
.withData("my-data")
.withExtension(tracing)
.build();
Expand Down Expand Up @@ -169,8 +169,8 @@ import java.util.HashMap;
import java.util.Map;

import io.cloudevents.CloudEvent;
import io.cloudevents.v02.AttributesImpl;
import io.cloudevents.v02.http.Unmarshallers;
import io.cloudevents.v1.AttributesImpl;
import io.cloudevents.v1.http.Unmarshallers;

// . . .

Expand All @@ -184,7 +184,7 @@ httpHeaders.put("tracestate", "congo=9");


/* JSON Payload */
String payload = "{\"data\":{\"wow\":\"yes!\"},\"id\":\"x10\",\"source\":\"/source\",\"specversion\":\"0.2\",\"type\":\"event-type\",\"contenttype\":\"application/json\"}";
String payload = "{\"data\":{\"wow\":\"yes!\"},\"id\":\"x10\",\"source\":\"/source\",\"specversion\":\"1.0\",\"type\":\"event-type\",\"datacontenttype\":\"application/json\"}";

/* Unmarshalling . . . */
CloudEvent<AttributesImpl, Map> event =
Expand Down Expand Up @@ -230,8 +230,8 @@ import io.cloudevents.format.Wire;
import io.cloudevents.format.builder.EventStep;
import io.cloudevents.json.Json;
import io.cloudevents.json.types.Much;
import io.cloudevents.v02.Accessor;
import io.cloudevents.v02.AttributesImpl;
import io.cloudevents.v1.Accessor;
import io.cloudevents.v1.AttributesImpl;

// . . .

Expand Down Expand Up @@ -314,8 +314,8 @@ import io.cloudevents.format.StructuredMarshaller;
import io.cloudevents.format.builder.EventStep;
import io.cloudevents.json.Json;
import io.cloudevents.json.types.Much;
import io.cloudevents.v02.Accessor;
import io.cloudevents.v02.AttributesImpl;
import io.cloudevents.v1.Accessor;
import io.cloudevents.v1.AttributesImpl;

// . . .

Expand Down Expand Up @@ -388,8 +388,8 @@ import io.cloudevents.format.BinaryUnmarshaller;
import io.cloudevents.format.builder.HeadersStep;
import io.cloudevents.json.Json;
import io.cloudevents.json.types.Much;
import io.cloudevents.v02.AttributesImpl;
import io.cloudevents.v02.CloudEventBuilder;
import io.cloudevents.v1.AttributesImpl;
import io.cloudevents.v1.CloudEventBuilder;

// . . .

Expand Down
4 changes: 2 additions & 2 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
<parent>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-parent</artifactId>
<version>0.3.1</version>
<version>1.0.0</version>
</parent>

<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-api</artifactId>
<name>CloudEvents - API</name>
<version>0.3.1</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/io/cloudevents/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public static <T> T fromInputStream(final InputStream inputStream,
+ e.getMessage());
}
}

public static <T> T fromInputStream(final InputStream inputStream,
final TypeReference<T> type) {
try {
return MAPPER.readValue(inputStream, type);
} catch (Exception e) {
throw new IllegalStateException("Failed to encode as JSON: "
+ e.getMessage());
}
}

/**
* Decode a given JSON string to a POJO of the given class type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
*
* @author fabiojose
* @version 0.2
* @version 0.3
*/
public class ExtensionMapper {
private ExtensionMapper() {}
Expand Down
59 changes: 59 additions & 0 deletions api/src/main/java/io/cloudevents/v1/Accessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 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.v1;

import java.util.Collection;
import java.util.Objects;

import io.cloudevents.Attributes;
import io.cloudevents.CloudEvent;
import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.fun.ExtensionFormatAccessor;

/**
*
* @author fabiojose
* @version 1.0
*/
public class Accessor {

/**
* To get access the set of {@link ExtensionFormat} inside the
* event.
*
* <br>
* <br>
* This method follow the signature of
* {@link ExtensionFormatAccessor#extensionsOf(CloudEvent)}
*
* @param cloudEvent
* @throws IllegalArgumentException When argument is not an instance
* of {@link CloudEventImpl}
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <A extends Attributes, T> Collection<ExtensionFormat>
extensionsOf(CloudEvent<A, T> cloudEvent) {
Objects.requireNonNull(cloudEvent);

if(cloudEvent instanceof CloudEventImpl) {
CloudEventImpl impl = (CloudEventImpl)cloudEvent;
return impl.getExtensionsFormats();
}

throw new IllegalArgumentException("Invalid instance type: "
+ cloudEvent.getClass());
}
}
Loading

0 comments on commit f641cb1

Please sign in to comment.