From 137d999da3e3724c5fdb281cfa83fb5b5f3b8052 Mon Sep 17 00:00:00 2001 From: gfawcett Date: Sat, 2 Oct 2010 23:54:42 +0000 Subject: [PATCH] add 'proxify_url' to integration API; provide ezproxy implementation for Leddy. git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@1032 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/integration/uwindsor.py | 16 ++++++++++++++ conifer/libsystems/ezproxy.py | 48 +++++++++++++++++++++++++++++++++++++++++ conifer/syrup/integration.py | 10 +++++++++ conifer/syrup/models.py | 2 ++ 4 files changed, 76 insertions(+) create mode 100644 conifer/libsystems/ezproxy.py diff --git a/conifer/integration/uwindsor.py b/conifer/integration/uwindsor.py index 30506b2..a459f33 100644 --- a/conifer/integration/uwindsor.py +++ b/conifer/integration/uwindsor.py @@ -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 index 0000000..9729b55 --- /dev/null +++ b/conifer/libsystems/ezproxy.py @@ -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 = ('' + '%s' + '') % ( + 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/') diff --git a/conifer/syrup/integration.py b/conifer/syrup/integration.py index eff6b98..b55f512 100644 --- a/conifer/syrup/integration.py +++ b/conifer/syrup/integration.py @@ -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. + """ + + diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index 98ab28f..040dadc 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -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) #-------------------------------------------------- -- 2.11.0