Created
July 24, 2019 15:02
-
-
Save jonathangreen/ee947eeacc9f9fd30ab0120a6fb40592 to your computer and use it in GitHub Desktop.
Cantaloupe Islandora Lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require 'java' | |
require 'monitor' | |
class CustomDelegate < Monitor | |
attr_accessor :context | |
def shared_context | |
synchronize do | |
if ($shared_context == nil) | |
logger = Java::edu.illinois.library.cantaloupe.script.Logger | |
logger.debug('Initializing shared context') | |
config = YAML.load_file('sites.yml') | |
$shared_context = Hash.new | |
config.each do |name, data| | |
logger.debug("Loading #{name}") | |
$shared_context[name] = Hash.new | |
$shared_context[name]['config'] = data | |
$shared_context[name]['pool'] = com.zaxxer.hikari.HikariDataSource.new() | |
$shared_context[name]['pool'].setJdbcUrl("jdbc:mysql://#{data['db_host']}/#{data['db_name']}?autoReconnect=true&useSSL=false") | |
$shared_context[name]['pool'].setUsername(data['db_name']) | |
$shared_context[name]['pool'].setPassword(data['db_pass']) | |
end | |
end | |
end | |
return $shared_context | |
end | |
## | |
# @param options [Hash] Empty hash. | |
# @return [String,Hash<String,String>,nil] String URI; Hash with `uri` key, | |
# and optionally `username` and `secret` keys; or nil if not found. | |
# | |
def httpsource_resource_info(options = {}) | |
logger = Java::edu.illinois.library.cantaloupe.script.Logger | |
values = context['identifier'].split(':') | |
if values.length != 4 | |
logger.debug('Returning NIL because there are not enough arguements.') | |
return nil | |
end | |
site = values[0] | |
dsid = values[3] | |
pid = values[1,2].join(':') | |
logger.debug("Site: #{site}. PID: #{pid}. DSID: #{dsid}.") | |
if not self.shared_context.key?(site) | |
logger.error("Site `#{site}` Does Not Exist.") | |
return nil | |
end | |
site_config = self.shared_context[site]['config'] | |
pool = self.shared_context[site]['pool'] | |
uri = { 'uri' => "#{site_config['fedora_uri']}/objects/#{pid}/datastreams/#{dsid}/content"} | |
if not context['request_headers'].key?('X-ISLANDORA-TOKEN') | |
token = '' | |
logger.debug('No token sent.') | |
user = 'anonymous' | |
pass = 'anonymous' | |
else | |
token = context['request_headers']['X-ISLANDORA-TOKEN'] | |
logger.debug("Token #{token}.") | |
begin | |
connection = pool.getConnection() | |
query = "SELECT token.token, token.time, user.name, user.pass, user.uid | |
FROM islandora_authtokens AS token | |
INNER JOIN users AS user ON user.uid=token.uid | |
WHERE token.token=?" | |
statement = connection.prepare_statement(query) | |
statement.setString(1, token) | |
results = statement.execute_query | |
if results.next and results.getInt('user.uid') != 0 | |
user = results.getString('user.name') | |
pass = results.getString('user.pass') | |
else | |
user = 'anonymous' | |
pass = 'anonymous' | |
end | |
ensure | |
statement.close | |
connection.close | |
end | |
end | |
uri['username'] = user | |
uri['secret'] = pass | |
logger.info(uri.inspect) | |
return uri | |
end | |
def redirect(options = {}) | |
end | |
def authorized?(options = {}) | |
true | |
end | |
def extra_iiif2_information_response_keys(options = {}) | |
{} | |
end | |
def source(options = {}) | |
end | |
def azurestoragesource_blob_key(options = {}) | |
end | |
def filesystemsource_pathname(options = {}) | |
end | |
def jdbcsource_database_identifier(options = {}) | |
end | |
def jdbcsource_media_type(options = {}) | |
end | |
def jdbcsource_lookup_sql(options = {}) | |
end | |
def s3source_object_info(options = {}) | |
end | |
def overlay(options = {}) | |
end | |
def redactions(options = {}) | |
[] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment