Today I was working with a "Facebook interface" to an application being rewritten with Wicket. I needed to do a redirect, and as you probably know, a "normal" redirect does not work if the Facebook app is running in the "FBML mode".
Facebook provides a custom tag to accomplish a redirect within the Facebook canvas - Fb:redirect (http://wiki.developers.facebook.com/index.php/Fb:redirect).
I ended up doing a custom IRequestTarget implementation for this purpose, and thought I'd share what I came up with here. Please bear with my very limited experience with Wicket, I was just happy to get it working for my simple need. Feel free to edit/add to this as you see fit.
This is how I use it in my code, relevant code below:
Code Block |
---|
page.getRequestCycle().setRequestTarget(new FBRedirectRequestTarget(SomeCanvasPageToRedirectTo.class)); |
Custom request target:
Code Block |
---|
import org.apache.wicket.Page; import org.apache.wicket.PageParameters; import org.apache.wicket.RequestCycle; import org.apache.wicket.request.IRequestCycleProcessor; import org.apache.wicket.request.target.component.BookmarkablePageRequestTarget; public class FBRedirectRequestTarget extends BookmarkablePageRequestTarget { public FBRedirectRequestTarget(Class pageClass) { this(pageClass, null); } public FBRedirectRequestTarget(Class pageClass, PageParameters pageParameters) { super(null, pageClass, pageParameters); } public void respond(RequestCycle requestCycle) { IRequestCycleProcessor processor = requestCycle.getProcessor(); String redirectUrl = processor.getRequestCodingStrategy().encode( requestCycle, this).toString(); Page page = new FBRedirectPage(redirectUrl); page.renderPage(); } } |
Simple page for the FBML markup:
Code Block |
---|
package com.unet.sniff.view.pages.facebook; import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.basic.Label; public class FBRedirectPage extends WebPage { public FBRedirectPage(String url) { add(new Label("redirect").add(new SimpleAttributeModifier("url", url))); } } |
Markup for the above page (FBRedirectPage.html):
Code Block | ||||
---|---|---|---|---|
| ||||
<fb:redirect wicket:id="redirect" url="#" /> |