|
| 1 | +/* |
| 2 | + * Copyright 2015-Present 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.router; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.rsocket.frame.FrameType; |
| 21 | +import io.rsocket.metadata.CompositeMetadata; |
| 22 | +import io.rsocket.metadata.CompositeMetadata.Entry; |
| 23 | +import io.rsocket.metadata.CompositeMetadata.WellKnownMimeTypeEntry; |
| 24 | +import io.rsocket.metadata.RoutingMetadata; |
| 25 | +import io.rsocket.metadata.WellKnownMimeType; |
| 26 | +import reactor.util.annotation.Nullable; |
| 27 | + |
| 28 | +public class CompositeMetadataRouteCodec implements RouteCodec { |
| 29 | + |
| 30 | + @Override |
| 31 | + @Nullable |
| 32 | + public Route decode(ByteBuf metadataByteBuf, FrameType requestType) { |
| 33 | + final CompositeMetadata compositeMetadata = new CompositeMetadata(metadataByteBuf, false); |
| 34 | + |
| 35 | + String route = null; |
| 36 | + String mimeType = null; |
| 37 | + |
| 38 | + for (Entry compositeMetadatum : compositeMetadata) { |
| 39 | + if (compositeMetadatum instanceof WellKnownMimeTypeEntry) { |
| 40 | + final WellKnownMimeTypeEntry wellKnownMimeTypeEntry = |
| 41 | + (WellKnownMimeTypeEntry) compositeMetadatum; |
| 42 | + final WellKnownMimeType type = wellKnownMimeTypeEntry.getType(); |
| 43 | + |
| 44 | + if (type == WellKnownMimeType.MESSAGE_RSOCKET_ROUTING) { |
| 45 | + final RoutingMetadata routingMetadata = |
| 46 | + new RoutingMetadata(compositeMetadatum.getContent()); |
| 47 | + for (String routeEntry : routingMetadata) { |
| 48 | + route = routeEntry; |
| 49 | + break; |
| 50 | + } |
| 51 | + } else if (type == WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE) { |
| 52 | + // FIXME: once codecs are available |
| 53 | + // mimeType = compositeMetadatum |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (route != null) { |
| 59 | + return new Route(requestType, route, mimeType); |
| 60 | + } |
| 61 | + |
| 62 | + return null; |
| 63 | + } |
| 64 | +} |
0 commit comments