use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class KeycloakSecurityContextExtractFilter method doFilter.
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
Assert.requireNonNull(chain, "chain");
final KeycloakSecurityContext securityContext = keyCloakSecurityExtractor.extractContext(request);
contextHolder.set(securityContext);
realmHolder.set(req.getHeader(REALM_NAME_HEADER));
appNameHolder.set(req.getHeader(APPLICATION_NAME_HEADER));
accessDenied.set(false);
final Subscription userContextSubscription = Optional.ofNullable(securityContext).map(c -> c.getToken()).map(t -> t.getPreferredUsername()).map(u -> ContextManagerImpl.getInstance().addThreadContext(USER_CONTEXT, u)).orElse(null);
try {
chain.doFilter(request, response);
} catch (Exception e) {
if (!accessDenied.get()) {
throw e;
} else {
LOG.error("SecurityContext error in request", e);
}
} finally {
Optional.ofNullable(userContextSubscription).ifPresent(s -> s.unsubscribe());
contextHolder.set(null);
boolean sendAccessDenied = accessDenied.get();
accessDenied.set(false);
if (sendAccessDenied) {
((HttpServletResponse) response).sendError(403, "Access Denied");
}
}
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class TestPropertyChange method testWithAnnotatedSimpleModel.
@Test
public void testWithAnnotatedSimpleModel(@Mocked AbstractClientConnector connector) {
final ClientModelStore clientModelStore = createClientModelStore(connector);
final EventDispatcher dispatcher = createEventDispatcher(clientModelStore);
final BeanRepository repository = createBeanRepository(clientModelStore, dispatcher);
final BeanManager manager = createBeanManager(clientModelStore, repository, dispatcher);
final SimpleAnnotatedTestModel model = manager.create(SimpleAnnotatedTestModel.class);
final ListerResults<String> results = new ListerResults<>();
ValueChangeListener<String> myListener = new ValueChangeListener<String>() {
@Override
public void valueChanged(ValueChangeEvent<? extends String> evt) {
Assert.assertEquals(evt.getSource(), model.myProperty());
results.newValue = evt.getNewValue();
results.oldValue = evt.getOldValue();
results.listenerCalls++;
}
};
final Subscription subscription = model.myProperty().onChanged(myListener);
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, nullValue());
model.myProperty().set("Hallo Property");
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is("Hallo Property"));
assertThat(results.oldValue, nullValue());
results.listenerCalls = 0;
model.myProperty().set("Hallo Property2");
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is("Hallo Property2"));
assertThat(results.oldValue, is("Hallo Property"));
results.listenerCalls = 0;
model.myProperty().set(null);
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, is("Hallo Property2"));
results.listenerCalls = 0;
subscription.unsubscribe();
model.myProperty().set("Hallo Property3");
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, is("Hallo Property2"));
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class TestObservableArrayListWriteOperations method removeListener_shouldNotFireListener.
// TODO: removeAll, retainAll
// ////////////////////////////////////////
// add(Object)
// ////////////////////////////////////////
@Test
public void removeListener_shouldNotFireListener() {
final ObservableArrayList<String> list = new ObservableArrayList<>();
final TestListChangeListener listener = new TestListChangeListener();
final Subscription subscription = list.onChanged(listener);
subscription.unsubscribe();
list.add("42");
assertThat(listener.calls, is(0));
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform-examples by canoo.
the class ToDoController method onInit.
@PostConstruct
public void onInit() {
final Subscription changedSubscription = eventBus.subscribe(ITEM_MARK_CHANGED, message -> updateItemState(message.getData()));
final Subscription removedSubscription = eventBus.subscribe(ITEM_REMOVED, message -> removeItem(message.getData()));
final Subscription addedSubscritpion = eventBus.subscribe(ITEM_ADDED, message -> addItem(message.getData()));
subscriptions.add(changedSubscription);
subscriptions.add(removedSubscription);
subscriptions.add(addedSubscritpion);
todoItemStore.itemNameStream().forEach(name -> addItem(name));
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class ReactiveTransormations method throttleLast.
/**
* Provides a {@link TransformedProperty} that is "throttleLast" transformation of the given {@link Property}.
* @param property the property
* @param timeout timeout for the "throttleLast" transformation
* @param unit time unit for the "throttleLast" transformation
* @param <T> type of the property
* @return the transformed property
*/
public static <T> TransformedProperty<T> throttleLast(Property<T> property, long timeout, TimeUnit unit) {
Assert.requireNonNull(property, "property");
Assert.requireNonNull(unit, "unit");
final PublishSubject<T> reactiveObservable = PublishSubject.create();
Subscription basicSubscription = property.onChanged(new ValueChangeListener<T>() {
@Override
public void valueChanged(ValueChangeEvent<? extends T> evt) {
reactiveObservable.onNext(evt.getNewValue());
}
});
TransformedPropertyImpl result = new TransformedPropertyImpl<>(basicSubscription);
Observable<T> transformedObservable = reactiveObservable.throttleLast(timeout, unit);
transformedObservable.subscribe(result);
reactiveObservable.onNext(property.get());
return result;
}
Aggregations