diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxRequestHandler.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxRequestHandler.java index d9a1d386ad4..fb0ba56c258 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxRequestHandler.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxRequestHandler.java @@ -80,13 +80,13 @@ public class AjaxRequestHandler extends AbstractPartialPageRequestHandler implem /** * Collector of page updates. */ - private final PartialPageUpdate update; + private PartialPageUpdate update; /** a set of listeners */ - private Set listeners = null; + protected Set listeners = null; /** */ - private final Set respondListeners = new HashSet<>(); + protected final Set respondListeners = new HashSet<>(); /** see https://issues.apache.org/jira/browse/WICKET-3564 */ protected transient boolean respondersFrozen; @@ -103,8 +103,17 @@ public class AjaxRequestHandler extends AbstractPartialPageRequestHandler implem public AjaxRequestHandler(final Page page) { super(page); + } - update = new XmlPartialPageUpdate(page) + /** + * Factory method for {@link PartialPageUpdate}'s + * + * @param page The {@link Page} + * @return an instance of {@link PartialPageUpdate} + */ + protected PartialPageUpdate newPartialPageUpdate(final Page page) + { + return new XmlPartialPageUpdate(page) { /** * Freezes the {@link AjaxRequestHandler#listeners} before firing the event and @@ -130,7 +139,7 @@ protected void onBeforeRespond(final Response response) /** * Freezes the {@link AjaxRequestHandler#listeners}, and does not un-freeze them as the * events will have been fired by now. - * + * * @param response * the response to write to */ @@ -143,7 +152,7 @@ protected void onAfterRespond(final Response response) if (listeners != null) { final Map components = Collections - .unmodifiableMap(markupIdToComponent); + .unmodifiableMap(markupIdToComponent); for (AjaxRequestTarget.IListener listener : listeners) { @@ -174,13 +183,16 @@ public void addListener(AjaxRequestTarget.IListener listener) throws IllegalStat @Override public PartialPageUpdate getUpdate() { + if (update == null) { + update = newPartialPageUpdate(getPage()); + } return update; } @Override public final Collection getComponents() { - return update.getComponents(); + return getUpdate().getComponents(); } /** @@ -194,7 +206,7 @@ public void detach(final IRequestCycle requestCycle) logData = new PageLogData(getPage()); } - update.detach(requestCycle); + getUpdate().detach(requestCycle); } /** @@ -206,7 +218,7 @@ public boolean equals(final Object obj) if (obj instanceof AjaxRequestHandler) { AjaxRequestHandler that = (AjaxRequestHandler)obj; - return update.equals(that.update); + return getUpdate().equals(that.update); } return false; } @@ -218,7 +230,7 @@ public boolean equals(final Object obj) public int hashCode() { int result = "AjaxRequestHandler".hashCode(); - result += update.hashCode() * 17; + result += getUpdate().hashCode() * 17; return result; } @@ -265,7 +277,7 @@ public final void respond(final IRequestCycle requestCycle) final String encoding = app.getRequestCycleSettings().getResponseRequestEncoding(); // Set content type based on markup type for page - update.setContentType(response, encoding); + getUpdate().setContentType(response, encoding); // Make sure it is not cached by a client response.disableCaching(); @@ -276,7 +288,7 @@ public final void respond(final IRequestCycle requestCycle) // WICKET-7074 we need to write to a temporary buffer, otherwise, if an exception is produced, // and a redirect is done we will end up with a malformed XML final StringResponse bodyResponse = new StringResponse(); - update.writeTo(bodyResponse, encoding); + getUpdate().writeTo(bodyResponse, encoding); if (filters == null || filters.isEmpty()) { response.write(bodyResponse.getBuffer()); @@ -290,7 +302,7 @@ public final void respond(final IRequestCycle requestCycle) private boolean shouldRedirectToPage(IRequestCycle requestCycle) { - if (update.containsPage()) + if (getUpdate().containsPage()) { return true; } @@ -333,7 +345,7 @@ private CharSequence invokeResponseFilters(final StringResponse contentResponse, @Override public String toString() { - return "[AjaxRequestHandler@" + hashCode() + " responseObject [" + update + "]"; + return "[AjaxRequestHandler@" + hashCode() + " responseObject [" + getUpdate() + "]"; } /** diff --git a/wicket-core/src/main/java/org/apache/wicket/page/PartialPageUpdate.java b/wicket-core/src/main/java/org/apache/wicket/page/PartialPageUpdate.java index 98fb9880a72..67871bdd62b 100644 --- a/wicket-core/src/main/java/org/apache/wicket/page/PartialPageUpdate.java +++ b/wicket-core/src/main/java/org/apache/wicket/page/PartialPageUpdate.java @@ -75,7 +75,7 @@ public abstract class PartialPageUpdate * Length of the script block that combined scripts are wrapped in. This includes the script tag, * CDATA and if CSP is enabled also the nonce. */ - private static final int SCRIPT_BLOCK_LENGTH = 100; + protected static final int SCRIPT_BLOCK_LENGTH = 100; /** * A list of scripts (JavaScript) which should be executed on the client side before the