|
| 1 | +/* |
| 2 | + * Copyright 2002-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 | +package org.springframework.messaging.rsocket.annotation.support; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.BiConsumer; |
| 22 | + |
| 23 | +import io.netty.buffer.ByteBuf; |
| 24 | +import io.rsocket.Payload; |
| 25 | +import io.rsocket.metadata.CompositeMetadata; |
| 26 | + |
| 27 | +import org.springframework.core.ParameterizedTypeReference; |
| 28 | +import org.springframework.core.ResolvableType; |
| 29 | +import org.springframework.core.codec.Decoder; |
| 30 | +import org.springframework.core.io.buffer.DataBuffer; |
| 31 | +import org.springframework.core.io.buffer.DataBufferFactory; |
| 32 | +import org.springframework.core.io.buffer.NettyDataBufferFactory; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | +import org.springframework.messaging.rsocket.RSocketStrategies; |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.util.MimeType; |
| 37 | + |
| 38 | +/** |
| 39 | + * Default {@link MetadataExtractor} implementation that relies on {@link Decoder}s |
| 40 | + * to deserialize the content of metadata entries. |
| 41 | + * |
| 42 | + * <p>By default only {@code "message/x.rsocket.routing.v0""} is extracted and |
| 43 | + * saved under {@link MetadataExtractor#ROUTE_KEY}. Use the |
| 44 | + * {@code metadataToExtract} methods to specify other metadata mime types of |
| 45 | + * interest to extract. |
| 46 | + * |
| 47 | + * @author Rossen Stoyanchev |
| 48 | + * @since 5.2 |
| 49 | + */ |
| 50 | +public class DefaultMetadataExtractor implements MetadataExtractor { |
| 51 | + |
| 52 | + private final RSocketStrategies rsocketStrategies; |
| 53 | + |
| 54 | + private final Map<String, EntryProcessor<?>> entryProcessors = new HashMap<>(); |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * Default constructor with {@link RSocketStrategies}. |
| 59 | + */ |
| 60 | + public DefaultMetadataExtractor(RSocketStrategies strategies) { |
| 61 | + Assert.notNull(strategies, "RSocketStrategies is required"); |
| 62 | + this.rsocketStrategies = strategies; |
| 63 | + // TODO: remove when rsocket-core API available |
| 64 | + metadataToExtract(MessagingRSocket.ROUTING, String.class, ROUTE_KEY); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Decode metadata entries with the given {@link MimeType} to the specified |
| 70 | + * target class, and store the decoded value in the output map under the |
| 71 | + * given name. |
| 72 | + * @param mimeType the mime type of metadata entries to extract |
| 73 | + * @param targetType the target value type to decode to |
| 74 | + * @param name assign a name for the decoded value; if not provided, then |
| 75 | + * the mime type is used as the key |
| 76 | + */ |
| 77 | + public void metadataToExtract( |
| 78 | + MimeType mimeType, Class<?> targetType, @Nullable String name) { |
| 79 | + |
| 80 | + String key = name != null ? name : mimeType.toString(); |
| 81 | + metadataToExtract(mimeType, targetType, (value, map) -> map.put(key, value)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Variant of {@link #metadataToExtract(MimeType, Class, String)} that accepts |
| 86 | + * {@link ParameterizedTypeReference} instead of {@link Class} for |
| 87 | + * specifying a target type with generic parameters. |
| 88 | + */ |
| 89 | + public void metadataToExtract( |
| 90 | + MimeType mimeType, ParameterizedTypeReference<?> targetType, @Nullable String name) { |
| 91 | + |
| 92 | + String key = name != null ? name : mimeType.toString(); |
| 93 | + metadataToExtract(mimeType, targetType, (value, map) -> map.put(key, value)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Variant of {@link #metadataToExtract(MimeType, Class, String)} that allows |
| 98 | + * custom logic to be used to map the decoded value to any number of values |
| 99 | + * in the output map. |
| 100 | + * @param mimeType the mime type of metadata entries to extract |
| 101 | + * @param targetType the target value type to decode to |
| 102 | + * @param mapper custom logic to add the decoded value to the output map |
| 103 | + * @param <T> the target value type |
| 104 | + */ |
| 105 | + public <T> void metadataToExtract( |
| 106 | + MimeType mimeType, Class<T> targetType, |
| 107 | + BiConsumer<T, Map<String, Object>> mapper) { |
| 108 | + |
| 109 | + EntryProcessor<T> spec = new EntryProcessor<>(mimeType, targetType, mapper); |
| 110 | + this.entryProcessors.put(mimeType.toString(), spec); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Variant of {@link #metadataToExtract(MimeType, Class, BiConsumer)} that |
| 115 | + * accepts {@link ParameterizedTypeReference} instead of {@link Class} for |
| 116 | + * specifying a target type with generic parameters. |
| 117 | + * @param mimeType the mime type of metadata entries to extract |
| 118 | + * @param targetType the target value type to decode to |
| 119 | + * @param mapper custom logic to add the decoded value to the output map |
| 120 | + * @param <T> the target value type |
| 121 | + */ |
| 122 | + public <T> void metadataToExtract( |
| 123 | + MimeType mimeType, ParameterizedTypeReference<T> targetType, |
| 124 | + BiConsumer<T, Map<String, Object>> mapper) { |
| 125 | + |
| 126 | + EntryProcessor<T> spec = new EntryProcessor<>(mimeType, targetType, mapper); |
| 127 | + this.entryProcessors.put(mimeType.toString(), spec); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + @Override |
| 132 | + public Map<String, Object> extract(Payload payload, MimeType metadataMimeType) { |
| 133 | + Map<String, Object> result = new HashMap<>(); |
| 134 | + if (metadataMimeType.equals(MessagingRSocket.COMPOSITE_METADATA)) { |
| 135 | + for (CompositeMetadata.Entry entry : new CompositeMetadata(payload.metadata(), false)) { |
| 136 | + processEntry(entry.getContent(), entry.getMimeType(), result); |
| 137 | + } |
| 138 | + } |
| 139 | + else { |
| 140 | + processEntry(payload.metadata(), metadataMimeType.toString(), result); |
| 141 | + } |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + private void processEntry(ByteBuf content, @Nullable String mimeType, Map<String, Object> result) { |
| 146 | + EntryProcessor<?> entryProcessor = this.entryProcessors.get(mimeType); |
| 147 | + if (entryProcessor != null) { |
| 148 | + content.retain(); |
| 149 | + entryProcessor.process(content, result); |
| 150 | + return; |
| 151 | + } |
| 152 | + if (MessagingRSocket.ROUTING.toString().equals(mimeType)) { |
| 153 | + // TODO: use rsocket-core API when available |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + /** |
| 159 | + * Helps to decode a metadata entry and add the resulting value to the |
| 160 | + * output map. |
| 161 | + */ |
| 162 | + private class EntryProcessor<T> { |
| 163 | + |
| 164 | + private final MimeType mimeType; |
| 165 | + |
| 166 | + private final ResolvableType targetType; |
| 167 | + |
| 168 | + private final BiConsumer<T, Map<String, Object>> accumulator; |
| 169 | + |
| 170 | + private final Decoder<T> decoder; |
| 171 | + |
| 172 | + |
| 173 | + public EntryProcessor( |
| 174 | + MimeType mimeType, Class<T> targetType, |
| 175 | + BiConsumer<T, Map<String, Object>> accumulator) { |
| 176 | + |
| 177 | + this(mimeType, ResolvableType.forClass(targetType), accumulator); |
| 178 | + } |
| 179 | + |
| 180 | + public EntryProcessor( |
| 181 | + MimeType mimeType, ParameterizedTypeReference<T> targetType, |
| 182 | + BiConsumer<T, Map<String, Object>> accumulator) { |
| 183 | + |
| 184 | + this(mimeType, ResolvableType.forType(targetType), accumulator); |
| 185 | + } |
| 186 | + |
| 187 | + private EntryProcessor( |
| 188 | + MimeType mimeType, ResolvableType targetType, |
| 189 | + BiConsumer<T, Map<String, Object>> accumulator) { |
| 190 | + |
| 191 | + this.mimeType = mimeType; |
| 192 | + this.targetType = targetType; |
| 193 | + this.accumulator = accumulator; |
| 194 | + this.decoder = rsocketStrategies.decoder(targetType, mimeType); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + public void process(ByteBuf byteBuf, Map<String, Object> result) { |
| 199 | + DataBufferFactory factory = rsocketStrategies.dataBufferFactory(); |
| 200 | + DataBuffer buffer = factory instanceof NettyDataBufferFactory ? |
| 201 | + ((NettyDataBufferFactory) factory).wrap(byteBuf) : |
| 202 | + factory.wrap(byteBuf.nioBuffer()); |
| 203 | + |
| 204 | + T value = this.decoder.decode(buffer, this.targetType, this.mimeType, Collections.emptyMap()); |
| 205 | + this.accumulator.accept(value, result); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments