feat: 70 implement dataref extension (#151)
Signed-off-by: Paul Schwarz <paulsschwarz@gmail.com>
This commit is contained in:
parent
1d07d9c392
commit
815c9cff26
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright 2018-Present The CloudEvents Authors
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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.core.extensions;
|
||||
|
||||
import io.cloudevents.CloudEventExtensions;
|
||||
import io.cloudevents.Extension;
|
||||
import io.cloudevents.core.extensions.impl.ExtensionUtils;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* This extension supports the "Claim Check Pattern". It allows to specify a reference to a location where the event payload is stored.
|
||||
* @see <a href=https://github.com/cloudevents/spec/blob/v1.0/extensions/dataref.md>https://github.com/cloudevents/spec/blob/v1.0/extensions/dataref.md</a>
|
||||
*/
|
||||
public final class DatarefExtension implements Extension {
|
||||
|
||||
public static final String DATAREF = "dataref";
|
||||
private static final Set<String> KEY_SET = Collections.unmodifiableSet(new HashSet<>(Collections.singletonList(DATAREF)));
|
||||
|
||||
private URI dataref;
|
||||
|
||||
public URI getDataref() {
|
||||
return dataref;
|
||||
}
|
||||
|
||||
public void setDataref(URI dataref) {
|
||||
this.dataref = dataref;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFrom(CloudEventExtensions extensions) {
|
||||
Object value = extensions.getExtension(DATAREF);
|
||||
if (value != null) {
|
||||
this.dataref = URI.create(value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue(String key) {
|
||||
if (DATAREF.equals(key)) {
|
||||
return this.dataref;
|
||||
}
|
||||
throw ExtensionUtils.generateInvalidKeyException(this.getClass().getSimpleName(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getKeys() {
|
||||
return KEY_SET;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DatarefExtension{" +
|
||||
"dataref='" + dataref + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
return prime + ((dataref == null) ? 0 : dataref.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
DatarefExtension other = (DatarefExtension) obj;
|
||||
if (dataref == null) {
|
||||
return other.dataref == null;
|
||||
} else {
|
||||
return dataref.equals(other.dataref);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,6 +19,7 @@ package io.cloudevents.core.provider;
|
|||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.Extension;
|
||||
import io.cloudevents.core.extensions.DatarefExtension;
|
||||
import io.cloudevents.core.extensions.DistributedTracingExtension;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
@ -27,7 +28,7 @@ import java.util.function.Supplier;
|
|||
public final class ExtensionProvider {
|
||||
|
||||
private static class SingletonContainer {
|
||||
private final static ExtensionProvider INSTANCE = new ExtensionProvider();
|
||||
private static final ExtensionProvider INSTANCE = new ExtensionProvider();
|
||||
}
|
||||
|
||||
public static ExtensionProvider getInstance() {
|
||||
|
@ -40,6 +41,7 @@ public final class ExtensionProvider {
|
|||
private ExtensionProvider() {
|
||||
this.extensionFactories = new HashMap<>();
|
||||
registerExtension(DistributedTracingExtension.class, DistributedTracingExtension::new);
|
||||
registerExtension(DatarefExtension.class, DatarefExtension::new);
|
||||
}
|
||||
|
||||
public <T extends Extension> void registerExtension(Class<T> extensionClass, Supplier<Extension> factory) {
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright 2018-Present The CloudEvents Authors
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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.core.extensions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.core.builder.CloudEventBuilder;
|
||||
import io.cloudevents.core.provider.ExtensionProvider;
|
||||
import java.net.URI;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author paulschwarz
|
||||
*/
|
||||
public class DatarefExtensionTest {
|
||||
|
||||
@Test
|
||||
public void writeExtension() {
|
||||
DatarefExtension datarefExtension = new DatarefExtension();
|
||||
datarefExtension.setDataref(URI.create("http://example"));
|
||||
|
||||
CloudEvent event = CloudEventBuilder.v1()
|
||||
.withId("aaa")
|
||||
.withSource(URI.create("http://localhost"))
|
||||
.withType("example")
|
||||
.withExtension(datarefExtension)
|
||||
.build();
|
||||
|
||||
assertThat(event.getExtension(DatarefExtension.DATAREF))
|
||||
.isEqualTo(URI.create("http://example"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseExtension() {
|
||||
CloudEvent event = CloudEventBuilder.v1()
|
||||
.withId("aaa")
|
||||
.withSource(URI.create("http://localhost"))
|
||||
.withType("example")
|
||||
.withExtension(DatarefExtension.DATAREF, "http://example")
|
||||
.build();
|
||||
|
||||
DatarefExtension datarefExtension = ExtensionProvider.getInstance()
|
||||
.parseExtension(DatarefExtension.class, event);
|
||||
|
||||
assertThat(datarefExtension).isNotNull();
|
||||
assertThat(datarefExtension.getDataref()).isEqualTo(URI.create("http://example"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue