2

I need to set a custom HTTP header to all responses from my Wicket application. I'm currently doing it in a custom RequestCycle, where getWebResponse() is overridden along these lines:

@Override
public WebResponse getWebResponse() {
    WebResponse response = super.getWebResponse();
    response.setHeader("X-custom", "..." );
    return response;
}

This has worked great, until now that I've switched to using AjaxCheckBox (something like this) instead of normal CheckBox for certain configuration options.

My question is, is there an easy way to include my custom header also in Wicket's Ajax responses?

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • NB: the header value is dynamic and comes from a domain object (ultimately from database). I'd much rather do this *inside Wicket*, if at all possible, rather than with normal (Java EE) filter or something. – Jonik Dec 09 '10 at 10:35
  • Another restriction I forgot to mention at first: the Ajax request in question causes a value in database to change, and the custom header should contain precisely that *updated* value. (It's a sort of serial number that gets incremented when certain things change, and the header passes that to a another piece of co-operating software.) I readily admit this probably isn't a very typical requirement for a Wicket app, or any webapp. :) – Jonik Dec 10 '10 at 20:10

3 Answers3

3

I found a way. It actually wasn't hard at all, in the end. When running through some requests with my debugger, I noticed that onEndRequest() does get called for Ajax requests too.

The onEndRequest() method was already overriden in our custom RequestCycle implementation for other purposes (transaction commit), so I just moved the code that sets the header there from getWebResponse().

@Override
protected void onEndRequest() {
    super.onEndRequest();
    ((WebResponse) response).setHeader("X-custom", "..." );
    // ...
}

Perhaps the only non-obvious thing here was that I needed to cast response into WebResponse (when the field's type is Response) to be able to call setHeader().

This could have been done in a normal Java EE filter too, by setting the header after chain.doFilter() call (see my second comment on the question). I didn't choose that because 1) it wasn't clear to me how to wire up data access there and 2) I don't want extra moving parts if I can avoid it. We already use our RequestCycle subclass for HTTP header related things and this fits in nicely. In fact, this change simplified that class, as there's no reason to override getWebResponse() anymore!

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
1

Under the hood, Wicket still uses the standard Java HTML stack. So instead of overriding existing methods, just implement a Filter and register it in your web.xml. With the correct URL pattern, it will apply to all requests, no matter who handles them.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Ah, thanks, I just added more details in a comment to the question. Sure, if there's no other way, I'd need to add a filter and somehow wire up my data access objects there too. (Or then just switch back to normal CheckBox for this case, as I just want to get it working again quickly...) – Jonik Dec 09 '10 at 10:39
0

Looking at the implementation of AjaxRequestTarget

[...]

/**
 * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
 */
public final void respond(final RequestCycle requestCycle)
{
    final WebResponse response = (WebResponse)requestCycle.getResponse();

    if (markupIdToComponent.values().contains(page))

[...]

the Wicket solution would be to override RequestCycle.getResponse() instead.

mfunk
  • 180
  • 8
  • Hmm, `RequestCycle.getResponse()` is final though so you can't do that. :/ – Jonik Dec 09 '10 at 16:03
  • Hmm...., true. I'd say its a bug in the AjaxRequestTarget. At least nice enough to file an Issue: https://issues.apache.org/jira/browse/WICKET-3251 – mfunk Dec 10 '10 at 13:32
  • Thanks... Um, I don't know if it's a Wicket bug or not, but I found an easy way to make it work, inside Wicket and all! See [this answer](http://stackoverflow.com/questions/4397211/how-to-set-custom-http-response-header-in-wickets-ajax-responses/4412995#4412995). – Jonik Dec 10 '10 at 20:30
  • I don't have an account for ASF Jira, so could you mention the solution I found in a comment? (If you think it's relevant at all, of course.) – Jonik Dec 10 '10 at 20:35