Skip to content

Commit

Permalink
ZK-5707: avoid invoking a event listener after a session expired
Browse files Browse the repository at this point in the history
  • Loading branch information
jumperchen committed Sep 19, 2024
1 parent 1d27a30 commit 2bbeca0
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
4 changes: 3 additions & 1 deletion zk/src/main/java/org/zkoss/zk/ui/impl/UiEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.Richlet;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.WebApp;
Expand Down Expand Up @@ -708,13 +709,14 @@ private static Extension initializeExtension() {
return new DefaultExtension();
}
}

private Extension getExtension() {
return ExtensionHolder.INSTANCE;
}

private static final Event nextEvent(UiVisualizer uv) {
final Event evt = ((ExecutionCtrl) uv.getExecution()).getNextEvent();
return evt != null && !uv.isAborting() ? evt : null;
return evt != null && !uv.isAborting() && !((SessionCtrl) Sessions.getCurrent()).isInvalidated() ? evt : null;
}

/** Cycle 1:
Expand Down
1 change: 1 addition & 0 deletions zkdoc/release-note
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ZK 10.1.0
ZK-5743: clicking menuitem fires 2 onClick events in mobile browser
ZK-5594: ListboxDataLoader doGroupsDataChange finds incorrect offset, causes class cast exception
ZK-5475: select a date with time produces an invalid result under locale pt
ZK-5707: avoid invoking a event listener after a session expired

* Upgrade Notes
+ Remove Htmls.encodeJavaScript(), Strings.encodeJavaScript(), Strings.escape() with Strings.ESCAPE_JAVASCRIPT, and replace them with OWASP Java Encoder APIs instead.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* B101_ZK_5707Composer.java
Purpose:
Description:
History:
12:44 PM 2024/9/19, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.test2;

import java.util.concurrent.CompletableFuture;
import java.util.stream.IntStream;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.http.SimpleSession;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.Button;

/**
* @author jumperchen
*/
public class B101_ZK_5707Composer implements Composer {
static int count = 0;
@Override
public void doAfterCompose(Component comp) throws Exception {
Button button = new Button("many users logout");
button.addEventListener(Events.ON_CLICK, e -> {
Events.postEvent("onLogout", comp, null);
IntStream.range(0, 100).forEach(n -> Events.postEvent("onDummy", comp, null));
});
comp.addEventListener("onLogout", e -> {
// HttpSession session = (HttpSession) Sessions.getCurrent().getNativeSession();
SimpleSession simpleSession = ((SimpleSession)Sessions.getCurrent());
// simulate session logout/timeout from a different thread (browser tab/background process)
System.out.println(">> handle onLogout ");
CompletableFuture.runAsync( () ->{
System.out.println(">> handle onLogout Now");
// session.invalidate();
simpleSession.invalidateNow();
});
});

comp.addEventListener("onDummy", event -> {
Thread.sleep(100);
System.out.println(">> handle onDummy " + ++count);
});
comp.appendChild(button);
}

}
18 changes: 18 additions & 0 deletions zktest/src/main/webapp/test2/B101-ZK-5707.zul
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
B101-ZK-5707.zul
Purpose:
Description:
History:
2024/9/19, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
-->
<zk>
<div apply="org.zkoss.zktest.test2.B101_ZK_5707Composer"/>
</zk>
1 change: 1 addition & 0 deletions zktest/src/main/webapp/test2/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,7 @@ B90-ZK-4431.zul=A,E,Multislider
##zats##B101-ZK-5743.zul=A,E,Compressed,JS,Debug-JS,TabletUI,Mobile,onClick
##zats##B101-ZK-5594.zul=A,E,Group,ListGroup,ROD,zkmax,Exception
##zats##B101-ZK-5475.zul=A,E,Datebox,PT,Locale,Format,Constraint
##Zats##B101-ZK-5707.zul=A,E,Session,Timeout,Event,Exception,Desktop

##
# Features - 3.0.x version
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* B101_ZK_5707Test.java
Purpose:
Description:
History:
12:48 PM 2024/9/19, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.zats.test2;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;

import org.zkoss.test.webdriver.ForkJVMTestOnly;
import org.zkoss.zats.mimic.DesktopAgent;
import org.zkoss.zk.ui.AbstractComponent;
import org.zkoss.zktest.zats.ZATSTestCase;

/**
* @author jumperchen
*/
@ForkJVMTestOnly
public class B101_ZK_5707Test extends ZATSTestCase {
private static Logger logger;
@BeforeAll
public static void beforeAll() throws Exception {
logger = mock(Logger.class);
setFinalStatic(AbstractComponent.class.getDeclaredField("log"), logger);
}

@Test
public void test() throws Exception {
DesktopAgent desktopAgent = connect("/test2/B101-ZK-5707.zul");
desktopAgent.query("button").click();
verify(logger, never()).warn(any());
}
// https://stackoverflow.com/a/30703932
private static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
}

0 comments on commit 2bbeca0

Please sign in to comment.