Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Enable cookies in BrowserField

by Retired ‎02-16-2010 01:03 PM - edited ‎09-16-2010 03:47 PM (5,368 Views)

Summary

 

This article applies to the following:

  • BlackBerry® Java® Development Environment (BlackBerry JDE) 4.1 or later

Details

 

The BrowserFieldDemo sample that is distributed with BlackBerry JDE 4.1 or later, does not support cookies without some additional development. The additional cookie management code will need to handle storing the cookies, applying correct domain matching policies, and expiring the cookies appropriately. There will also need to be functionality added to catch events that are executed when cookies are set and as well as code added to correctly pass any valid cookies along with a new request. This article describes how to implement logic for the former, and it will be the responsibility of the developer to implement their own cookie management functionality.

 

Step1 - Make sure the BlackBerry® MDS Simulator is not caching cookies

 

The BlackBerry MDS Simulator will, by default, cache cookies instead of passing them back to the requesting application. By default, requests made through a BlackBerry® Enterprise Server or BlackBerry® Internet Service will not cache cookies, so it is important to make sure that the BlackBerry MDS Simulator is performing similarly.

  1. Open the rimpublic.property file. This file can be in one of the following locations:

    For BlackBerry MDS Simulators installed with the BlackBerry JDE

    \Program Files\Research In Motion\BlackBerry JDE #.#.#\MDS\config

    For standalone BlackBerry MDS Simulators

    \Program Files\Research In Motion\BlackBerry Email and MDS Services Simulators #.#.#\MDS\config

  2. Under the [HTTP HANDLER] section, update the following:

    application.handler.http.CookieSupport = false

Step 2 - Handle cookies set in JavaScript®

 

One of the ways that cookies can be set is through JavaScript running on a web page. The following modification is to the eventOccurred(event) method, which is responsible for handling all of the browser events:

 

 

public Object eventOccurred(Event event) {
    ...
    switch (eventId)
        ...
        case Event.EVENT_SET_HTTP_COOKIE :
            string cookie = ((SetHttpCookieEvent) event).getCookie();
            // perform further cookie management logic
            break;
        ...
}

 

 

Step 3 - Handle cookies in the HTTP header

 

Cookies can also be set in the Hypertext Transfer Protocol (HTTP) header. To handle cookies set in the header, you will need to inspect the HttpConnection object after the content has been retrieved.

 

The following modification is to the processConnection() method, which is responsible for retrieving the HTML content:

 

 

public void processConnection(HttpConnection connection, Event e) {
    ...
    Field field = browserContent.getDisplayableContent();
    ...
    String key = "";
    for (int i = 0;(key = _currentConnection.getHeaderFieldKey(i)) != null; i++)
    {
        if (key.equalsIgnoreCase("set-cookie"))
        {
            String cookie = _currentConnection.getHeaderField(key);
            // perform further cookie management
        } //if
    } //for
    ...
    browserContent.finishLoading();
}

 

Step 4 - Add cookies to HTTP requests

 

The final step is to add all valid cookies to the HTTP requests so that they are available to the web applications. It is the responsibility of the developer to ensure that only the appropriate cookies are added to the request.

 

The following modification is to the processConnection() method, which is responsible for retrieving the HTML content:

 

 

public void processConnection(HttpConnection connection, Event e) {
    ...
    _currentConnection = connection;
    BrowserContent browserContent = null;
    try
    {
        //add all valid cookies to the connection
        _currentConnection.setRequestProperty("Cookie","cookie1=value; cookie2=value;");
        browserContent = _renderingSession.getBrowserContent(connection, this, e);
        ...
    }
}

 

Contributors
Users Online
Currently online: 31 members 6,221 guests
Please welcome our newest community members: