pal
pal

Reputation: 949

Wicket and responding with "not HTML" to requests

I'm sure this has been answered somewhere else - but I don't know where

I need to respond to HTTP requests from a partner, in our wicket website. The partner expected the response body to say "OK" or anything else in the case of an error

Is there a "nice" way to do this? ... or am I going to be stuck adding a servlet to my (previously) pretty Wicket application?

Upvotes: 3

Views: 432

Answers (1)

tetsuo
tetsuo

Reputation: 10896

You can use resources for that:

class OkResource implements IResource {
    @Override
    public void respond(Attributes attributes) {
        WebResponse resp = (WebResponse) attributes.getResponse();
        resp.setContentType("text/plain");
        resp.write("OK");
    }
}

And register it in your Application class

@Override
protected void init() {
    super.init();
    getSharedResources().add("confirm", new OkResource());
    mountResource("confirm", new SharedResourceReference("confirm"));
}

so that it can be accessed through something like http://host/app/confirm.

Just observe that here you registering a single instance of the resource, so it must be thread-safe, since multiple requests can call it simultaneously.

[EDIT] In Wicket 1.4:

class OkResource extends Resource {
    @Override
    public IResourceStream getResourceStream() {
        return new StringResourceStream("ok", "text/plain");
    }
}

@Override
protected void init() {
    super.init();
    getSharedResources().add("confirm", new OkResource());
    mountSharedResource("confirm", "confirm");
}

Upvotes: 7

Related Questions