|
| 1 | +/* |
| 2 | + * Copyright 2015-2018 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 | + * http://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 io.rsocket.metadata; |
| 18 | + |
| 19 | +import static io.rsocket.metadata.CompositeMetadataFlyweight.computeNextEntryIndex; |
| 20 | +import static io.rsocket.metadata.CompositeMetadataFlyweight.decodeMimeAndContentBuffersSlices; |
| 21 | +import static io.rsocket.metadata.CompositeMetadataFlyweight.decodeMimeIdFromMimeBuffer; |
| 22 | +import static io.rsocket.metadata.CompositeMetadataFlyweight.decodeMimeTypeFromMimeBuffer; |
| 23 | +import static io.rsocket.metadata.CompositeMetadataFlyweight.hasEntry; |
| 24 | +import static io.rsocket.metadata.CompositeMetadataFlyweight.isWellKnownMimeType; |
| 25 | + |
| 26 | +import io.netty.buffer.ByteBuf; |
| 27 | +import io.netty.buffer.ByteBufAllocator; |
| 28 | +import io.netty.buffer.CompositeByteBuf; |
| 29 | +import io.rsocket.metadata.CompositeMetadata.Entry; |
| 30 | +import java.util.Iterator; |
| 31 | +import reactor.util.annotation.Nullable; |
| 32 | + |
| 33 | +/** |
| 34 | + * An {@link Iterable} wrapper around a {@link ByteBuf} that exposes metadata entry information at |
| 35 | + * each decoding step. This is only possible on frame types used to initiate interactions, if the |
| 36 | + * SETUP metadata mime type was {@link WellKnownMimeType#MESSAGE_RSOCKET_COMPOSITE_METADATA}. |
| 37 | + * |
| 38 | + * <p>This allows efficient incremental decoding of the entries (without moving the source's {@link |
| 39 | + * io.netty.buffer.ByteBuf#readerIndex()}). The buffer is assumed to contain just enough bytes to |
| 40 | + * represent one or more entries (mime type compressed or not). The decoding stops when the buffer |
| 41 | + * reaches 0 readable bytes, and fails if it contains bytes but not enough to correctly decode an |
| 42 | + * entry. |
| 43 | + * |
| 44 | + * <p>A note on future-proofness: it is possible to come across a compressed mime type that this |
| 45 | + * implementation doesn't recognize. This is likely to be due to the use of a byte id that is merely |
| 46 | + * reserved in this implementation, but maps to a {@link WellKnownMimeType} in the implementation |
| 47 | + * that encoded the metadata. This can be detected by detecting that an entry is a {@link |
| 48 | + * ReservedMimeTypeEntry}. In this case {@link Entry#getMimeType()} will return {@code null}. The |
| 49 | + * encoded id can be retrieved using {@link ReservedMimeTypeEntry#getType()}. The byte and content |
| 50 | + * buffer should be kept around and re-encoded using {@link |
| 51 | + * CompositeMetadataFlyweight#encodeAndAddMetadata(CompositeByteBuf, ByteBufAllocator, byte, |
| 52 | + * ByteBuf)} in case passing that entry through is required. |
| 53 | + */ |
| 54 | +public final class CompositeMetadata implements Iterable<Entry> { |
| 55 | + |
| 56 | + private final boolean retainSlices; |
| 57 | + |
| 58 | + private final ByteBuf source; |
| 59 | + |
| 60 | + public CompositeMetadata(ByteBuf source, boolean retainSlices) { |
| 61 | + this.source = source; |
| 62 | + this.retainSlices = retainSlices; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Iterator<Entry> iterator() { |
| 67 | + return new Iterator<Entry>() { |
| 68 | + |
| 69 | + private int entryIndex = 0; |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean hasNext() { |
| 73 | + return hasEntry(CompositeMetadata.this.source, this.entryIndex); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Entry next() { |
| 78 | + ByteBuf[] headerAndData = |
| 79 | + decodeMimeAndContentBuffersSlices( |
| 80 | + CompositeMetadata.this.source, |
| 81 | + this.entryIndex, |
| 82 | + CompositeMetadata.this.retainSlices); |
| 83 | + |
| 84 | + ByteBuf header = headerAndData[0]; |
| 85 | + ByteBuf data = headerAndData[1]; |
| 86 | + |
| 87 | + this.entryIndex = computeNextEntryIndex(this.entryIndex, header, data); |
| 88 | + |
| 89 | + if (!isWellKnownMimeType(header)) { |
| 90 | + CharSequence typeString = decodeMimeTypeFromMimeBuffer(header); |
| 91 | + if (typeString == null) { |
| 92 | + throw new IllegalStateException("MIME type cannot be null"); |
| 93 | + } |
| 94 | + |
| 95 | + return new ExplicitMimeTimeEntry(data, typeString.toString()); |
| 96 | + } |
| 97 | + |
| 98 | + byte id = decodeMimeIdFromMimeBuffer(header); |
| 99 | + WellKnownMimeType type = WellKnownMimeType.fromIdentifier(id); |
| 100 | + |
| 101 | + if (WellKnownMimeType.UNKNOWN_RESERVED_MIME_TYPE == type) { |
| 102 | + return new ReservedMimeTypeEntry(data, id); |
| 103 | + } |
| 104 | + |
| 105 | + return new WellKnownMimeTypeEntry(data, type); |
| 106 | + } |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + /** An entry in the {@link CompositeMetadata}. */ |
| 111 | + public interface Entry { |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns the un-decoded content of the {@link Entry}. |
| 115 | + * |
| 116 | + * @return the un-decoded content of the {@link Entry} |
| 117 | + */ |
| 118 | + ByteBuf getContent(); |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the MIME type of the entry, if it can be decoded. |
| 122 | + * |
| 123 | + * @return the MIME type of the entry, if it can be decoded, otherwise {@code null}. |
| 124 | + */ |
| 125 | + @Nullable |
| 126 | + String getMimeType(); |
| 127 | + } |
| 128 | + |
| 129 | + /** An {@link Entry} backed by an explicitly declared MIME type. */ |
| 130 | + public static final class ExplicitMimeTimeEntry implements Entry { |
| 131 | + |
| 132 | + private final ByteBuf content; |
| 133 | + |
| 134 | + private final String type; |
| 135 | + |
| 136 | + public ExplicitMimeTimeEntry(ByteBuf content, String type) { |
| 137 | + this.content = content; |
| 138 | + this.type = type; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public ByteBuf getContent() { |
| 143 | + return this.content; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public String getMimeType() { |
| 148 | + return this.type; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * An {@link Entry} backed by a {@link WellKnownMimeType} entry, but one that is not understood by |
| 154 | + * this implementation. |
| 155 | + */ |
| 156 | + public static final class ReservedMimeTypeEntry implements Entry { |
| 157 | + private final ByteBuf content; |
| 158 | + private final int type; |
| 159 | + |
| 160 | + public ReservedMimeTypeEntry(ByteBuf content, int type) { |
| 161 | + this.content = content; |
| 162 | + this.type = type; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public ByteBuf getContent() { |
| 167 | + return this.content; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * {@inheritDoc} Since this entry represents a compressed id that couldn't be decoded, this is |
| 172 | + * always {@code null}. |
| 173 | + */ |
| 174 | + @Override |
| 175 | + public String getMimeType() { |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Returns the reserved, but unknown {@link WellKnownMimeType} for this entry. Range is 0-127 |
| 181 | + * (inclusive). |
| 182 | + * |
| 183 | + * @return the reserved, but unknown {@link WellKnownMimeType} for this entry |
| 184 | + */ |
| 185 | + public int getType() { |
| 186 | + return this.type; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + /** An {@link Entry} backed by a {@link WellKnownMimeType}. */ |
| 191 | + public static final class WellKnownMimeTypeEntry implements Entry { |
| 192 | + |
| 193 | + private final ByteBuf content; |
| 194 | + private final WellKnownMimeType type; |
| 195 | + |
| 196 | + public WellKnownMimeTypeEntry(ByteBuf content, WellKnownMimeType type) { |
| 197 | + this.content = content; |
| 198 | + this.type = type; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public ByteBuf getContent() { |
| 203 | + return this.content; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public String getMimeType() { |
| 208 | + return this.type.getString(); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Returns the {@link WellKnownMimeType} for this entry. |
| 213 | + * |
| 214 | + * @return the {@link WellKnownMimeType} for this entry |
| 215 | + */ |
| 216 | + public WellKnownMimeType getType() { |
| 217 | + return this.type; |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments