Skip to content

Commit 1ab5a9d

Browse files
committed
[WICKET-7059] make easier to skip some page serializations
1 parent d8b64cf commit 1ab5a9d

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

wicket-core/src/main/java/org/apache/wicket/pageStore/RequestPageStore.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.wicket.MetaDataKey;
2323
import org.apache.wicket.page.IManageablePage;
24+
import org.apache.wicket.request.IRequestCycle;
2425
import org.apache.wicket.request.cycle.RequestCycle;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
@@ -107,9 +108,10 @@ public void end(IPageContext context)
107108
public void detach(IPageContext context)
108109
{
109110
RequestData requestData = getRequestData(context);
111+
IRequestCycle requestCycle = RequestCycle.get();
110112
for (IManageablePage page : requestData.pages())
111113
{
112-
if (isPageStateless(page) == false)
114+
if (isPageStateless(page) == false && shouldSerializePage(requestCycle, page))
113115
{
114116
getDelegate().addPage(context, page);
115117
}
@@ -119,6 +121,21 @@ public void detach(IPageContext context)
119121
getDelegate().detach(context);
120122
}
121123

124+
/**
125+
* Give the opportunity to skip some serializations. E.g. we have some AJAX behavior that is sending some
126+
* info from client to page but page structure didn't change at all and nothing is repainted via AJAX.
127+
* But this will trigger a serialization. Returning false here would prevent that request from doing a
128+
* page serialization. For heavy pages this can really make a difference.
129+
*
130+
* @param requestCycle The request
131+
* @param page The {@link IManageablePage}
132+
* @return <code>true</code> if page should be serialized for this request. The default is true.
133+
*/
134+
protected boolean shouldSerializePage(IRequestCycle requestCycle, IManageablePage page)
135+
{
136+
return true;
137+
}
138+
122139
private boolean isPageStateless(final IManageablePage page) {
123140
boolean isPageStateless;
124141
try

wicket-core/src/test/java/org/apache/wicket/pageStore/RequestPageStoreTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.apache.wicket.MockPage;
2424
import org.apache.wicket.mock.MockPageContext;
2525
import org.apache.wicket.mock.MockPageStore;
26+
import org.apache.wicket.page.IManageablePage;
27+
import org.apache.wicket.request.IRequestCycle;
2628
import org.junit.jupiter.api.Test;
2729

2830
/**
@@ -62,6 +64,43 @@ void testAdd()
6264
assertNull(store.getPage(context, 2), "no page in request store");
6365
assertNull(store.getPage(context, 3), "no page in request store");
6466
}
67+
68+
69+
@Test
70+
void testAvoidSomePage()
71+
{
72+
MockPageStore mockStore = new MockPageStore();
73+
74+
MockPageContext context = new MockPageContext();
75+
76+
RequestPageStore store = new RequestPageStore(mockStore) {
77+
@Override
78+
protected boolean shouldSerializePage(IRequestCycle requestCycle, IManageablePage page) {
79+
// we just skip serialization of third page.
80+
return page.getPageId() != 3;
81+
}
82+
};
83+
84+
MockPage page1 = new MockPage(1);
85+
MockPage page2 = new MockPage(2);
86+
MockPage page3 = new MockPage(3);
87+
88+
store.addPage(context, page1);
89+
store.addPage(context, page2);
90+
store.addPage(context, page3);
91+
92+
assertTrue(mockStore.getPages().isEmpty(), "no pages delegated before detach");
93+
94+
store.detach(context);
95+
96+
assertEquals(2, mockStore.getPages().size(), "pages delegated on detach");
97+
98+
mockStore.getPages().clear();
99+
100+
assertNull(store.getPage(context, 1), "no page in request store");
101+
assertNull(store.getPage(context, 2), "no page in request store");
102+
assertNull(store.getPage(context, 3), "no page in request store");
103+
}
65104

66105
@Test
67106
void testUntouch()

0 commit comments

Comments
 (0)