File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
services-api/src/main/java/io/scalecube/services/transport/api Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 11package io .scalecube .services .transport .api ;
22
3+ import io .scalecube .utils .ServiceLoaderUtil ;
34import java .io .IOException ;
45import java .io .InputStream ;
56import java .io .OutputStream ;
67import java .util .Map ;
8+ import java .util .concurrent .ConcurrentHashMap ;
79
810public 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 ;
You can’t perform that action at this time.
0 commit comments