-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…e mapping Added CloudEventAttributesProvider and default implementation Added CloudEventMessageUtils
- Loading branch information
Showing
12 changed files
with
438 additions
and
69 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...ext/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2019-2019 the original author or 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 | ||
* | ||
* https://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 org.springframework.cloud.function.cloudevent; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
* | ||
* @author Oleg Zhurakousky | ||
* @since 3.1 | ||
*/ | ||
public class CloudEventAttributes extends HashMap<String, Object> { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 5393610770855366497L; | ||
|
||
|
||
|
||
CloudEventAttributes(Map<String, Object> headers) { | ||
super(headers); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <A> A getId() { | ||
return this.containsKey(CloudEventMessageUtils.CE_ID) | ||
? (A) this.get(CloudEventMessageUtils.CE_ID) | ||
: (A) this.get(CloudEventMessageUtils.ID); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <A> A getSource() { | ||
return this.containsKey(CloudEventMessageUtils.CE_SOURCE) | ||
? (A) this.get(CloudEventMessageUtils.CE_SOURCE) | ||
: (A) this.get(CloudEventMessageUtils.SOURCE); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <A> A getSpecversion() { | ||
return this.containsKey(CloudEventMessageUtils.CE_SPECVERSION) | ||
? (A) this.get(CloudEventMessageUtils.CE_SPECVERSION) | ||
: (A) this.get(CloudEventMessageUtils.SPECVERSION); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <A> A getType() { | ||
return this.containsKey(CloudEventMessageUtils.CE_TYPE) | ||
? (A) this.get(CloudEventMessageUtils.CE_TYPE) | ||
: (A) this.get(CloudEventMessageUtils.TYPE); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <A> A getDataContentType() { | ||
return this.containsKey(CloudEventMessageUtils.CE_DATACONTENTTYPE) | ||
? (A) this.get(CloudEventMessageUtils.CE_DATACONTENTTYPE) | ||
: (A) this.get(CloudEventMessageUtils.DATACONTENTTYPE); | ||
} | ||
|
||
public void setDataContentType(String datacontenttype) { | ||
this.put(CloudEventMessageUtils.CE_DATACONTENTTYPE, datacontenttype); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <A> A getAtttribute(String name) { | ||
return (A) this.get(name); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ain/java/org/springframework/cloud/function/cloudevent/CloudEventAtttributesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2020-2020 the original author or 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 | ||
* | ||
* https://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 org.springframework.cloud.function.cloudevent; | ||
|
||
import org.springframework.messaging.MessageHeaders; | ||
|
||
/** | ||
* | ||
* @author Oleg Zhurakousky | ||
* @since 3.1 | ||
*/ | ||
public interface CloudEventAtttributesProvider { | ||
|
||
/** | ||
* Will construct instance of {@link CloudEventAttributes} setting its required attributes. | ||
* | ||
* @param ce_id value for Cloud Event 'id' attribute | ||
* @param ce_specversion value for Cloud Event 'specversion' attribute | ||
* @param ce_source value for Cloud Event 'source' attribute | ||
* @param ce_type value for Cloud Event 'type' attribute | ||
* @return instance of {@link CloudEventAttributes} | ||
*/ | ||
CloudEventAttributes get(String ce_id, String ce_specversion, String ce_source, String ce_type); | ||
|
||
/** | ||
* Will construct instance of {@link CloudEventAttributes} | ||
* Should default/generate cloud event ID and SPECVERSION. | ||
* | ||
* @param ce_source value for Cloud Event 'source' attribute | ||
* @param ce_type value for Cloud Event 'type' attribute | ||
* @return instance of {@link CloudEventAttributes} | ||
*/ | ||
CloudEventAttributes get(String ce_source, String ce_type); | ||
|
||
|
||
/** | ||
* Will construct instance of {@link CloudEventAttributes} from {@link MessageHeaders}. | ||
* | ||
* Should copy Cloud Event related headers into an instance of {@link CloudEventAttributes} | ||
* NOTE: Certain headers must not be copied. | ||
* | ||
* @param headers instance of {@link MessageHeaders} | ||
* @return modifiable instance of {@link CloudEventAttributes} | ||
*/ | ||
RequiredAttributeAccessor get(MessageHeaders headers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...va/org/springframework/cloud/function/cloudevent/DefaultCloudEventAttributesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2019-2019 the original author or 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 | ||
* | ||
* https://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 org.springframework.cloud.function.cloudevent; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.springframework.messaging.MessageHeaders; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* | ||
* @author Oleg Zhurakousky | ||
* @since 3.1 | ||
* | ||
*/ | ||
public class DefaultCloudEventAttributesProvider implements CloudEventAtttributesProvider { | ||
/* | ||
* should i provide instance() method for convinience or should it be always injected into function | ||
*/ | ||
|
||
@Override | ||
public CloudEventAttributes get(String ce_id, String ce_specversion, String ce_source, String ce_type) { | ||
Assert.hasText(ce_id, "'ce_id' must not be null or empty"); | ||
Assert.hasText(ce_specversion, "'ce_specversion' must not be null or empty"); | ||
Assert.hasText(ce_source, "'ce_source' must not be null or empty"); | ||
Assert.hasText(ce_type, "'ce_type' must not be null or empty"); | ||
Map<String, Object> requiredAttributes = new HashMap<>(); | ||
requiredAttributes.put(CloudEventMessageUtils.CE_ID, ce_id); | ||
requiredAttributes.put(CloudEventMessageUtils.CE_SPECVERSION, ce_specversion); | ||
requiredAttributes.put(CloudEventMessageUtils.CE_SOURCE, ce_source); | ||
requiredAttributes.put(CloudEventMessageUtils.CE_TYPE, ce_type); | ||
return new CloudEventAttributes(requiredAttributes); | ||
} | ||
|
||
@Override | ||
public CloudEventAttributes get(String ce_source, String ce_type) { | ||
return this.get(UUID.randomUUID().toString(), "1.0", ce_source, ce_type); | ||
} | ||
|
||
/** | ||
* By default it will copy all the headers while exposing accessor to allow user to modify any of them. | ||
*/ | ||
@Override | ||
public RequiredAttributeAccessor get(MessageHeaders headers) { | ||
return new RequiredAttributeAccessor(headers); | ||
} | ||
|
||
} |
Oops, something went wrong.