Thursday, May 27, 2010

Make .Net App Blackberry Sniffed

Blackberry, and other modern handsets as well, are not just phone, emailer or messenger only, they are gaining ground as web application terminals. In my current application, Blackberryers can view business data and take actions on their business just with their handset.

As a designer/developer, I am trying to find out a general pattern to facilitate a whole web application Blackberry sniffed, instead of, as some applications did, just sniff a couple of pages. Here is a solution I worked out:


1. BBSniff base page
Create a base page so as to avoid sniffing on each portal page.

class BBSnifferPage : System.Web.UI.Page

2. Routing
In Init event of the base page, implement sniffing and routing logic. There are three cases:

A. A regular browser request;
B. a Blackberry request and the BB version page is available;
C. a Blackberry request and the BB version page is not found;

Following code shows the implementation:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

// sniff and route
if (!IsBB())
{
// Render current page for regular browser
Response.Write("<b>This is for IE, Firefox etc.</b>");
}
else if (!IsBBFileExist())
{
// BB version page showing no bb version for requested page (this page)
Server.Transfer(System.IO.Path.Combine("bbPages","NoBBVersion.aspx"));
}
else
{
// render BB version page
string[] parts = Request.Url.PathAndQuery.Split('/');
Server.Transfer(System.IO.Path.Combine("bbPages", "bb" + parts[parts.Length - 1]));
}
}

3. BB Page Convention
In order to redirect requests to their BB pages, a convention for storing that pages is needed. In following example, all BB pages' name starts with "bb" and followed with the portal page name, and all BB pages are stored under "bbPage" subfolder.

private bool IsBBFileExist()
{
string[] parts = Request.Url.LocalPath.Split('/');
string fileName = parts[parts.Length - 1];
return (System.IO.File.Exists(Server.MapPath(System.IO.Path.Combine("bbPages", "bb" + fileName))));
}

4. Portal Pages are BBSnifferPage
Because all portal pages are exposed to web access, they are suggested to be Blackberry sniffered.

class FuncPage : BBSnifferPage
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<b>This is for Black Berry. </b>");
}
}

5. BB Function Page
The corresponding BB pages of portal pages are regular web pages.

class BBFuncPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<b>This is for Black Berry. </b>");
}
}

I believe this is a easy-to-apply, easy-to-maintain, loose-coupling framework for blended browsers support.

No comments: