Search in sources :

Example 11 with Subscription

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");
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) ServletRequest(javax.servlet.ServletRequest) Subscription(com.canoo.platform.core.functional.Subscription) Logger(org.slf4j.Logger) Assert(com.canoo.dp.impl.platform.core.Assert) ContextManagerImpl(com.canoo.dp.impl.platform.core.context.ContextManagerImpl) ServletException(javax.servlet.ServletException) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) API(org.apiguardian.api.API) USER_CONTEXT(com.canoo.dp.impl.security.SecurityConstants.USER_CONTEXT) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) HttpServletRequest(javax.servlet.http.HttpServletRequest) REALM_NAME_HEADER(com.canoo.dp.impl.security.SecurityConstants.REALM_NAME_HEADER) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) ServletResponse(javax.servlet.ServletResponse) APPLICATION_NAME_HEADER(com.canoo.dp.impl.security.SecurityConstants.APPLICATION_NAME_HEADER) FilterConfig(javax.servlet.FilterConfig) Optional(java.util.Optional) Filter(javax.servlet.Filter) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) Subscription(com.canoo.platform.core.functional.Subscription) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 12 with Subscription

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"));
}
Also used : ValueChangeEvent(com.canoo.platform.remoting.ValueChangeEvent) EventDispatcher(com.canoo.dp.impl.remoting.EventDispatcher) ValueChangeListener(com.canoo.platform.remoting.ValueChangeListener) SimpleAnnotatedTestModel(com.canoo.dolphin.client.util.SimpleAnnotatedTestModel) BeanRepository(com.canoo.dp.impl.remoting.BeanRepository) Subscription(com.canoo.platform.core.functional.Subscription) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) BeanManager(com.canoo.platform.remoting.BeanManager) Test(org.testng.annotations.Test) AbstractDolphinBasedTest(com.canoo.dolphin.client.util.AbstractDolphinBasedTest)

Example 13 with Subscription

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));
}
Also used : ObservableArrayList(com.canoo.dp.impl.remoting.collections.ObservableArrayList) Subscription(com.canoo.platform.core.functional.Subscription) Test(org.testng.annotations.Test)

Example 14 with Subscription

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));
}
Also used : Subscription(com.canoo.platform.core.functional.Subscription) PostConstruct(javax.annotation.PostConstruct)

Example 15 with Subscription

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;
}
Also used : TransformedPropertyImpl(com.canoo.dp.impl.reactive.TransformedPropertyImpl) Subscription(com.canoo.platform.core.functional.Subscription)

Aggregations

Subscription (com.canoo.platform.core.functional.Subscription)30 Test (org.testng.annotations.Test)10 BeanManager (com.canoo.platform.remoting.BeanManager)7 ValueChangeEvent (com.canoo.platform.remoting.ValueChangeEvent)6 ValueChangeListener (com.canoo.platform.remoting.ValueChangeListener)6 TransformedPropertyImpl (com.canoo.dp.impl.reactive.TransformedPropertyImpl)4 AbstractDolphinBasedTest (com.canoo.dolphin.client.util.AbstractDolphinBasedTest)3 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)3 BeanRepository (com.canoo.dp.impl.remoting.BeanRepository)3 EventDispatcher (com.canoo.dp.impl.remoting.EventDispatcher)3 ServerModelStore (com.canoo.dp.impl.server.legacy.ServerModelStore)3 AbstractDolphinBasedTest (com.canoo.impl.server.util.AbstractDolphinBasedTest)3 Binding (com.canoo.platform.core.functional.Binding)3 SimpleTestModel (com.canoo.dolphin.client.util.SimpleTestModel)2 Assert (com.canoo.dp.impl.platform.core.Assert)2 ContextManagerImpl (com.canoo.dp.impl.platform.core.context.ContextManagerImpl)2 ObservableArrayList (com.canoo.dp.impl.remoting.collections.ObservableArrayList)2 SimpleTestModel (com.canoo.impl.server.util.SimpleTestModel)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2