|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 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.Map; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import org.springframework.cloud.function.context.config.SmartCompositeMessageConverter; |
| 23 | +import org.springframework.messaging.Message; |
| 24 | +import org.springframework.messaging.MessageHeaders; |
| 25 | +import org.springframework.messaging.converter.CompositeMessageConverter; |
| 26 | +import org.springframework.messaging.converter.ContentTypeResolver; |
| 27 | +import org.springframework.messaging.converter.DefaultContentTypeResolver; |
| 28 | +import org.springframework.messaging.support.MessageBuilder; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import org.springframework.util.MimeType; |
| 31 | +import org.springframework.util.MimeTypeUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * A Cloud Events specific pre-processor that is added to {@link SmartCompositeMessageConverter} |
| 35 | + * to potentially modify incoming message. |
| 36 | + * <br><br> |
| 37 | + * For Cloud Event coming in binary-mode such modification implies determining |
| 38 | + * content type of the 'data' attribute (see {@link #getDataContentType(MessageHeaders)} |
| 39 | + * of Cloud Event and creating a new {@link Message} with its `contentType` set to such |
| 40 | + * content type while copying the rest of the headers. |
| 41 | + * <br><br> |
| 42 | + * Similar to Cloud Event coming in binary-mode, the Cloud Event coming in structured-mode |
| 43 | + * such modification also implies determining content type of the 'data' attribute |
| 44 | + * (see {@link #getDataContentType(MessageHeaders)}... |
| 45 | + * |
| 46 | + * @author Oleg Zhurakousky |
| 47 | + * @since 3.1 |
| 48 | + */ |
| 49 | +public class CloudEventDataContentTypeMessagePreProcessor implements Function<Message<?>, Message<?>> { |
| 50 | + |
| 51 | + private final ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver(); |
| 52 | + |
| 53 | + private final MimeType cloudEventContentType = MimeTypeUtils.parseMimeType("application/cloudevents"); |
| 54 | + |
| 55 | + private final CompositeMessageConverter messageConverter; |
| 56 | + |
| 57 | + public CloudEventDataContentTypeMessagePreProcessor(CompositeMessageConverter messageConverter) { |
| 58 | + Assert.notNull(messageConverter, "'messageConverter' must not be null"); |
| 59 | + this.messageConverter = messageConverter; |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + @Override |
| 64 | + public Message<?> apply(Message<?> inputMessage) { |
| 65 | + if (CloudEventUtils.isBinary(inputMessage)) { |
| 66 | + String dataContentType = this.getDataContentType(inputMessage.getHeaders()); |
| 67 | + Message<?> message = MessageBuilder.fromMessage(inputMessage) |
| 68 | + .setHeader(MessageHeaders.CONTENT_TYPE, dataContentType) |
| 69 | +// .setHeader("originalContentType", inputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE)) not sure about it |
| 70 | + .build(); |
| 71 | + return message; |
| 72 | + } |
| 73 | + else if (this.isStructured(inputMessage)) { |
| 74 | + MimeType contentType = this.contentTypeResolver.resolve(inputMessage.getHeaders()); |
| 75 | + String dataContentType = this.getDataContentType(inputMessage.getHeaders()); |
| 76 | + String suffix = contentType.getSubtypeSuffix(); |
| 77 | + MimeType cloudEventDeserializationContentType = MimeTypeUtils |
| 78 | + .parseMimeType(contentType.getType() + "/" + suffix); |
| 79 | + Message<?> cloudEventMessage = MessageBuilder.fromMessage(inputMessage) |
| 80 | + .setHeader(MessageHeaders.CONTENT_TYPE, cloudEventDeserializationContentType) |
| 81 | + .setHeader(CloudEventUtils.CE_DATACONTENTTYPE, dataContentType).build(); |
| 82 | + Map<String, Object> structuredCloudEvent = (Map<String, Object>) this.messageConverter |
| 83 | + .fromMessage(cloudEventMessage, Map.class); |
| 84 | + Message<?> binaryCeMessage = this.buildCeMessageFromStructured(structuredCloudEvent); |
| 85 | + return binaryCeMessage; |
| 86 | + } |
| 87 | + else { |
| 88 | + return inputMessage; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private Message<?> buildCeMessageFromStructured(Map<String, Object> structuredCloudEvent) { |
| 93 | + MessageBuilder<?> builder = MessageBuilder.withPayload(structuredCloudEvent.get(CloudEventUtils.DATA)); |
| 94 | + structuredCloudEvent.remove(CloudEventUtils.DATA); |
| 95 | + builder.copyHeaders(structuredCloudEvent); |
| 96 | + return builder.build(); |
| 97 | + } |
| 98 | + |
| 99 | + private String getDataContentType(MessageHeaders headers) { |
| 100 | + if (headers.containsKey(CloudEventUtils.DATACONTENTTYPE)) { |
| 101 | + return (String) headers.get(CloudEventUtils.DATACONTENTTYPE); |
| 102 | + } |
| 103 | + else if (headers.containsKey(CloudEventUtils.CE_DATACONTENTTYPE)) { |
| 104 | + return (String) headers.get(CloudEventUtils.CE_DATACONTENTTYPE); |
| 105 | + } |
| 106 | + else if (headers.containsKey(MessageHeaders.CONTENT_TYPE)) { |
| 107 | + return headers.get(MessageHeaders.CONTENT_TYPE).toString(); |
| 108 | + } |
| 109 | + return "application/json"; |
| 110 | + } |
| 111 | + |
| 112 | + private boolean isStructured(Message<?> message) { |
| 113 | + if (!CloudEventUtils.isBinary(message)) { |
| 114 | + Map<String, Object> headers = message.getHeaders(); |
| 115 | + |
| 116 | + if (headers.containsKey(MessageHeaders.CONTENT_TYPE)) { |
| 117 | + MimeType contentType = this.contentTypeResolver.resolve(message.getHeaders()); |
| 118 | + if (contentType.getType().equals(this.cloudEventContentType.getType()) |
| 119 | + && contentType.getSubtype().startsWith(this.cloudEventContentType.getSubtype())) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | +} |
0 commit comments