1313import com .fasterxml .jackson .databind .ObjectMapper ;
1414import com .fasterxml .jackson .databind .ser .impl .SimpleBeanPropertyFilter ;
1515import com .fasterxml .jackson .databind .ser .impl .SimpleFilterProvider ;
16- import dev .restate .sdk .core .generated .discovery .Discovery ;
1716import dev .restate .sdk .core .generated .manifest .EndpointManifestSchema ;
1817import dev .restate .sdk .core .generated .manifest .Handler ;
1918import dev .restate .sdk .core .generated .manifest .Service ;
2322import java .util .stream .Collectors ;
2423import java .util .stream .Stream ;
2524
26- class DiscoveryProtocol {
27- static final Discovery .ServiceDiscoveryProtocolVersion MIN_SERVICE_DISCOVERY_PROTOCOL_VERSION =
28- Discovery .ServiceDiscoveryProtocolVersion .V1 ;
29- static final Discovery .ServiceDiscoveryProtocolVersion MAX_SERVICE_DISCOVERY_PROTOCOL_VERSION =
30- Discovery .ServiceDiscoveryProtocolVersion .V4 ;
31-
32- static boolean isSupported (
33- Discovery .ServiceDiscoveryProtocolVersion serviceDiscoveryProtocolVersion ) {
34- return MIN_SERVICE_DISCOVERY_PROTOCOL_VERSION .getNumber ()
35- <= serviceDiscoveryProtocolVersion .getNumber ()
36- && serviceDiscoveryProtocolVersion .getNumber ()
37- <= MAX_SERVICE_DISCOVERY_PROTOCOL_VERSION .getNumber ();
25+ public class DiscoveryProtocol {
26+ public enum Version {
27+ V1 ("application/vnd.restate.endpointmanifest.v1+json" ),
28+ V2 ("application/vnd.restate.endpointmanifest.v2+json" ),
29+ V3 ("application/vnd.restate.endpointmanifest.v3+json" ),
30+ V4 ("application/vnd.restate.endpointmanifest.v4+json" );
31+
32+ private final String header ;
33+
34+ Version (String header ) {
35+ this .header = header ;
36+ }
37+
38+ public String getHeader () {
39+ return header ;
40+ }
41+
42+ public int getNumber () {
43+ return ordinal () + 1 ;
44+ }
45+
46+ public boolean isSupported () {
47+ // We support all versions so far
48+ return true ;
49+ }
50+
51+ public static final Version MIN = Version .V1 ;
52+ public static final Version MAX = Version .V4 ;
53+
54+ public static Optional <Version > fromHeader (String headerValue ) {
55+ String trimmed = headerValue .trim ();
56+ return Stream .of (values ())
57+ .filter (version -> version .header .equalsIgnoreCase (trimmed ))
58+ .findFirst ();
59+ }
3860 }
3961
4062 /**
@@ -44,69 +66,36 @@ static boolean isSupported(
4466 * @return The highest supported service protocol version, otherwise
4567 * Protocol.ServiceProtocolVersion.SERVICE_PROTOCOL_VERSION_UNSPECIFIED
4668 */
47- static Discovery .ServiceDiscoveryProtocolVersion selectSupportedServiceDiscoveryProtocolVersion (
48- String acceptedVersionsString ) {
69+ static Version selectSupportedServiceDiscoveryProtocolVersion (String acceptedVersionsString ) {
4970 // assume V1 in case nothing was set
5071 if (acceptedVersionsString == null || acceptedVersionsString .isEmpty ()) {
51- return Discovery . ServiceDiscoveryProtocolVersion .V1 ;
72+ return Version .V1 ;
5273 }
5374
5475 final String [] supportedVersions = acceptedVersionsString .split ("," );
5576
56- Discovery .ServiceDiscoveryProtocolVersion maxVersion =
57- Discovery .ServiceDiscoveryProtocolVersion .SERVICE_DISCOVERY_PROTOCOL_VERSION_UNSPECIFIED ;
77+ Version maxVersion = null ;
5878
5979 for (String versionString : supportedVersions ) {
60- final Optional <Discovery .ServiceDiscoveryProtocolVersion > optionalVersion =
61- parseServiceDiscoveryProtocolVersion (versionString );
80+ final Optional <Version > optionalVersion = Version .fromHeader (versionString );
6281
6382 if (optionalVersion .isPresent ()) {
64- final Discovery .ServiceDiscoveryProtocolVersion version = optionalVersion .get ();
65- if (isSupported (version ) && version .getNumber () > maxVersion .getNumber ()) {
83+ final Version version = optionalVersion .get ();
84+ if (version .isSupported ()
85+ && (maxVersion == null || version .getNumber () > maxVersion .getNumber ())) {
6686 maxVersion = version ;
6787 }
6888 }
6989 }
7090
71- return maxVersion ;
72- }
73-
74- static Optional <Discovery .ServiceDiscoveryProtocolVersion > parseServiceDiscoveryProtocolVersion (
75- String versionString ) {
76- versionString = versionString .trim ();
77-
78- if (versionString .equals ("application/vnd.restate.endpointmanifest.v1+json" )) {
79- return Optional .of (Discovery .ServiceDiscoveryProtocolVersion .V1 );
80- }
81- if (versionString .equals ("application/vnd.restate.endpointmanifest.v2+json" )) {
82- return Optional .of (Discovery .ServiceDiscoveryProtocolVersion .V2 );
83- }
84- if (versionString .equals ("application/vnd.restate.endpointmanifest.v3+json" )) {
85- return Optional .of (Discovery .ServiceDiscoveryProtocolVersion .V3 );
86- }
87- if (versionString .equals ("application/vnd.restate.endpointmanifest.v4+json" )) {
88- return Optional .of (Discovery .ServiceDiscoveryProtocolVersion .V4 );
91+ if (Objects .isNull (maxVersion )) {
92+ throw new ProtocolException (
93+ String .format (
94+ "Unsupported Discovery version in the Accept header '%s'" , acceptedVersionsString ),
95+ ProtocolException .UNSUPPORTED_MEDIA_TYPE_CODE );
8996 }
90- return Optional .empty ();
91- }
9297
93- static String serviceDiscoveryProtocolVersionToHeaderValue (
94- Discovery .ServiceDiscoveryProtocolVersion version ) {
95- if (Objects .requireNonNull (version ) == Discovery .ServiceDiscoveryProtocolVersion .V1 ) {
96- return "application/vnd.restate.endpointmanifest.v1+json" ;
97- }
98- if (Objects .requireNonNull (version ) == Discovery .ServiceDiscoveryProtocolVersion .V2 ) {
99- return "application/vnd.restate.endpointmanifest.v2+json" ;
100- }
101- if (Objects .requireNonNull (version ) == Discovery .ServiceDiscoveryProtocolVersion .V3 ) {
102- return "application/vnd.restate.endpointmanifest.v3+json" ;
103- }
104- if (Objects .requireNonNull (version ) == Discovery .ServiceDiscoveryProtocolVersion .V4 ) {
105- return "application/vnd.restate.endpointmanifest.v4+json" ;
106- }
107- throw new IllegalArgumentException (
108- String .format (
109- "Service discovery protocol version '%s' has no header value" , version .getNumber ()));
98+ return maxVersion ;
11099 }
111100
112101 static final ObjectMapper MANIFEST_OBJECT_MAPPER = new ObjectMapper ();
@@ -139,12 +128,11 @@ interface FieldsMixin {}
139128 }
140129
141130 static byte [] serializeManifest (
142- Discovery .ServiceDiscoveryProtocolVersion serviceDiscoveryProtocolVersion ,
143- EndpointManifestSchema response )
131+ Version serviceDiscoveryProtocolVersion , EndpointManifestSchema response )
144132 throws ProtocolException {
145133 try {
146134 SimpleBeanPropertyFilter filter ;
147- if (serviceDiscoveryProtocolVersion == Discovery . ServiceDiscoveryProtocolVersion .V1 ) {
135+ if (serviceDiscoveryProtocolVersion == Version .V1 ) {
148136 filter =
149137 SimpleBeanPropertyFilter .serializeAllExcept (
150138 Stream .concat (
@@ -153,14 +141,14 @@ static byte[] serializeManifest(
153141 DISCOVERY_FIELDS_ADDED_IN_V3 .stream ()),
154142 DISCOVERY_FIELDS_ADDED_IN_V4 .stream ())
155143 .collect (Collectors .toSet ()));
156- } else if (serviceDiscoveryProtocolVersion == Discovery . ServiceDiscoveryProtocolVersion .V2 ) {
144+ } else if (serviceDiscoveryProtocolVersion == Version .V2 ) {
157145 filter =
158146 SimpleBeanPropertyFilter .serializeAllExcept (
159147 Stream .concat (
160148 DISCOVERY_FIELDS_ADDED_IN_V3 .stream (),
161149 DISCOVERY_FIELDS_ADDED_IN_V4 .stream ())
162150 .collect (Collectors .toSet ()));
163- } else if (serviceDiscoveryProtocolVersion == Discovery . ServiceDiscoveryProtocolVersion .V3 ) {
151+ } else if (serviceDiscoveryProtocolVersion == Version .V3 ) {
164152 filter = SimpleBeanPropertyFilter .serializeAllExcept (DISCOVERY_FIELDS_ADDED_IN_V4 );
165153 } else {
166154 filter = SimpleBeanPropertyFilter .serializeAll ();
0 commit comments