Convert cloudbar api responses to JSONP
authorKostas Papadimitriou <kpap@grnet.gr>
Wed, 29 Feb 2012 16:19:02 +0000 (18:19 +0200)
committerKostas Papadimitriou <kpap@grnet.gr>
Thu, 1 Mar 2012 10:09:53 +0000 (12:09 +0200)
if callback parameter is set on url use it and set the appropriate
response mimetype.

snf-astakos-app/astakos/im/api.py
snf-astakos-app/astakos/im/static/im/cloudbar/cloudbar.js

index 6ef50a1..4df9f84 100644 (file)
@@ -1,18 +1,18 @@
 # Copyright 2011-2012 GRNET S.A. All rights reserved.
-# 
+#
 # Redistribution and use in source and binary forms, with or
 # without modification, are permitted provided that the following
 # conditions are met:
-# 
+#
 #   1. Redistributions of source code must retain the above
 #      copyright notice, this list of conditions and the following
 #      disclaimer.
-# 
+#
 #   2. Redistributions in binary form must reproduce the above
 #      copyright notice, this list of conditions and the following
 #      disclaimer in the documentation and/or other materials
 #      provided with the distribution.
-# 
+#
 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
@@ -25,7 +25,7 @@
 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 # POSSIBILITY OF SUCH DAMAGE.
-# 
+#
 # The views and conclusions contained in the software and
 # documentation are those of the authors and should not be
 # interpreted as representing official policies, either expressed
@@ -48,7 +48,7 @@ from astakos.im.settings import CLOUD_SERVICES, INVITATIONS_ENABLED
 def render_fault(request, fault):
     if isinstance(fault, InternalServerError) and settings.DEBUG:
         fault.details = format_exc(fault)
-    
+
     request.serialization = 'text'
     data = fault.message + '\n'
     if fault.details:
@@ -68,20 +68,20 @@ def authenticate(request):
         x_auth_token = request.META.get('HTTP_X_AUTH_TOKEN')
         if not x_auth_token:
             return render_fault(request, BadRequest('Missing X-Auth-Token'))
-        
+
         try:
             user = AstakosUser.objects.get(auth_token=x_auth_token)
         except AstakosUser.DoesNotExist, e:
-            return render_fault(request, Unauthorized('Invalid X-Auth-Token')) 
-        
+            return render_fault(request, Unauthorized('Invalid X-Auth-Token'))
+
         # Check if the is active.
         if not user.is_active:
             return render_fault(request, Unauthorized('User inactive'))
-        
+
         # Check if the token has expired.
         if (time() - mktime(user.auth_token_expires.timetuple())) > 0:
             return render_fault(request, Unauthorized('Authentication expired'))
-        
+
         response = HttpResponse()
         response.status=204
         user_info = {'username':user.username,
@@ -100,8 +100,16 @@ def authenticate(request):
 def get_services(request):
     if request.method != 'GET':
         raise BadRequest('Method not allowed.')
+
+    callback = request.GET.get('callback', None)
     data = json.dumps(CLOUD_SERVICES)
-    return HttpResponse(content=data, mimetype="application/json")
+    mimetype = 'application/json'
+
+    if callback:
+        mimetype = 'application/javascript'
+        data = '%s(%s)' % (callback, data)
+
+    return HttpResponse(content=data, mimetype=mimetype)
 
 def get_menu(request):
     if request.method != 'GET':
@@ -128,5 +136,13 @@ def get_menu(request):
                   'name': "feedback..." })
         l.append({ 'url': absolute(reverse('astakos.im.views.logout')),
                   'name': "logout..."})
+
+    callback = request.GET.get('callback', None)
     data = json.dumps(tuple(l))
-    return HttpResponse(content=data, mimetype="application/json")
\ No newline at end of file
+    mimetype = 'application/json'
+
+    if callback:
+        mimetype = 'application/javascript'
+        data = '%s(%s)' % (callback, data)
+
+    return HttpResponse(content=data, mimetype=mimetype)
index 1cb5e31..23726ad 100644 (file)
@@ -36,8 +36,10 @@ $(document).ready(function(){
     var services = $('<div class="services"></div>');
     var profile = $('<div class="profile"></div>');
     
+    var get_services_url = window.GET_SERVICES_URL || window.CLOUDBAR_SERVICES;
+    
     // create services links and set the active class to the current service
-    $.getJSON(window.GET_SERVICES_URL || window.CLOUDBAR_SERVICES, function(data) {
+    $.getJSON(get_services_url + "?callback=?", function(data) {
             $.each(data, function(i, el){
             var slink = $("<a>");
             if (el.icon) {
@@ -58,8 +60,8 @@ $(document).ready(function(){
     var user = $('<div class="user"></div>');    
     var username = $('<a href="#"></a>');
     var usermenu = $("<ul>");
-    var get_menu_url = (window.GET_MENU_URL || window.CLOUDBAR_MENU).concat('?location=').concat(window.location.toString());
-    $.getJSON(get_menu_url, function(data) {
+    var get_menu_url = (window.GET_MENU_URL || window.CLOUDBAR_MENU) + '?callback=?&location=' + window.location.toString();
+    $.getJSON(get_menu_url + "&callback=?", function(data) {
         $.each(data, function(i,el) {
             if (i == 0){
                 username.text(el.name);