Every day, the more I learn new stuff,the more I feel exited !!!
Last week I had a delicious bite from {Jaggery.js} and let me share a bit with you all. :)
First we need to setup a {Jaggery.js} server to host the client application we are about to write.
Setting up Jaggery
- Download {Jaggery.js} server from {Jaggery.js} site.
- Extract jaggery-0.9.0-SNAPSHOT.zip to a directory you prefer. lets call it JAGGERY_HOME
- Navigate to JAGGERY_HOME/bin directory which contains all the Jaggery execution scripts.
- Run sh JAGGERY_HOME/bin/server.sh ( JAGGERY_HOME/bin/server.bat in windows) command to start the server.
Writing the Jaggery Client
- Goto JAGGERY_HOME/apps directory.
- Create directory JaggeryClient inside JAGGERY_HOME/apps.
- Create secureClient.jag and Base64Encode.jag files inside JAGGERY_HOME/apps/JaggeryClient directory.
- secureClient.jag file content is as follows.
- </%function secure_call(request){
- var Base64Encode = require("Base64Encode.jag");
- var username = request.username;
- var password = request.password;
- var url = request.url;
- var data = request.data;
- var code = "Basic " + username + ":" + password;;
- var auth = Base64Encode.base64_encode(code);
- var header = {"Authorization":auth};
- var repleyJson = get(url, data ,header,"json");
- response.content = repleyJson;
- }
- %>
- Above request filed is in JSON format as follows.
{ username:"username",password:"password", url:"url of the resource", data:"" }
- Jaggery GET method is used to access the rest resource and Basic Authentication username and password are sent, encoded in the header.
- Base64Encode file content is as follows. [source: http://phpjs.org/functions/base64_encode/]
- function base64_encode(data) {
- var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
- var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
- ac = 0,
- enc = '',
- tmp_arr = [];
- if (!data) {
- return data;
- }
- do { // pack three octets into four hexets
- o1 = data.charCodeAt(i++);
- o2 = data.charCodeAt(i++);
- o3 = data.charCodeAt(i++);
- bits = o1 << 16 | o2 << 8 | o3;
- h1 = bits >> 18 & 0x3f;
- h2 = bits >> 12 & 0x3f;
- h3 = bits >> 6 & 0x3f;
- h4 = bits & 0x3f;
- // use hexets to index into b64, and append result to encoded string
- tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
- } while (i < data.length);
- enc = tmp_arr.join('');
- var r = data.length % 3;
- return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
- }
- %>
- Now we are done with the Jaggery Client.
Accessing secured resource via the Client
- Next we try it with a secured REST Resource as shown in following secureCall.jag.
- <%
- var include = require("secureClient.jag");
- include.secure_call({ username:"replace_with_valid_username",password:"replace_with_valid_password", url:"replace_with_valid_url_to secured source", data:"" });
- %>
- place above secureCall.jag file in JAGGERY_HOME/apps/JaggeryClient folder and goto http://localhost:9763/JaggerySecureClient/secureCall.jag to see whether it works.
Hope you enjoyed the taste.
No comments:
Post a Comment