Skip to content

Commit 98dafba

Browse files
committed
Adding reactivesocket-mime-types module. (rsocket#11)
This module provides support for encoding/decoding ReactiveSocket data and metadata into using different mime types as defined by [ReactiveSocket protocol](https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md#setup-frame). The support for mime types is not comprehensive but it will atleast support the [default metadata mime type](https://github.com/ReactiveSocket/reactivesocket/blob/mimetypes/MimeTypes.md) See README.md for usage.
1 parent df1d295 commit 98dafba

25 files changed

Lines changed: 1768 additions & 1 deletion
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Overview
2+
3+
This module provides support for encoding/decoding ReactiveSocket data and metadata into using different mime types as defined by [ReactiveSocket protocol](https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md#setup-frame).
4+
The support for mime types is not comprehensive but it will at least support the [default metadata mime type](https://github.com/ReactiveSocket/reactivesocket/blob/mimetypes/MimeTypes.md)
5+
6+
## Usage
7+
8+
#### Supported Codecs
9+
10+
Supported mime types are listed as [SupportedMimeTypes](src/main/java/io/reactivesocket/mimetypes/SupportedMimeTypes.java).
11+
12+
#### Obtaining the appropriate codec
13+
14+
[MimeType](src/main/java/io/reactivesocket/mimetypes/MimeType.java) is the interface that provides different methods for encoding/decoding ReactiveSocket data and metadata.
15+
An instance of `MimeType` can be obtained via [MimeTypeFactory](src/main/java/io/reactivesocket/mimetypes/MimeTypeFactory.java).
16+
17+
A simple usage of `MimeType` is as follows:
18+
19+
```java
20+
public class ConnectionSetupHandlerImpl implements ConnectionSetupHandler {
21+
22+
@Override
23+
public RequestHandler apply(ConnectionSetupPayload setupPayload, ReactiveSocket reactiveSocket) throws SetupException {
24+
25+
final MimeType mimeType = MimeTypeFactory.from(setupPayload); // If the mime types aren't supported, throws an error.
26+
27+
return new RequestHandler() {
28+
29+
// Not a complete implementation, just a method to demonstrate usage.
30+
@Override
31+
public Publisher<Payload> handleRequestResponse(Payload payload) {
32+
// use (en/de)codeMetadata() methods to encode/decode metadata
33+
mimeType.decodeMetadata(payload.getMetadata(), KVMetadata.class);
34+
// use (en/de)codeData() methods to encode/decode data
35+
mimeType.decodeData(payload.getData(), Person.class);
36+
return PublisherUtils.empty(); // Do something useful in reality!
37+
}
38+
};
39+
}
40+
}
41+
```
42+
43+
## Build and Binaries
44+
45+
<a href='https://travis-ci.org/ReactiveSocket/reactivesocket-java/builds'><img src='https://travis-ci.org/ReactiveSocket/reactivesocket-java.svg?branch=master'></a>
46+
47+
Artifacts are available via JCenter.
48+
49+
Example:
50+
51+
```groovy
52+
repositories {
53+
maven { url 'https://jcenter.bintray.com' }
54+
}
55+
56+
dependencies {
57+
compile 'io.reactivesocket:reactivesocket-mime-types:x.y.z'
58+
}
59+
```
60+
61+
No releases to Maven Central have occurred yet.
62+
63+
64+
## Bugs and Feedback
65+
66+
For bugs, questions and discussions please use the [Github Issues](https://github.com/ReactiveSocket/reactivesocket-java-impl/issues).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016 Netflix, Inc.
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+
18+
dependencies {
19+
20+
compile 'com.fasterxml.jackson.core:jackson-core:latest.release'
21+
compile 'com.fasterxml.jackson.core:jackson-databind:latest.release'
22+
compile 'com.fasterxml.jackson.module:jackson-module-afterburner:latest.release'
23+
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:latest.release'
24+
25+
testCompile "org.hamcrest:hamcrest-library:1.3"
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.reactivesocket.mimetypes;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.charset.Charset;
5+
import java.util.Map;
6+
7+
/**
8+
* A representation of ReactiveSocket metadata as a key-value pair.
9+
*
10+
* <b>Implementations are not required to be thread-safe.</b>
11+
*/
12+
public interface KVMetadata extends Map<String, ByteBuffer> {
13+
14+
/**
15+
* Lookup the value for the passed key and return the value as a string.
16+
*
17+
* @param key To Lookup.
18+
* @param valueEncoding Encoding for the value.
19+
*
20+
* @return Value as a string with the passed {@code valueEncoding}
21+
*/
22+
String getAsString(String key, Charset valueEncoding);
23+
24+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package io.reactivesocket.mimetypes;
2+
3+
import io.reactivesocket.Frame;
4+
import org.agrona.DirectBuffer;
5+
import org.agrona.MutableDirectBuffer;
6+
7+
import java.nio.ByteBuffer;
8+
9+
/**
10+
* Encoding and decoding operations for a ReactiveSocket. Since, mime-types for data and metadata do not change once
11+
* setup, a MimeType instance can be stored per ReactiveSocket instance and can be used for repeated encode/decode of
12+
* data and metadata.
13+
*/
14+
public interface MimeType {
15+
16+
/**
17+
* Decodes metadata of the passed frame to the specified {@code clazz}.
18+
*
19+
* @param toDecode Frame for which metadata is to be decoded.
20+
* @param clazz Class to which metadata will be decoded.
21+
*
22+
* @param <T> Type of the class to which metadata will be decoded.
23+
*
24+
* @return Instance of the class post decode.
25+
*/
26+
default <T> T decodeMetadata(Frame toDecode, Class<T> clazz) {
27+
return decodeMetadata(toDecode.getMetadata(), clazz);
28+
}
29+
30+
/**
31+
* Decodes the passed buffer to the specified {@code clazz}.
32+
*
33+
* @param toDecode buffer to be decoded.
34+
* @param clazz Class to which the buffer will be decoded.
35+
*
36+
* @param <T> Type of the class to which the buffer will be decoded.
37+
*
38+
* @return Instance of the class post decode.
39+
*/
40+
<T> T decodeMetadata(ByteBuffer toDecode, Class<T> clazz);
41+
42+
/**
43+
* Decodes the passed buffer to the specified {@code clazz}.
44+
*
45+
* @param toDecode buffer to be decoded.
46+
* @param clazz Class to which the buffer will be decoded.
47+
*
48+
* @param <T> Type of the class to which the buffer will be decoded.
49+
*
50+
* @return Instance of the class post decode.
51+
*/
52+
<T> T decodeMetadata(DirectBuffer toDecode, Class<T> clazz);
53+
54+
/**
55+
* Encodes passed metadata to a buffer.
56+
*
57+
* @param toEncode Object to encode as metadata.
58+
*
59+
* @param <T> Type of the object to encode.
60+
*
61+
* @return Buffer with encoded data.
62+
*/
63+
<T> ByteBuffer encodeMetadata(T toEncode);
64+
65+
/**
66+
* Encodes passed metadata to a buffer.
67+
*
68+
* @param toEncode Object to encode as metadata.
69+
*
70+
* @param <T> Type of the object to encode.
71+
*
72+
* @return Buffer with encoded data.
73+
*/
74+
<T> DirectBuffer encodeMetadataDirect(T toEncode);
75+
76+
/**
77+
* Encodes passed metadata to the passed buffer.
78+
*
79+
* @param buffer Encodes the metadata to this buffer.
80+
* @param toEncode Metadata to encode.
81+
*
82+
* @param <T> Type of the object to encode.
83+
*/
84+
<T> void encodeMetadataTo(MutableDirectBuffer buffer, T toEncode);
85+
86+
/**
87+
* Encodes passed metadata to the passed buffer.
88+
*
89+
* @param buffer Encodes the metadata to this buffer.
90+
* @param toEncode Metadata to encode.
91+
*
92+
* @param <T> Type of the object to encode.
93+
*/
94+
<T> void encodeMetadataTo(ByteBuffer buffer, T toEncode);
95+
96+
/**
97+
* Decodes data of the passed frame to the specified {@code clazz}.
98+
*
99+
* @param toDecode Frame for which metadata is to be decoded.
100+
* @param clazz Class to which metadata will be decoded.
101+
*
102+
* @param <T> Type of the class to which metadata will be decoded.
103+
*
104+
* @return Instance of the class post decode.
105+
*/
106+
default <T> T decodeData(Frame toDecode, Class<T> clazz) {
107+
return decodeData(toDecode.getData(), clazz);
108+
}
109+
110+
/**
111+
* Decodes the passed buffer to the specified {@code clazz}.
112+
*
113+
* @param toDecode buffer to be decoded.
114+
* @param clazz Class to which the buffer will be decoded.
115+
*
116+
* @param <T> Type of the class to which the buffer will be decoded.
117+
*
118+
* @return Instance of the class post decode.
119+
*/
120+
<T> T decodeData(ByteBuffer toDecode, Class<T> clazz);
121+
122+
/**
123+
* Decodes the passed buffer to the specified {@code clazz}.
124+
*
125+
* @param toDecode buffer to be decoded.
126+
* @param clazz Class to which the buffer will be decoded.
127+
*
128+
* @param <T> Type of the class to which the buffer will be decoded.
129+
*
130+
* @return Instance of the class post decode.
131+
*/
132+
<T> T decodeData(DirectBuffer toDecode, Class<T> clazz);
133+
134+
/**
135+
* Encodes passed data to a buffer.
136+
*
137+
* @param toEncode Object to encode as data.
138+
*
139+
* @param <T> Type of the object to encode.
140+
*
141+
* @return Buffer with encoded data.
142+
*/
143+
<T> ByteBuffer encodeData(T toEncode);
144+
145+
/**
146+
* Encodes passed data to a buffer.
147+
*
148+
* @param toEncode Object to encode as data.
149+
*
150+
* @param <T> Type of the object to encode.
151+
*
152+
* @return Buffer with encoded data.
153+
*/
154+
<T> DirectBuffer encodeDataDirect(T toEncode);
155+
156+
/**
157+
* Encodes passed data to the passed buffer.
158+
*
159+
* @param buffer Encodes the data to this buffer.
160+
* @param toEncode Data to encode.
161+
*
162+
* @param <T> Type of the object to encode.
163+
*/
164+
<T> void encodeDataTo(MutableDirectBuffer buffer, T toEncode);
165+
166+
/**
167+
* Encodes passed data to the passed buffer.
168+
*
169+
* @param buffer Encodes the data to this buffer.
170+
* @param toEncode Data to encode.
171+
*
172+
* @param <T> Type of the object to encode.
173+
*/
174+
<T> void encodeDataTo(ByteBuffer buffer, T toEncode);
175+
}

0 commit comments

Comments
 (0)