changeset 623:6012fe93f78c

better scheme-less URL code.
author casties
date Mon, 15 Dec 2014 16:10:05 +0100
parents bc68ca0d2c0a
children 80a0191ae51c
files SrvTxtUtils.py documentViewer.py
diffstat 2 files changed, 41 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/SrvTxtUtils.py	Fri Dec 12 16:27:58 2014 +0100
+++ b/SrvTxtUtils.py	Mon Dec 15 16:10:05 2014 +0100
@@ -7,6 +7,7 @@
 import os
 import stat
 import urllib
+from urlparse import urlparse, urlunparse
 import logging
 import time
 import re
@@ -21,7 +22,7 @@
 
 import xml.etree.ElementTree as ET
 
-srvTxtUtilsVersion = "1.12.3"
+srvTxtUtilsVersion = "1.13"
 
 map_months = {'en': [u"",
                      u"January",
@@ -370,3 +371,35 @@
     return s
 
 
+def sslifyUrl(url, app=None, force=False):
+    """returns URL with http or https scheme.
+    
+    Looks at app.REQUEST.URL to find the scheme of the current page.
+    Changes only schemeless (starting with //) URLs unless force=True.  
+    """
+    thatUrl = urlparse(url) 
+    if hasattr(app, 'REQUEST'):
+        # get current page URL
+        thisUrl = urlparse(app.REQUEST['URL'])
+        if thatUrl.scheme == '':
+            # schemeless URL -> use this scheme
+            return "%s:%s"%(thisUrl.scheme, url)
+        elif force:
+            # use this scheme
+            if thisUrl.scheme != thatUrl.scheme:
+                return urlunparse((thisUrl.scheme,)+thatUrl[1:])
+            else:
+                # keep scheme
+                return url
+            
+        else:
+            # keep scheme
+            return url
+        
+    else:
+        # no current page URL
+        if force:
+            # use https for force
+            return urlunparse(('https',)+thatUrl[1:])
+        
+    return url
--- a/documentViewer.py	Fri Dec 12 16:27:58 2014 +0100
+++ b/documentViewer.py	Mon Dec 15 16:10:05 2014 +0100
@@ -16,14 +16,11 @@
 
 from Products.MetaDataProvider import MetaDataFolder
 
-from SrvTxtUtils import getInt, utf8ify, getText, getHttpData, refreshingImageFileIndexHtml
+from SrvTxtUtils import getInt, utf8ify, getText, getHttpData, refreshingImageFileIndexHtml, sslifyUrl
     
 
 def getMDText(node):
     """returns the @text content from the MetaDataProvider metadata node"""
-
-    
-
     if isinstance(node, dict):
         return node.get('@text', None)
     
@@ -34,8 +31,6 @@
                 return node.get('@text',None)
         return None
 
-
-
     return node
 
 def getParentPath(path, cnt=1):
@@ -173,15 +168,11 @@
             self.digilibScalerUrl = digilibBaseUrl + '/servlet/Scaler'
             self.digilibViewerUrl = digilibBaseUrl + '/jquery/digilib.html'
             
-        
+
     # proxy text server methods to fulltextclient
     def getTextPage(self, **args):
         """returns full text content of page"""
-       
         return self.template.fulltextclient.getTextPage(**args)
-    
-   
-   
 
     def getSearchResults(self, **args):
         """loads list of search results and stores XML in docinfo"""
@@ -233,7 +224,7 @@
             return "ERROR: template folder missing!"
                         
         if not self.digilibBaseUrl:
-            self.digilibBaseUrl = self.findDigilibUrl() or "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
+            self.digilibBaseUrl = self.findDigilibUrl() or "http://digilib.mpiwg-berlin.mpg.de/digitallibrary"
             
         docinfo = self.getDocinfo(mode=mode,url=url)
         #pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
@@ -267,8 +258,6 @@
             # this won't work
             logging.error("template folder missing!")
             return "ERROR: template folder missing!"
-        
-        
 
         if not getattr(self, 'digilibBaseUrl', None):
             self.digilibBaseUrl = self.findDigilibUrl() or "http://digilib.mpiwg-berlin.mpg.de/digitallibrary"
@@ -306,9 +295,6 @@
             # legacy fix
             viewMode = 'image'
             self.REQUEST['viewMode'] = 'image'
-            
-        
-            
 
         # safe viewLayer in userinfo
         userinfo['viewLayer'] = viewLayer
@@ -333,7 +319,7 @@
     def findDigilibUrl(self):
         """try to get the digilib URL from zogilib"""
         url = self.template.zogilib.getDLBaseUrl()
-        return url
+        return sslifyUrl(url, self, force=True)
     
     def getScalerUrl(self, fn=None, pn=None, dw=100, dh=100, docinfo=None):
         """returns URL to digilib Scaler with params"""
@@ -765,9 +751,6 @@
         # old style text URL
         textUrl = getMDText(texttool.get('text', None))
 
-        
-
-
         if textUrl and docPath:
             if urlparse.urlparse(textUrl)[0] == "": #keine url
                 textUrl = os.path.join(docPath, textUrl) 
@@ -1161,14 +1144,8 @@
     def getAnnotatorGroupsForUser(self, user, annotationServerUrl="http://tuxserve03.mpiwg-berlin.mpg.de/AnnotationManager"):
         """returns list of groups {name:*, id:*} on the annotation server for the user"""
         groups = []
-        if annotationServerUrl.startswith('//'):
-            # add matching http(s) from our URL
-            myUrl = self.REQUEST['URL']
-            logging.debug("my URL: %s"%myUrl)
-            if myUrl.startswith('https:'):
-                annotationServerUrl = "https:%s"%annotationServerUrl
-            else:
-                annotationServerUrl = "http:%s"%annotationServerUrl
+        # add matching http(s) from our URL
+        annotationServerUrl = sslifyUrl(annotationServerUrl, self)
             
         groupsUrl = "%s/annotator/groups?user=%s"%(annotationServerUrl,user)
         data = getHttpData(url=groupsUrl, noExceptions=True)
@@ -1205,6 +1182,7 @@
 
         if RESPONSE is not None:
             RESPONSE.redirect('manage_main')
+            
         
 def manage_AddDocumentViewerForm(self):
     """add the viewer form"""