use of io.realm.mongodb.User in project realm-java by realm.
the class SyncObjectServerFacade method getSyncConfigurationOptions.
@Override
public Object[] getSyncConfigurationOptions(RealmConfiguration config) {
if (config instanceof SyncConfiguration) {
SyncConfiguration syncConfig = (SyncConfiguration) config;
User user = syncConfig.getUser();
App app = user.getApp();
String rosServerUrl = syncConfig.getServerUrl().toString();
String rosUserIdentity = user.getId();
String syncRealmAuthUrl = user.getApp().getConfiguration().getBaseUrl().toString();
String rosUserProvider = user.getProviderType().getId();
String syncUserRefreshToken = user.getRefreshToken();
String syncUserAccessToken = user.getAccessToken();
String deviceId = user.getDeviceId();
byte sessionStopPolicy = syncConfig.getSessionStopPolicy().getNativeValue();
String urlPrefix = syncConfig.getUrlPrefix();
String customAuthorizationHeaderName = app.getConfiguration().getAuthorizationHeaderName();
Map<String, String> customHeaders = app.getConfiguration().getCustomRequestHeaders();
SyncClientResetStrategy clientResetStrategy = syncConfig.getSyncClientResetStrategy();
// undefined value
byte clientResetMode = -1;
if (clientResetStrategy instanceof ManuallyRecoverUnsyncedChangesStrategy) {
clientResetMode = OsRealmConfig.CLIENT_RESYNC_MODE_MANUAL;
} else if (clientResetStrategy instanceof DiscardUnsyncedChangesStrategy) {
clientResetMode = OsRealmConfig.CLIENT_RESYNC_MODE_DISCARD_LOCAL;
}
BeforeClientResetHandler beforeClientResetHandler = (localPtr, osRealmConfig) -> {
NativeContext.execute(nativeContext -> {
Realm before = realmInstanceFactory.createInstance(new OsSharedRealm(localPtr, osRealmConfig, nativeContext));
((DiscardUnsyncedChangesStrategy) clientResetStrategy).onBeforeReset(before);
});
};
AfterClientResetHandler afterClientResetHandler = (localPtr, afterPtr, osRealmConfig) -> {
NativeContext.execute(nativeContext -> {
Realm before = realmInstanceFactory.createInstance(new OsSharedRealm(localPtr, osRealmConfig, nativeContext));
Realm after = realmInstanceFactory.createInstance(new OsSharedRealm(afterPtr, osRealmConfig, nativeContext));
((DiscardUnsyncedChangesStrategy) clientResetStrategy).onAfterReset(before, after);
});
};
long appNativePointer;
// access it.
try {
if (osAppField == null) {
synchronized (SyncObjectServerFacade.class) {
if (osAppField == null) {
Field field = App.class.getDeclaredField("osApp");
field.setAccessible(true);
osAppField = field;
}
}
}
OsApp osApp = (OsApp) osAppField.get(app);
appNativePointer = osApp.getNativePtr();
} catch (Exception e) {
throw new RuntimeException(e);
}
// TODO Simplify. org.bson serialization only allows writing full documents, so the partition
// key is embedded in a document with key 'value' and unwrapped in JNI.
String encodedPartitionValue = null;
if (syncConfig.isPartitionBasedSyncConfiguration()) {
BsonValue partitionValue = syncConfig.getPartitionValue();
switch(partitionValue.getBsonType()) {
case STRING:
case OBJECT_ID:
case INT32:
case INT64:
case BINARY:
case NULL:
encodedPartitionValue = JniBsonProtocol.encode(partitionValue, AppConfiguration.DEFAULT_BSON_CODEC_REGISTRY);
break;
default:
throw new IllegalArgumentException("Unsupported type: " + partitionValue);
}
}
int i = 0;
Object[] configObj = new Object[SYNC_CONFIG_OPTIONS];
configObj[i++] = rosUserIdentity;
configObj[i++] = rosUserProvider;
configObj[i++] = rosServerUrl;
configObj[i++] = syncRealmAuthUrl;
configObj[i++] = syncUserRefreshToken;
configObj[i++] = syncUserAccessToken;
configObj[i++] = deviceId;
configObj[i++] = sessionStopPolicy;
configObj[i++] = urlPrefix;
configObj[i++] = customAuthorizationHeaderName;
configObj[i++] = customHeaders;
configObj[i++] = clientResetMode;
configObj[i++] = beforeClientResetHandler;
configObj[i++] = afterClientResetHandler;
configObj[i++] = encodedPartitionValue;
configObj[i++] = app.getSync();
configObj[i++] = appNativePointer;
return configObj;
} else {
return new Object[SYNC_CONFIG_OPTIONS];
}
}
Aggregations