Skip to content

Commit 6e551e2

Browse files
Server options: validate URI, make server return with bound port
1 parent 36808ea commit 6e551e2

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@
3030
public interface SyncServer extends Closeable {
3131

3232
/**
33-
* Gets the URL the server is running at.
33+
* Returns the URL this server is listening on, including the bound port (see {@link #getPort()}).
3434
*/
3535
String getUrl();
3636

3737
/**
38-
* Gets the port the server has bound to.
38+
* Returns the port this server listens on, or 0 if the server was not yet started.
39+
* <p>
40+
* This is especially useful if the port was assigned arbitrarily (a "0" port was used in the URL when building the
41+
* server).
3942
*/
4043
int getPort();
4144

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServerBuilder.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.objectbox.sync.server;
1818

19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.util.ArrayList;
2022
import java.util.List;
2123

@@ -37,7 +39,7 @@
3739
public class SyncServerBuilder {
3840

3941
final BoxStore boxStore;
40-
final String url;
42+
final URI url;
4143
private final List<SyncCredentialsToken> credentials = new ArrayList<>();
4244

4345
private @Nullable String certificatePath;
@@ -64,7 +66,11 @@ public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials authenti
6466
"Please visit https://objectbox.io/sync/ for options.");
6567
}
6668
this.boxStore = boxStore;
67-
this.url = url;
69+
try {
70+
this.url = new URI(url);
71+
} catch (URISyntaxException e) {
72+
throw new IllegalArgumentException("Sync server URL is invalid: " + url, e);
73+
}
6874
authenticatorCredentials(authenticatorCredentials);
6975
}
7076

@@ -263,7 +269,7 @@ byte[] buildSyncServerOptions() {
263269
fbb.forceDefaults(true);
264270

265271
// Serialize non-integer values first to get their offset
266-
int urlOffset = fbb.createString(url);
272+
int urlOffset = fbb.createString(url.toString());
267273
int certificatePathOffset = 0;
268274
if (certificatePath != null) {
269275
certificatePathOffset = fbb.createString(certificatePath);

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServerImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package io.objectbox.sync.server;
1818

19+
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
1922
import javax.annotation.Nullable;
2023

2124
import io.objectbox.annotation.apihint.Internal;
@@ -28,7 +31,7 @@
2831
@Internal
2932
public class SyncServerImpl implements SyncServer {
3033

31-
private final String url;
34+
private final URI url;
3235
private volatile long handle;
3336

3437
/**
@@ -63,7 +66,11 @@ private long getHandle() {
6366

6467
@Override
6568
public String getUrl() {
66-
return url;
69+
try {
70+
return new URI(url.getScheme(), null, url.getHost(), getPort(), null, null, null).toString();
71+
} catch (URISyntaxException e) {
72+
throw new RuntimeException("Server URL can not be constructed", e);
73+
}
6774
}
6875

6976
@Override

0 commit comments

Comments
 (0)