Class: Ezproxy

Inherits:
Service show all
Defined in:
lib/service_adaptors/ezproxy.rb

Constant Summary

Constants inherited from Service

Service::LinkOutFilterTask, Service::StandardTask

Instance Attribute Summary

Attributes inherited from Service

#name, #priority, #request, #service_id, #session_id, #status, #task, #url

Instance Method Summary collapse

Methods inherited from Service

#credits, #display_name, #handle_wrapper, #preempted_by, required_config_params, #response_to_view_data, #response_url, #service_types_generated, #view_data_from_service_type

Constructor Details

#initialize(config) ⇒ Ezproxy

Returns a new instance of Ezproxy.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/service_adaptors/ezproxy.rb', line 33

def initialize(config)
  @precheck_with_api = true
  @display_name = "EZProxy"
  @proxy_login_path = "/login"
  
  super(config)
  
  
  @proxy_url_path ||= "/proxy_url"
  @proxy_url_path = "/" + @proxy_url_path unless @proxy_url_path[0,1] = '/'

  @exclude ||= []
end

Instance Method Details

#already_proxied(url) ⇒ Object

pass in url as a string. Return true if the url is already pointing to the proxy server configured.



165
166
167
168
169
# File 'lib/service_adaptors/ezproxy.rb', line 165

def already_proxied(url)
  uri_obj = URI.parse(url)

  return uri_obj.host == @proxy_server && uri_obj.path == @proxy_login_path
end

#auto_proxy_url(url) ⇒ Object

pass in a url, this just mindlessly sends it through your ezproxy instance.



114
115
116
# File 'lib/service_adaptors/ezproxy.rb', line 114

def auto_proxy_url(url)
  return "http://" + @proxy_server + @proxy_login_path + "?qurl=" + CGI.escape(url)
end

#check_proxy_urls(urls) ⇒ Object

Pass in an array of URLs. Will determine if they are proxyable by EZProxy. Returns a hash, where the key is the original URL, and the value is the proxied url—or nil if could not be proxied.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/service_adaptors/ezproxy.rb', line 121

def check_proxy_urls(urls)
  url_doc = REXML::Document.new
  doc_root = url_doc.add_element "proxy_url_request", {"password"=>@proxy_password}
  urls_elem = doc_root.add_element "urls"
  urls.each { | link |
    url_elem = urls_elem.add_element "url"
    url_elem.text = link
  }
  begin
    resp = Net::HTTP.post_form(URI.parse('http://' + @proxy_server+@proxy_url_path), {"xml"=>url_doc.to_s})    
    proxy_doc = REXML::Document.new resp.body
  rescue Timeout::Error
    Rails.logger.error "Timed out connecting to EZProxy"
    return proxy_links
  rescue Exception => e
    Rails.logger.error "EZProxy error, NOT proxying URL + #{e}"
  end

  return_hash = {}
  REXML::XPath.each(proxy_doc, "/proxy_url_response/proxy_urls/url") { | u |
    unless (u && u.get_text) # if u is empty... weird, but skip it.
      Rails.logger.error "EZProxy response seems to be missing some pieces.\n   Urls requested: #{urls.join(',')}\n   EZProxy api request xml: #{url_doc.to_s}\n   EZProxy response: #{proxy_doc.to_s}"
    end
    orig_url = u.get_text.value
    return_hash[orig_url] = nil
  
    if u.attributes["proxy"] == "true"
      proxied_url = u.attributes["scheme"]+"://"+u.attributes["hostname"]+":"+u.attributes["port"]+u.attributes["login_path"]
      if u.attributes["encode"] == "true"
        proxied_url += CGI::escape(u.get_text.value)
      else
        proxied_url += u.get_text.value
      end

      return_hash[orig_url] = proxied_url
              
    end
  }    
  return return_hash
end

#excluded?(url) ⇒ Boolean

see @exclude config parameter.

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/service_adaptors/ezproxy.rb', line 92

def excluded?(url)    
  return false if @exclude.blank?
  
  @exclude.each do |entry|
    if ((entry[0,1] == '/') && (entry[entry.length()-1 ,1 ] == '/'))
      # regexp. Match against entire url. 
      re = Regexp.new( entry )
      return true if re =~ url
    elsif (entry.kind_of? Regexp)
      return true if entry =~ url
    else
      # ordinary string. Just match against host.
      host = URI.parse(url).host
      return true if host == entry
    end      
  end
  # looped through them all, no match?
  return false
end

#handle(request) ⇒ Object

This is meant to be called as task:link_out_filter, it doesn’t have an implementation for handle, it implements link_out_filter() instead.



49
50
51
# File 'lib/service_adaptors/ezproxy.rb', line 49

def handle(request)
   raise "Not implemented."
end

Hook method called by Umlaut. Returns a proxied url if it should be proxied, or nil if the url can not or does not need to be proxied.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/service_adaptors/ezproxy.rb', line 56

def link_out_filter(orig_url, service_response, other_args = {})
  # remove trailing or leading whitespace from url, it makes it
  # an illegal URL anyway, but maybe we can rescue it? Marc 856's
  # sometimes have accidental trailing whitespace. 
  orig_url = orig_url.strip
  
  # bad uri? Forget it.    
  return nil unless valid_url?( orig_url )
  
  # If it's already proxied, leave it alone.
  return nil if already_proxied(orig_url)

  return nil if excluded?(orig_url)

  new_url = nil
  if @precheck_with_api
    new_url =  check_proxy_urls( [orig_url] ).values[0]
  else
    new_url =  auto_proxy_url(orig_url)
  end
  
  return new_url
end

#valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
# File 'lib/service_adaptors/ezproxy.rb', line 80

def valid_url?(url)
  begin
    raise Exception.new("Empty url!") if url.blank?
    URI.parse( url )
    return true
  rescue Exception => e
    Rails.logger.error("Bad uri sent to ezproxy service. Can not parse. url: <#{url}>")
    return false
  end
end