|
| 1 | +/* |
| 2 | + * Copyright 2019-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.function.cloudevent; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.util.StringUtils; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * |
| 27 | + * @author Oleg Zhurakousky |
| 28 | + * @since 3.1 |
| 29 | + */ |
| 30 | +public class CloudEventAttributesHelper extends HashMap<String, Object> { |
| 31 | + |
| 32 | + /** |
| 33 | + * |
| 34 | + */ |
| 35 | + private static final long serialVersionUID = 5393610770855366497L; |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + CloudEventAttributesHelper(Map<String, Object> headers) { |
| 40 | + super(headers); |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings("unchecked") |
| 44 | + public <A> A getId() { |
| 45 | + if (this.containsKey(CloudEventMessageUtils.CANONICAL_ID)) { |
| 46 | + return (A) this.get(CloudEventMessageUtils.CANONICAL_ID); |
| 47 | + } |
| 48 | + else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID)) { |
| 49 | + return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID); |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + String getAttributeName(String attributeName) { |
| 55 | + if (this.containsKey(CloudEventMessageUtils.ATTR_PREFIX + attributeName)) { |
| 56 | + return CloudEventMessageUtils.ATTR_PREFIX + attributeName; |
| 57 | + } |
| 58 | + else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + attributeName)) { |
| 59 | + return CloudEventMessageUtils.HTTP_ATTR_PREFIX + attributeName; |
| 60 | + } |
| 61 | + return attributeName; |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressWarnings("unchecked") |
| 65 | + public <A> A getSource() { |
| 66 | + if (this.containsKey(CloudEventMessageUtils.CANONICAL_SOURCE)) { |
| 67 | + return (A) this.get(CloudEventMessageUtils.CANONICAL_SOURCE); |
| 68 | + } |
| 69 | + else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE)) { |
| 70 | + return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE); |
| 71 | + } |
| 72 | + return (A) this.get(CloudEventMessageUtils.SOURCE); |
| 73 | + } |
| 74 | + |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + public <A> A getSpecversion() { |
| 77 | + if (this.containsKey(CloudEventMessageUtils.CANONICAL_SPECVERSION)) { |
| 78 | + return (A) this.get(CloudEventMessageUtils.CANONICAL_SPECVERSION); |
| 79 | + } |
| 80 | + else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SPECVERSION)) { |
| 81 | + return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SPECVERSION); |
| 82 | + } |
| 83 | + return (A) this.get(CloudEventMessageUtils.SPECVERSION); |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + public <A> A getType() { |
| 88 | + if (this.containsKey(CloudEventMessageUtils.CANONICAL_TYPE)) { |
| 89 | + return (A) this.get(CloudEventMessageUtils.CANONICAL_TYPE); |
| 90 | + } |
| 91 | + else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE)) { |
| 92 | + return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE); |
| 93 | + } |
| 94 | + return (A) this.get(CloudEventMessageUtils.TYPE); |
| 95 | + } |
| 96 | + |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public <A> A getDataContentType() { |
| 99 | + Object dataContentType; |
| 100 | + if (this.containsKey(CloudEventMessageUtils.CANONICAL_DATACONTENTTYPE)) { |
| 101 | + dataContentType = this.get(CloudEventMessageUtils.CANONICAL_DATACONTENTTYPE); |
| 102 | + } |
| 103 | + else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATACONTENTTYPE)) { |
| 104 | + dataContentType = this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATACONTENTTYPE); |
| 105 | + } |
| 106 | + dataContentType = this.get(CloudEventMessageUtils.DATACONTENTTYPE); |
| 107 | + return (A) dataContentType; |
| 108 | + } |
| 109 | + |
| 110 | + public void setDataContentType(String datacontenttype) { |
| 111 | + this.put(CloudEventMessageUtils.CANONICAL_DATACONTENTTYPE, datacontenttype); |
| 112 | + } |
| 113 | + |
| 114 | + @SuppressWarnings("unchecked") |
| 115 | + public <A> A getAtttribute(String name) { |
| 116 | + return (A) this.get(name); |
| 117 | + } |
| 118 | + |
| 119 | + public boolean isValidCloudEvent() { |
| 120 | + return StringUtils.hasText(this.getId()) |
| 121 | + && StringUtils.hasText(this.getSource()) |
| 122 | + && StringUtils.hasText(this.getSpecversion()) |
| 123 | + && StringUtils.hasText(this.getType()); |
| 124 | + } |
| 125 | +} |
0 commit comments