un) {
+ this.attributes = un.unmarshal(attributesMap);
+ return this;
+ }
+
+ @Override
+ public ExtensionsMapStep data(DataUnmarshaller
un) {
+ try {
+ this.data = un.unmarshall(this.payload, this.attributes);
+ return this;
+ }catch(Exception e) {
+ throw new RuntimeException(e.getMessage(), e);
+ }
+ }
+
+ @Override
+ public ExtensionsStepBegin
extensions(BinaryFormatExtensionMapper mapper) {
+ this.extensionsMap = mapper.map(headers);
+ return this;
+ }
+
+ @Override
+ public ExtensionsStep
begin() {
+ return this;
+ }
+
+ @Override
+ public ExtensionsStep
extension(ExtensionUmarshaller un) {
+ Optional ef = un.unmarshal(extensionsMap);
+ ef.ifPresent((value) ->{
+ this.extensions.add(value);
+ });
+ return this;
+ }
+
+ @Override
+ public Build end() {
+ return this;
+ }
+
+ @Override
+ public CloudEvent build(EventBuilder builder) {
+ return builder.build(data, attributes, extensions);
+ }
+
+ }
+
+ public static void main(String[] args) throws Exception {
+
+ Map myHeaders = new HashMap<>();
+ myHeaders.put("ce-id", "0x11");
+ myHeaders.put("ce-source", "/source");
+ myHeaders.put("ce-specversion", "0.3");
+ myHeaders.put("ce-type", "br.my");
+ myHeaders.put("ce-time", "2019-09-16T20:49:00Z");
+ myHeaders.put("ce-schemaurl", "http://my.br");
+ myHeaders.put("my-ext", "my-custom extension");
+ myHeaders.put("traceparent", "0");
+ myHeaders.put("tracestate", "congo=4");
+ myHeaders.put("Content-Type", "application/json");
+
+ String myPayload = "{\"foo\" : \"rocks\", \"name\" : \"jocker\"}";
+
+ //String payload = "{\"id\":\"0x19\",\"type\":\"aws.s3.object.created\",\"time\":\"2018-04-26T14:48:09.769Z\",\"source\":\"/source\",\"contenttype\":\"application/json\",\"specversion\":\"0.2\"}";
+
+ ObjectMapper mapper = new ObjectMapper();
+ System.out.println(mapper.writeValueAsString(new Dummy()));
+
+ CloudEvent event = BinaryUnmashaller.
+ builder()
+ .withHeaders(() -> myHeaders)
+ .withPayload(() -> myPayload)
+ .attributes(new BinaryFormatAttributeMapperImpl()::map)
+ .attributes(AttributesImpl.unmarshaller()::unmarshal)
+ .data((payload, attributes) -> {
+ return mapper.readValue(payload, Dummy.class);
+ })
+ .extensions(new BinaryFormatExtensionMapperImpl()::map)
+ .begin()
+ .extension((exts) -> {
+ String key = "my-ext";
+ String value = exts.get(key);
+
+ ExtensionFormat result = null;
+ if(null!= value) {
+ result = ExtensionFormat.of(InMemoryFormat
+ .of(key, value, Object.class), key, value);
+ }
+ return Optional.ofNullable(result);
+ })
+ .extension((exts) -> {
+ String traceparent = exts.get("traceparent");
+ String tracestate = exts.get("tracestate");
+
+ if(null!= traceparent && null!= tracestate) {
+ DistributedTracingExtension dte = new DistributedTracingExtension();
+ dte.setTraceparent(traceparent);
+ dte.setTracestate(tracestate);
+
+ InMemoryFormat inMemory =
+ InMemoryFormat.of("distributedTracing", dte, Object.class);
+
+ return Optional.of(
+ ExtensionFormat.of(inMemory,
+ new SimpleEntry<>("traceparent", traceparent),
+ new SimpleEntry<>("tracestate", tracestate))
+ );
+
+ }
+
+ return Optional.empty();
+ })
+ .end()
+ .build(CloudEventBuilder.builder()::build);
+
+ System.out.println(event.getAttributes());
+ System.out.println(event.getData());
+ System.out.println(event.getExtensions());
+ }
+}
diff --git a/api/src/main/java/io/cloudevents/format/Format.java b/api/src/main/java/io/cloudevents/format/Format.java
new file mode 100644
index 00000000..c43253cf
--- /dev/null
+++ b/api/src/main/java/io/cloudevents/format/Format.java
@@ -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.util.Map;
+import java.util.Optional;
+
+/**
+ * Represents a result of binary marshalling, to be used to wire serialization
+ *
+ * @author fabiojose
+ *
+ */
+public class Format {
+
+ private final T payload;
+ private final Map headers;
+
+ public Format(T payload, Map headers) {
+ this.payload = payload;
+ this.headers = headers;
+ }
+
+ /**
+ * The payload
+ */
+ public Optional getPayload() {
+ return Optional.ofNullable(payload);
+ }
+
+ /**
+ * The headers
+ */
+ public Map getHeaders() {
+ return headers;
+ }
+}