Skip to content

Commit 809159a

Browse files
committed
Wrap with expiring holders.
1 parent 2614555 commit 809159a

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

hub/src/main/java/cloud/katta/protocols/hub/HubSession.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ public class HubSession extends HttpSession<HubApiClient> {
8787
*/
8888
private OAuth2RequestInterceptor authorizationService;
8989

90-
private UserDto me;
91-
private ConfigDto config;
90+
private final ExpiringObjectHolder<UserDto> userDtoHolder
91+
= new ExpiringObjectHolder<>(-1L == preferences.getLong("katta.user.ttl") ? Long.MAX_VALUE : preferences.getLong("katta.user.ttl"));
9292

93-
private final ExpiringObjectHolder<UserKeys> userKeys
94-
= new ExpiringObjectHolder<>(preferences.getLong("katta.userkeys.ttl"));
93+
private final ExpiringObjectHolder<ConfigDto> configDtoHolder
94+
= new ExpiringObjectHolder<>(-1L == preferences.getLong("katta.config.ttl") ? Long.MAX_VALUE : preferences.getLong("katta.config.ttl"));
95+
96+
private final ExpiringObjectHolder<UserKeys> userKeysHolder
97+
= new ExpiringObjectHolder<>(-1L == preferences.getLong("katta.userkeys.ttl") ? Long.MAX_VALUE : preferences.getLong("katta.userkeys.ttl"));
9598

9699
private HubVaultListService vaults;
97100

@@ -113,7 +116,7 @@ protected HubApiClient connect(final ProxyFinder proxy, final HostKeyCallback ke
113116
final HubApiClient client = new HubApiClient(host, configuration.build());
114117
try {
115118
// Obtain OAuth configuration
116-
config = new ConfigResourceApi(client).apiConfigGet();
119+
final ConfigDto config = this.getConfig();
117120
final int minHubApiLevel = HostPreferencesFactory.get(host).getInteger("cloud.katta.min_api_level");
118121
final Integer apiLevel = config.getApiLevel();
119122
if(apiLevel == null || apiLevel < minHubApiLevel) {
@@ -129,9 +132,6 @@ protected HubApiClient connect(final ProxyFinder proxy, final HostKeyCallback ke
129132
log.debug("Apply profile {} to bookmark {}", profile, host);
130133
host.setProtocol(profile);
131134
}
132-
catch(ApiException e) {
133-
throw new HubExceptionMappingService().map(e);
134-
}
135135
finally {
136136
client.getHttpClient().close();
137137
}
@@ -164,12 +164,14 @@ public void login(final LoginCallback prompt, final CancelCallback cancel) throw
164164
throw new LoginCanceledException(e);
165165
}
166166
try {
167-
me = new UsersResourceApi(client).apiUsersMeGet(true, false);
167+
final UserDto me = this.getMe();
168168
log.debug("Retrieved user {}", me);
169169
// Ensure device key is available
170170
final DeviceSetupCallback setup = prompt.getFeature(DeviceSetupCallback.class);
171171
log.debug("Configured with setup prompt {}", setup);
172-
userKeys.set(this.pair(setup));
172+
final UserKeys userKeys = this.getUserKeys(setup);
173+
log.debug("Retrieved user keys {}", userKeys);
174+
final ConfigDto config = this.getConfig();
173175
final List<StorageProfileDto> storageProfileDtos = new StorageProfileResourceApi(client).apiStorageprofileGet(false);
174176
for(StorageProfileDto storageProfileDto : storageProfileDtos) {
175177
final StorageProfileDtoWrapper storageProfile = StorageProfileDtoWrapper.coerce(storageProfileDto);
@@ -198,7 +200,8 @@ private UserKeys pair(final DeviceSetupCallback setup) throws BackgroundExceptio
198200
try {
199201
final DeviceKeys deviceKeys = new DeviceKeysServiceImpl(keychain).getOrCreateDeviceKeys(host, setup);
200202
log.debug("Retrieved device keys {}", deviceKeys);
201-
final UserKeys userKeys = new UserKeysServiceImpl(this, keychain).getOrCreateUserKeys(host, me, deviceKeys, setup);
203+
final UserKeys userKeys = new UserKeysServiceImpl(this, keychain).getOrCreateUserKeys(host,
204+
this.getMe(), deviceKeys, setup);
202205
log.debug("Retrieved user keys {}", userKeys);
203206
return userKeys;
204207
}
@@ -224,19 +227,39 @@ protected void logout() {
224227
*
225228
* @return Null prior login
226229
*/
227-
public UserDto getMe() {
228-
return me;
230+
public UserDto getMe() throws BackgroundException {
231+
try {
232+
if(userDtoHolder.get() == null) {
233+
userDtoHolder.set(new UsersResourceApi(client).apiUsersMeGet(true, false));
234+
}
235+
return userDtoHolder.get();
236+
}
237+
catch(ApiException e) {
238+
throw new HubExceptionMappingService().map(e);
239+
}
240+
}
241+
242+
public ConfigDto getConfig() throws BackgroundException {
243+
try {
244+
if(configDtoHolder.get() == null) {
245+
configDtoHolder.set(new ConfigResourceApi(client).apiConfigGet());
246+
}
247+
return configDtoHolder.get();
248+
}
249+
catch(ApiException e) {
250+
throw new HubExceptionMappingService().map(e);
251+
}
229252
}
230253

231254
/**
232255
*
233256
* @return Destroyed keys after login
234257
*/
235258
public UserKeys getUserKeys(final DeviceSetupCallback setup) throws BackgroundException {
236-
if(userKeys.get() == null) {
237-
userKeys.set(this.pair(setup));
259+
if(userKeysHolder.get() == null) {
260+
userKeysHolder.set(this.pair(setup));
238261
}
239-
return userKeys.get();
262+
return userKeysHolder.get();
240263
}
241264

242265
@Override

0 commit comments

Comments
 (0)