@@ -72,18 +72,17 @@ private static void checkFeatureSyncServerAvailable() {
72
72
* Use {@link Sync#server(BoxStore, String, SyncCredentials)} instead.
73
73
*/
74
74
@ Internal
75
- public SyncServerBuilder (BoxStore boxStore , String url , SyncCredentials authenticatorCredentials ) {
75
+ public SyncServerBuilder (BoxStore boxStore , String url , @ Nullable SyncCredentials authenticatorCredentials ) {
76
76
checkNotNull (boxStore , "BoxStore is required." );
77
77
checkNotNull (url , "Sync server URL is required." );
78
- checkNotNull (authenticatorCredentials , "Authenticator credentials are required." );
79
78
checkFeatureSyncServerAvailable ();
80
79
this .boxStore = boxStore ;
81
80
try {
82
81
this .url = new URI (url );
83
82
} catch (URISyntaxException e ) {
84
83
throw new IllegalArgumentException ("Sync server URL is invalid: " + url , e );
85
84
}
86
- authenticatorCredentials (authenticatorCredentials );
85
+ authenticatorCredentialsOrNull (authenticatorCredentials );
87
86
}
88
87
89
88
/**
@@ -116,6 +115,18 @@ public SyncServerBuilder certificatePath(String certificatePath) {
116
115
return this ;
117
116
}
118
117
118
+ private SyncServerBuilder authenticatorCredentialsOrNull (@ Nullable SyncCredentials authenticatorCredentials ) {
119
+ if (authenticatorCredentials == null ) {
120
+ return this ; // Do nothing
121
+ }
122
+ if (!(authenticatorCredentials instanceof SyncCredentialsToken )) {
123
+ throw new IllegalArgumentException ("Sync credentials of type " + authenticatorCredentials .getType ()
124
+ + " are not supported" );
125
+ }
126
+ credentials .add ((SyncCredentialsToken ) authenticatorCredentials );
127
+ return this ;
128
+ }
129
+
119
130
/**
120
131
* Adds additional authenticator credentials to authenticate clients or peers with.
121
132
* <p>
@@ -124,12 +135,7 @@ public SyncServerBuilder certificatePath(String certificatePath) {
124
135
*/
125
136
public SyncServerBuilder authenticatorCredentials (SyncCredentials authenticatorCredentials ) {
126
137
checkNotNull (authenticatorCredentials , "Authenticator credentials must not be null." );
127
- if (!(authenticatorCredentials instanceof SyncCredentialsToken )) {
128
- throw new IllegalArgumentException ("Sync credentials of type " + authenticatorCredentials .getType ()
129
- + " are not supported" );
130
- }
131
- credentials .add ((SyncCredentialsToken ) authenticatorCredentials );
132
- return this ;
138
+ return authenticatorCredentialsOrNull (authenticatorCredentials );
133
139
}
134
140
135
141
/**
@@ -316,7 +322,7 @@ private boolean hasJwtConfig() {
316
322
* Note: this clears all previously set authenticator credentials.
317
323
*/
318
324
public SyncServer build () {
319
- if (credentials .isEmpty ()) {
325
+ if (! hasJwtConfig () && credentials .isEmpty ()) {
320
326
throw new IllegalStateException ("At least one authenticator is required." );
321
327
}
322
328
if (hasJwtConfig ()) {
@@ -368,7 +374,10 @@ byte[] buildSyncServerOptions() {
368
374
if (clusterId != null ) {
369
375
clusterIdOffset = fbb .createString (clusterId );
370
376
}
371
- int authenticationMethodsOffset = buildAuthenticationMethods (fbb );
377
+ int authenticationMethodsOffset = 0 ;
378
+ if (!credentials .isEmpty ()) {
379
+ authenticationMethodsOffset = buildAuthenticationMethods (fbb );
380
+ }
372
381
int clusterPeersVectorOffset = buildClusterPeers (fbb );
373
382
int jwtConfigOffset = 0 ;
374
383
if (hasJwtConfig ()) {
@@ -387,7 +396,9 @@ byte[] buildSyncServerOptions() {
387
396
// After collecting all offsets, create options
388
397
SyncServerOptions .startSyncServerOptions (fbb );
389
398
SyncServerOptions .addUrl (fbb , urlOffset );
390
- SyncServerOptions .addAuthenticationMethods (fbb , authenticationMethodsOffset );
399
+ if (authenticationMethodsOffset != 0 ) {
400
+ SyncServerOptions .addAuthenticationMethods (fbb , authenticationMethodsOffset );
401
+ }
391
402
if (syncFlags != 0 ) {
392
403
SyncServerOptions .addSyncFlags (fbb , syncFlags );
393
404
}
0 commit comments