My original email on this topic generated discussion -- but alas, it was all about whether to split this list (agreed that's a bad idea), and not about the actual question.

Quick review: I want to create an ICallback to repeat the current request, regardless of what service is handling that request.

Here's the solution I came up with. It's not a completely satisfactory solution, because (1) it feels hackish, and (2) I really feel like I'm doing too much work for what seems like an elementary and common problem. I still feel I must be missing something, and would appreciate any helpful hints!

My page base class (this could also be a service, or whatever) just fetches the raw HttpServletRequest, and passes that to a custom ICallback that does its best to reproduce the request:


public abstract class ImreBasePage extends BasePage {

    @InjectObject("service:tapestry.globals.HttpServletRequest")
    public abstract HttpServletRequest getRequest();

    protected ICallback getCallback() {
        if(getRequest().getMethod().equals("GET"))
            return new RedirectCallback(getRequest());
// Could try to make RedirectCallback handle POST, but it's a bad idea,
        // at least in the case of my app.
        return null;
    }
}

public class RedirectCallback implements ICallback {

    private final URL url;

    public RedirectCallback(URL uri) {
        this.url = uri;
    }

    public RedirectCallback(HttpServletRequest req) {
        StringBuffer urlS = req.getRequestURL();
        if(!StringUtils.isEmpty(req.getQueryString())) {
            urlS.append('?');
            urlS.append(req.getQueryString());
        }

        try {
            url = new URL(urlS.toString());
        } catch(MalformedURLException e) {
throw new IllegalArgumentException("broken request gives malformed URI: " + urlS, e);
        }
    }

    public void performCallback(IRequestCycle cycle) {
        cycle.sendRedirect(url.toExternalForm());
    }
}


Can I improve this code? Is there some utility I'm missing that does this for me? Is this a valid approach that I should be submitting to Tapestry? Anyone? Anyone? Beuller?

This is on Tapestry 4.0.1, BTW. :P

Cheers,

Paul


On Apr 30, 2006, at 11:50 PM, Paul Cantrell wrote:

I want to create a generic utility method to check whether a user is logged in, and redirect them to a login page if necessary.

To do this, I need to create an ICallback in the general case. In other words, I don't know whether I'm create an ExternalCallback or a PageCallback or a DirectCallback or what have you. I just want to create a callback for whatever the current request is, if such a callback is possible.

Ideally, there would be a IPage.getCallback() method, or something like it -- but I can't find one.

I did find some code on the mailing list involving things like "if (this instanceof IExternalPage)" ... yuck! Surely there's a better way, and I'm just missing it? I've dug around in the docs and list archives, but can't turn anything up. ICallback isn't even mentioned in the user guide, as far as I can tell....

Cheers,

Paul

_________________________________________________________________
Piano music podcast: http://inthehands.com
Other interesting stuff: http://innig.net



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to