Sunday, May 23, 2010

WSO2 Mashup Server - Google Maps API Key Mashup

If you are using Google Maps in your web site, you need to get an API key from Google.com. In order to do that, you need to login using an existing Google account, accept their terms and policies and finally you will be able to get the Google Maps API key. Although this is the usual procedure, if they provide a web service for it, it will be much useful in places where you want to get the key at runtime.

As an example, WSO2 Mashup Server has a Twitter Maps Mashup which shows recent tweets in a Google map. In order to have the proper functionality of this mashup independent of the domain where the server is running, it needs to dynamically generate an API key for that domain and use it within the Twitter map mashup. That was the original motivation towards this mashup.

You will need to deploy the following mashup in the WSO2 Mashup Server 2.0.2 or a newer one.
getAPIKey.inputTypes = { "username" : "string", "password" : "string", "url" : "string" };
getAPIKey.output = "string";
function getAPIKey(username, password, url) {
    var client = new HttpClient();
    var code = client.executeMethod("GET", "https://www.google.com/accounts/Login");
    if (code == 200) {
        var galx = client.cookies[0].value;
        var content = [
            { name : "Email", value : username },
            { name : "Passwd", value : password },
            { name : "signIn", value : "Sign in" },
            { name : "GALX", value : String(galx) },
            { name : "dsh", value : "5537526595243201224"},
            { name : "rmShown", value : "1"},
            { name : "PersistentCookie", value : "yes"}
        ];
        code = client.executeMethod("POST", "https://www.google.com/accounts/LoginAuth", content);
        if (code == 302 || code == 200) {
            code = client.executeMethod("GET", "http://code.google.com/apis/maps/signup/createkey", [
                { name : "referer", value : url }
            ]);
            if (code == 200) {
                var response = eval('(' + client.response + ')');
                client.releaseConnection();
                return response.generated_key;
            } else {
                client.releaseConnection();
                return new XML("" + code + "" + client.statusText + "");
            }
        } else {
            client.releaseConnection();
            return new XML("" + code + "" + client.statusText + "");
        }
    } else {
        client.releaseConnection();
        return new XML("" + code + "" + client.statusText + "");
    }
}

Friday, April 23, 2010

Subversion: How to revert to a previous revision

Assume that your current revision is 100 and you want to revert the repository to the revision 90. Then you can use the following command,
$ svn merge -rHEAD:90 .
As you have used . at the end, it will revert your local repository. Once commited you will get a new revision such as 101 which is same as revision 90. You can do this directly to a remote repository as well. In that case, replace . with repository url.

Wednesday, April 7, 2010

GCE O/L, A/L and Grade V Examination Results

Do you want to view O/L, A/L and Grade V results a bit earlier than http://www.doenets.lk lets us. I have tried it when it says, "New Await Results". You can do this using firebug addon on firefox.

www.doenets.lk lets us to check previous result too. These are displayed by the "New Await Results" table. So what you need to do is first select one of those forms and change action url to exam_OL.jsp, exam_AL.jsp or exam_GV.jsp as appropriate. You would also need to change the size/maxLength of the inputput field as well. Otherwise it won't let us to insert our full index number. That's all, fill it with your index number, submit and get your results. You can also do a http POST using your own form as well.

Attention : I am not responsible for the accuracy of results as www.doenets.lk might not have officialy released the results when you use this.

Sunday, March 21, 2010

How to enable mod rewrite on Apache

Tired up trying to enable mode rewrite on apache? Try this link which explains it quite well.
http://drupal.org/node/134439

Sunday, December 6, 2009

Serialize Javascript to PHP

When interacting Javascript with PHP, you might need to serialise any Javascript data type into the PHP serialized format, where it can be unserialized using PHP. The following function will serialize any Javascript data.
function serialize(mixed_value) {
    var _getType = function(inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';

    switch (type) {
        case "function":
            val = "";
            break;
        case "null":
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") {
                    continue;
                }

                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}
The code fragment below will do the serializing in Javascript and the later code will unserialize it in PHP.
var array = ["ruchira", "wageesha"];
//serialize value
var serialize_array = serialize(array);
$array = unserialize($_POST['array']);

Friday, September 11, 2009

Javascript Popup - close after redirect

If you want to redirect the popup window to another location (a page or a file download) and then close the window, you can simply use the following javascript.
<script type="text/javascript">
    window.opener.location = "new_page.htm";
    window.close();
</script>

Sunday, August 30, 2009

Find and Replace using grep and sed

Following command finds all the occurences of <br> in all .xml files in the current directory, and replace those <br> with <br/> . Please note that < and > have been surrounded with [ and ] in the sed command. / in the <br/> tag has been escaped using \. If you want to replace foo with bar, then you can just use those words.
find . -iname "*.xml" | xargs grep -l "<br>" | xargs sed -i -e 's/[<]br[>]/<br\/>/g'
find . -iname "*.xml" | xargs grep -l "<br>" | xargs sed -i -e 's/foo/bar/g'
A more general form of the expression would be
find . \( -name "*.php" -or -name "*.html" \) | xargs grep -l 'TEXT TO SEARCH' | xargs sed -i -e 's/TEXT TO REPLACE/REPLACEMENT STRING/g'