-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeDupeAjaxRequestHandler.java
484 lines (410 loc) · 15.5 KB
/
DeDupeAjaxRequestHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package org.coderdreams.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.core.request.handler.PageProvider;
import org.apache.wicket.core.request.handler.RenderPageRequestHandler;
import org.apache.wicket.core.request.handler.logger.PageLogData;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.page.PartialPageUpdate;
import org.apache.wicket.page.XmlPartialPageUpdate;
import org.apache.wicket.request.IRequestCycle;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.Response;
import org.apache.wicket.request.component.IRequestablePage;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.http.WebRequest;
import org.apache.wicket.request.http.WebResponse;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.response.StringResponse;
import org.apache.wicket.response.filter.IResponseFilter;
import org.apache.wicket.util.encoding.UrlDecoder;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Classes;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//a copy of AjaxRequestHandler.
// only change is to override update.writeNormalEvaluations() and update.writePriorityEvaluations()
public class DeDupeAjaxRequestHandler implements AjaxRequestTarget
{
private static final Logger LOGGER = LoggerFactory.getLogger(DeDupeAjaxRequestHandler.class);
/**
* Collector of page updates.
*/
private final PartialPageUpdate update;
/** a list of listeners */
private List<IListener> listeners = null;
/** */
private final Set<ITargetRespondListener> respondListeners = new HashSet<>();
/** see https://issues.apache.org/jira/browse/WICKET-3564 */
protected transient boolean respondersFrozen;
protected transient boolean listenersFrozen;
/** The associated Page */
private final Page page;
private PageLogData logData;
/**
* Constructor
*
* @param page the currently active page
*/
public DeDupeAjaxRequestHandler(final Page page) {
this.page = Args.notNull(page, "page");
update = new XmlPartialPageUpdate(page) {
/**
* Freezes the {@link DeDupeAjaxRequestHandler#listeners} before firing the event and
* un-freezes them afterwards to allow components to add more
* {@link IListener}s for the second event.
*/
@Override
protected void onBeforeRespond(final Response response) {
listenersFrozen = true;
if (listeners != null)
{
for (IListener listener : listeners)
{
listener.onBeforeRespond(markupIdToComponent, DeDupeAjaxRequestHandler.this);
}
}
listenersFrozen = false;
}
/**
* Freezes the {@link DeDupeAjaxRequestHandler#listeners}, and does not un-freeze them as the
* events will have been fired by now.
*
* @param response
* the response to write to
*/
@Override
protected void onAfterRespond(final Response response) {
listenersFrozen = true;
// invoke onafterresponse event on listeners
if (listeners != null)
{
final Map<String, Component> components = Collections
.unmodifiableMap(markupIdToComponent);
// create response that will be used by listeners to append
// javascript
final IJavaScriptResponse jsresponse = new IJavaScriptResponse()
{
@Override
public void addJavaScript(String script)
{
writeNormalEvaluations(response,
Collections.<CharSequence> singleton(script));
}
};
for (IListener listener : listeners)
{
listener.onAfterRespond(components, jsresponse);
}
}
}
@Override
protected void writeNormalEvaluations(final Response response, final Collection<CharSequence> scripts) {
writeEvaluationsDeDupe(response, "evaluate", scripts);
}
@Override
protected void writePriorityEvaluations(Response response, Collection<CharSequence> scripts) {
writeEvaluationsDeDupe(response, "priority-evaluate", scripts);
}
private void writeEvaluationsDeDupe(final Response response, String elementName, Collection<CharSequence> scripts) {
if (scripts.size() > 0) {
Collection<CharSequence> distinct = scripts.stream().distinct().collect(Collectors.toList());
if(distinct.size() > 0) {
StringBuilder combinedScript = new StringBuilder(1024);
for (CharSequence script : distinct) {
combinedScript.append("(function(){").append(script).append("})();");
}
writeEvaluation(elementName, response, combinedScript);
}
}
}
/**
* @param invocation
* type of invocation tag, usually {@literal evaluate} or
* {@literal priority-evaluate}
* @param response
* @param js
*/
private void writeEvaluation(final String invocation, final Response response, final CharSequence js)
{
response.write("<");
response.write(invocation);
response.write(">");
response.write("<![CDATA[");
response.write(encode(js));
response.write("]]>");
response.write("</");
response.write(invocation);
response.write(">");
bodyBuffer.reset();
}
};
}
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#getPage()
*/
@Override
public Page getPage() {
return page;
}
@Override
public void addListener(IListener listener) throws IllegalStateException {
Args.notNull(listener, "listener");
assertListenersNotFrozen();
if (listeners == null)
{
listeners = new LinkedList<>();
}
if (!listeners.contains(listener))
{
listeners.add(listener);
}
}
@Override
public final void addChildren(MarkupContainer parent, Class<?> childCriteria) {
Args.notNull(parent, "parent");
Args.notNull(childCriteria, "childCriteria");
parent.visitChildren(childCriteria, new IVisitor<Component, Void>()
{
@Override
public void component(final Component component, final IVisit<Void> visit)
{
add(component);
visit.dontGoDeeper();
}
});
}
@Override
public void add(Component... components) {
for (final Component component : components) {
if(component == null) {
LOGGER.error("Adding null component to target!", new Exception("component null"));
continue;
}
if (component.getOutputMarkupId() == false && !(component instanceof Page)) {
throw new IllegalArgumentException(
"cannot update component that does not have setOutputMarkupId property set to true. Component: " +
component.toString());
}
add(component, component.getMarkupId());
}
}
@Override
public void add(Component component, String markupId) {
update.add(component, markupId);
}
@Override
public final Collection<? extends Component> getComponents() {
return update.getComponents();
}
@Override
public final void focusComponent(Component component) {
if (component != null && component.getOutputMarkupId() == false) {
throw new IllegalArgumentException(
"cannot update component that does not have setOutputMarkupId property set to true. Component: " +
component.toString());
}
final String id = component != null ? ("'" + component.getMarkupId() + "'") : "null";
appendJavaScript("Wicket.Focus.setFocusOnId(" + id + ");");
}
@Override
public final void appendJavaScript(CharSequence javascript) {
update.appendJavaScript(javascript);
}
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#detach(IRequestCycle)
*/
@Override
public void detach(final IRequestCycle requestCycle) {
if (logData == null) {
logData = new PageLogData(page);
}
update.detach(requestCycle);
}
/**
* @see Object#equals(Object)
*/
@Override
public boolean equals(final Object obj) {
if (obj instanceof DeDupeAjaxRequestHandler) {
DeDupeAjaxRequestHandler that = (DeDupeAjaxRequestHandler) obj;
return update.equals(that.update);
}
return false;
}
/**
* @see Object#hashCode()
*/
@Override
public int hashCode() {
int result = "AjaxRequestHandler".hashCode();
result += update.hashCode() * 17;
return result;
}
@Override
public final void prependJavaScript(CharSequence javascript) {
update.prependJavaScript(javascript);
}
@Override
public void registerRespondListener(ITargetRespondListener listener) {
assertRespondersNotFrozen();
respondListeners.add(listener);
}
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#respond(IRequestCycle)
*/
@Override
public final void respond(final IRequestCycle requestCycle) {
final RequestCycle rc = (RequestCycle)requestCycle;
final WebResponse response = (WebResponse)requestCycle.getResponse();
if (shouldRedirectToPage(requestCycle))
{
// the page itself has been added to the request target, we simply issue a redirect
// back to the page
IRequestHandler handler = new RenderPageRequestHandler(new PageProvider(page));
final String url = rc.urlFor(handler).toString();
response.sendRedirect(url);
return;
}
respondersFrozen = true;
for (ITargetRespondListener listener : respondListeners)
{
listener.onTargetRespond(this);
}
final Application app = page.getApplication();
page.send(app, Broadcast.BREADTH, this);
// Determine encoding
final String encoding = app.getRequestCycleSettings().getResponseRequestEncoding();
// Set content type based on markup type for page
update.setContentType(response, encoding);
// Make sure it is not cached by a client
response.disableCaching();
final StringResponse bodyResponse = new StringResponse();
update.writeTo(bodyResponse, encoding);
CharSequence filteredResponse = invokeResponseFilters(bodyResponse);
response.write(filteredResponse);
}
private boolean shouldRedirectToPage(IRequestCycle requestCycle)
{
if (update.containsPage())
{
return true;
}
if (((WebRequest)requestCycle.getRequest()).isAjax() == false)
{
// the request was not sent by wicket-ajax.js - this can happen when an Ajax request was
// intercepted with #redirectToInterceptPage() and then the original request is re-sent
// by the browser on a subsequent #continueToOriginalDestination()
return true;
}
return false;
}
/**
* Runs the configured {@link IResponseFilter}s over the constructed Ajax response
*
* @param contentResponse
* the Ajax {@link Response} body
* @return filtered response
*/
private AppendingStringBuffer invokeResponseFilters(final StringResponse contentResponse)
{
AppendingStringBuffer responseBuffer = new AppendingStringBuffer(
contentResponse.getBuffer());
List<IResponseFilter> responseFilters = Application.get()
.getRequestCycleSettings()
.getResponseFilters();
if (responseFilters != null)
{
for (IResponseFilter filter : responseFilters)
{
responseBuffer = filter.filter(responseBuffer);
}
}
return responseBuffer;
}
/**
* @see Object#toString()
*/
@Override
public String toString() {
return "[AjaxRequestHandler@" + hashCode() + " responseObject [" + update + "]";
}
@Override
public IHeaderResponse getHeaderResponse() {
return update.getHeaderResponse();
}
/**
* @return the markup id of the focused element in the browser
*/
@Override
public String getLastFocusedElementId()
{
WebRequest request = (WebRequest)page.getRequest();
String id = request.getHeader("Wicket-FocusedElementId");
// WICKET-6568 might contain non-ASCII
return Strings.isEmpty(id) ? null : UrlDecoder.QUERY_INSTANCE.decode(id, request.getCharset());
}
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#getPageClass()
*/
@Override
public Class<? extends IRequestablePage> getPageClass() {
return page.getPageClass();
}
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#getPageId()
*/
@Override
public Integer getPageId() {
return page.getPageId();
}
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#getPageParameters()
*/
@Override
public PageParameters getPageParameters() {
return page.getPageParameters();
}
@Override
public final boolean isPageInstanceCreated() {
return true;
}
@Override
public final Integer getRenderCount() {
return page.getRenderCount();
}
/**
* {@inheritDoc}
*/
@Override
public PageLogData getLogData() {
return logData;
}
private void assertNotFrozen(boolean frozen, Class<?> clazz) {
if (frozen) {
throw new IllegalStateException(Classes.simpleName(clazz) + "s can no longer be added");
}
}
private void assertRespondersNotFrozen() {
assertNotFrozen(respondersFrozen, ITargetRespondListener.class);
}
private void assertListenersNotFrozen() {
assertNotFrozen(listenersFrozen, IListener.class);
}
}