add 'proxify_url' to integration API; provide ezproxy implementation for Leddy.
authorgfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Sat, 2 Oct 2010 23:54:42 +0000 (23:54 +0000)
committergfawcett <gfawcett@6d9bc8c9-1ec2-4278-b937-99fde70a366f>
Sat, 2 Oct 2010 23:54:42 +0000 (23:54 +0000)
git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@1032 6d9bc8c9-1ec2-4278-b937-99fde70a366f

conifer/integration/uwindsor.py
conifer/libsystems/ezproxy.py [new file with mode: 0644]
conifer/syrup/integration.py
conifer/syrup/models.py

index 30506b2..a459f33 100644 (file)
@@ -2,6 +2,7 @@
 
 from datetime import date
 from django.conf import settings
+from conifer.libsystems import ezproxy
 from conifer.libsystems.evergreen.support import initialize, E1
 from conifer.libsystems import marcxml as M
 from conifer.libsystems.evergreen import item_status as I
@@ -169,3 +170,18 @@ def external_memberships(userid, include_titles=False):
     for m in memberships:
         m['role'] = decode_role(m['role'])
     return memberships
+
+#--------------------------------------------------
+# proxy server integration
+
+ezproxy_service = ezproxy.EZProxyService(
+    settings.UWINDSOR_EZPROXY_HOST,
+    settings.UWINDSOR_EZPROXY_PASSWORD)
+
+def proxify_url(url):
+    """
+    Given a URL, determine whether the URL needs to be passed through
+    a reverse-proxy, and if so, return a modified URL that includes
+    the proxy. If not, return None.
+    """
+    return ezproxy_service.proxify(url)
diff --git a/conifer/libsystems/ezproxy.py b/conifer/libsystems/ezproxy.py
new file mode 100644 (file)
index 0000000..9729b55
--- /dev/null
@@ -0,0 +1,48 @@
+import re
+from xml.etree import ElementTree
+from urllib    import urlencode
+from urllib2   import *
+
+
+class EZProxyService(object):
+
+    def __init__(self, host, password, strict=False):
+        self.host = host
+        self.password = password
+        self.query_url = 'http://%s/proxy_url' % host
+        self.strict = strict
+
+    def proxify(self, url):
+        if not url:             # empty or blank
+            return None
+        if not self.strict:
+            # If the hostname is in the URL, assume it has already
+            # been proxified.
+            if self.host in url:
+                return None
+        xml = ('<proxy_url_request password="%s">'
+               '<urls><url>%s</url>'
+               '</urls></proxy_url_request>') % (
+            self.password, url)
+        data = urlencode({'xml':xml})
+        resp = urlopen(self.query_url, data).read()
+        root = ElementTree.fromstring(resp)
+        node = root.find('proxy_urls/url')
+        needs_proxy = (node.attrib.get('proxy') == 'true')
+        if needs_proxy:
+            return self.translate(url, **node.attrib)
+
+    pat = re.compile(r'(.*://[^/]+)(.*)$')
+
+    def translate(self, url, **kwargs):
+        prefix, suffix = self.pat.match(url).groups()
+        return ''.join((prefix, '.', self.host, suffix))
+    
+
+
+host = 'ezproxy.uwindsor.ca'
+pwd = 'leddy'
+s = EZProxyService(host, pwd)
+
+print s.proxify('http://jstor.org/')
+print s.proxify('http://jstor.org.ezproxy.uwindsor.ca/')
index eff6b98..b55f512 100644 (file)
@@ -120,3 +120,13 @@ def user_needs_decoration(user_obj):
     'external_person_lookup,' is used by Syrup to fetch the personal
     information when needed.
     """
+
+@disable
+def proxify_url(url):
+    """
+    Given a URL, determine whether the URL needs to be passed through
+    a reverse-proxy, and if so, return a modified URL that includes
+    the proxy. If not, return None.
+    """
+
+                  
index 98ab28f..040dadc 100644 (file)
@@ -565,6 +565,8 @@ class Item(BaseModel):
             maybe_bib = callhook('marc_to_bib_id', self.marcxml)
             if maybe_bib:
                 self.bib_id = maybe_bib
+        # proxify the item's URL if necessary
+        self.url = callhook('proxify_url', self.url) or self.url
         super(Item, self).save(*args, **kwargs)
 
     #--------------------------------------------------