Skip to content

Commit e7266de

Browse files
committed
Added back function of HeadersCodec.contentType
1 parent 0d1a092 commit e7266de

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

services-api/src/main/java/io/scalecube/services/transport/api/HeadersCodec.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
package io.scalecube.services.transport.api;
22

3+
import io.scalecube.utils.ServiceLoaderUtil;
34
import java.io.IOException;
45
import java.io.InputStream;
56
import java.io.OutputStream;
67
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
79

810
public interface HeadersCodec {
911

1012
HeadersCodec DEFAULT_INSTANCE = new JdkCodec();
1113

14+
Map<String, HeadersCodec> INSTANCES = new ConcurrentHashMap<>();
15+
16+
static HeadersCodec getInstance(String contentType) {
17+
return INSTANCES.computeIfAbsent(contentType, HeadersCodec::loadInstance);
18+
}
19+
20+
/**
21+
* Returns {@link HeadersCodec} by given {@code contentType}.
22+
*
23+
* @param contentType contentType (required)
24+
* @return {@link HeadersCodec} by given {@code contentType} (or throws IllegalArgumentException
25+
* is thrown if not exist)
26+
*/
27+
static HeadersCodec loadInstance(String contentType) {
28+
return ServiceLoaderUtil.findFirst(
29+
HeadersCodec.class, codec -> codec.contentType().equalsIgnoreCase(contentType))
30+
.orElseThrow(
31+
() ->
32+
new IllegalArgumentException(
33+
"HeadersCodec for '" + contentType + "' not configured"));
34+
}
35+
36+
String contentType();
37+
1238
void encode(OutputStream stream, Map<String, String> headers) throws IOException;
1339

1440
Map<String, String> decode(InputStream stream) throws IOException;

0 commit comments

Comments
 (0)