/** * Creates a {@link DelegatingSecurityContextCallable} and with the given * {@link Callable} and {@link SecurityContext}, but if the securityContext is null * will defaults to the current {@link SecurityContext} on the * {@link SecurityContextHolder} * * @param delegate the delegate {@link DelegatingSecurityContextCallable} to run with * the specified {@link SecurityContext}. Cannot be null. * @param securityContext the {@link SecurityContext} to establish for the delegate * {@link Callable}. If null, defaults to {@link SecurityContextHolder#getContext()} * @return */ public static <V> Callable<V> create(Callable<V> delegate, SecurityContext securityContext) { return securityContext == null ? new DelegatingSecurityContextCallable<>( delegate) : new DelegatingSecurityContextCallable<>(delegate, securityContext); } }
protected final <T> Callable<T> wrap(Callable<T> delegate) { return DelegatingSecurityContextCallable.create(delegate, securityContext); } }
/** * Creates a {@link DelegatingSecurityContextCallable} and with the given * {@link Callable} and {@link SecurityContext}, but if the securityContext is null * will defaults to the current {@link SecurityContext} on the * {@link SecurityContextHolder} * * @param delegate the delegate {@link DelegatingSecurityContextCallable} to run with * the specified {@link SecurityContext}. Cannot be null. * @param securityContext the {@link SecurityContext} to establish for the delegate * {@link Callable}. If null, defaults to {@link SecurityContextHolder#getContext()} * @return */ public static <V> Callable<V> create(Callable<V> delegate, SecurityContext securityContext) { return securityContext == null ? new DelegatingSecurityContextCallable<>( delegate) : new DelegatingSecurityContextCallable<>(delegate, securityContext); } }
protected final <T> Callable<T> wrap(Callable<T> delegate) { return DelegatingSecurityContextCallable.create(delegate, securityContext); } }
@Test(expected = IllegalArgumentException.class) public void constructorNullDelegate() { new DelegatingSecurityContextCallable<>(null); }
@Test(expected = IllegalArgumentException.class) public void createNullDelegate() { DelegatingSecurityContextCallable.create(null, securityContext); }
@Test public void toStringDelegates() { callable = new DelegatingSecurityContextCallable<>(delegate, securityContext); assertThat(callable.toString()).isEqualTo(delegate.toString()); }
@Test public void create() throws Exception { callable = DelegatingSecurityContextCallable.create(delegate, securityContext); assertWrapped(callable); }
@Test(expected = IllegalArgumentException.class) public void constructorNullDelegateNonNullSecurityContext() { new DelegatingSecurityContextCallable<>(null, securityContext); }
@Test public void createNullSecurityContext() throws Exception { SecurityContextHolder.setContext(securityContext); callable = DelegatingSecurityContextCallable.create(delegate, null); SecurityContextHolder.clearContext(); // ensure callable is what sets up the // SecurityContextHolder assertWrapped(callable); }
@Test(expected = IllegalArgumentException.class) public void constructorNullDelegateAndSecurityContext() { new DelegatingSecurityContextCallable<>(null, null); }
protected final <T> Callable<T> wrap(Callable<T> delegate) { return DelegatingSecurityContextCallable.create(delegate, securityContext); } }
@Test(expected = IllegalArgumentException.class) public void constructorNullSecurityContext() { new DelegatingSecurityContextCallable<>(delegate, null); }
@Test public void call() throws Exception { callable = new DelegatingSecurityContextCallable<>(delegate, securityContext); assertWrapped(callable); }
@Test public void callOnSameThread() throws Exception { originalSecurityContext = securityContext; SecurityContextHolder.setContext(originalSecurityContext); callable = new DelegatingSecurityContextCallable<>(delegate, securityContext); assertWrapped(callable.call()); }
@Test public void callDefaultSecurityContext() throws Exception { SecurityContextHolder.setContext(securityContext); callable = new DelegatingSecurityContextCallable<>(delegate); SecurityContextHolder.clearContext(); // ensure callable is what sets up the // SecurityContextHolder assertWrapped(callable); }
/** * Creates a {@link DelegatingSecurityContextCallable} and with the given * {@link Callable} and {@link SecurityContext}, but if the securityContext is null * will defaults to the current {@link SecurityContext} on the * {@link SecurityContextHolder} * * @param delegate the delegate {@link DelegatingSecurityContextCallable} to run with * the specified {@link SecurityContext}. Cannot be null. * @param securityContext the {@link SecurityContext} to establish for the delegate * {@link Callable}. If null, defaults to {@link SecurityContextHolder#getContext()} * @return */ public static <V> Callable<V> create(Callable<V> delegate, SecurityContext securityContext) { return securityContext == null ? new DelegatingSecurityContextCallable<>( delegate) : new DelegatingSecurityContextCallable<>(delegate, securityContext); } }
@Override public <T> Callable<T> wrapCallable(Callable<T> callable) { return existingConcurrencyStrategy != null ? existingConcurrencyStrategy .wrapCallable(new DelegatingSecurityContextCallable<T>(callable)) : super.wrapCallable(new DelegatingSecurityContextCallable<T>(callable)); } }