be.objectify.deadbolt.java.cache.DefaultSubjectCache Maven / Gradle / Ivy
/*
* Copyright 2010-2016 Steve Chaloner
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package be.objectify.deadbolt.java.cache;
import be.objectify.deadbolt.java.ConfigKeys;
import be.objectify.deadbolt.java.DeadboltExecutionContextProvider;
import be.objectify.deadbolt.java.DeadboltHandler;
import be.objectify.deadbolt.java.ExecutionContextProvider;
import be.objectify.deadbolt.java.models.Subject;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import play.libs.concurrent.HttpExecution;
import play.mvc.Http;
import scala.concurrent.ExecutionContext;
import scala.concurrent.ExecutionContextExecutor;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
/**
* @author Steve Chaloner ([email protected])
*/
@Singleton
public class DefaultSubjectCache implements SubjectCache
{
private final boolean cacheUserPerRequestEnabled;
private final DeadboltExecutionContextProvider executionContextProvider;
@Inject
public DefaultSubjectCache(final Config config,
final ExecutionContextProvider ecProvider)
{
final HashMap defaults = new HashMap<>();
defaults.put(ConfigKeys.CACHE_DEADBOLT_USER_DEFAULT._1,
ConfigKeys.CACHE_DEADBOLT_USER_DEFAULT._2);
final Config configWithFallback = config.withFallback(ConfigFactory.parseMap(defaults));
this.cacheUserPerRequestEnabled = configWithFallback.getBoolean(ConfigKeys.CACHE_DEADBOLT_USER_DEFAULT._1);
this.executionContextProvider = ecProvider.get();
}
@Override
public CompletionStage> apply(final DeadboltHandler deadboltHandler,
final Http.Context context)
{
final CompletionStage> promise;
if (cacheUserPerRequestEnabled)
{
final Optional extends Subject> cachedUser = Optional.ofNullable((Subject) context.args.get(ConfigKeys.CACHE_DEADBOLT_USER_DEFAULT._1));
if (cachedUser.isPresent())
{
promise = CompletableFuture.completedFuture(cachedUser);
}
else
{
final ExecutionContext executionContext = executionContextProvider.get();
final ExecutionContextExecutor executor = HttpExecution.fromThread(executionContext);
promise = deadboltHandler.getSubject(context)
.thenApplyAsync(subjectOption ->
{
subjectOption.ifPresent(subject -> context.args.put(ConfigKeys.CACHE_DEADBOLT_USER_DEFAULT._1,
subject));
return subjectOption;
}, executor);
}
}
else
{
promise = deadboltHandler.getSubject(context);
}
return promise;
}
}