Skip to content

Commit

Permalink
Delegation Viewer to FLAT works now #48
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Jan 4, 2017
1 parent b9375ac commit 01794ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
3 changes: 1 addition & 2 deletions clam/clamservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,6 @@ def getoutputfile(project, filename, credentials=None): #pylint: disable=too-man
viewer = v
if viewer:
output = viewer.view(outputfile, **flask.request.values)
print("TYPE", type(output),file=sys.stderr)
if isinstance(output, (flask.Response, werkzeug.wrappers.Response)):
return output
else:
Expand Down Expand Up @@ -2227,7 +2226,7 @@ def do( actionid, method, user="anonymous", oauth_access_token=""): #pylint: dis
raise
else:
return flask.make_response(e,500)
if not isinstance(r, flask.Response):
if not isinstance(r, (flask.Response, werkzeug.wrappers.Response)):
if sys.version >= '3':
return withheaders(flask.make_response(str(r)), action.mimetype)
else:
Expand Down
41 changes: 22 additions & 19 deletions clam/common/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,10 @@ def __init__(self, **kwargs):
value = bool(value)
self.embed = value

def url(self, file):
"""Returns the URL to view this resource, the link is usually an external service/website. If necessary, the file can be 'pushed' to the service from here. file is a CLAMOutputFile instance"""
return False

def view(self, file, **kwargs):
"""Returns the view itself, in xhtml (it's recommended to use flask's template system!). file is a CLAMOutputFile instance. By default, if not overriden and a remote service is specified, this issues a GET to the remote service."""
url = self.url(file)
if url: #is the resource external?
if self.embed:
#fetch
if kwargs:
req = requests.get(url,data=kwargs)
else:
req = requests.get(url)
return withheaders(flask.make_reponse(req.iter_lines()), self.mimetype)
else:
#redirect
return flask.redirect(url)
raise NotImplementedError


class SimpleTableViewer(AbstractViewer):
Expand Down Expand Up @@ -197,14 +183,31 @@ def __init__(self, **kwargs):
else:
self.url = ''

if 'configuration' in kwargs:
self.configuration = kwargs['configuration']
del kwargs['configuration']
else:
self.configuration = ''

if 'mode' in kwargs:
self.mode = kwargs['mode']
del kwargs['mode']
else:
self.mode = ''

super(FLATViewer,self).__init__(**kwargs)

def view(self, file, **kwargs):
#filename will contain a random component to prevent clashes
filename = os.path.basename(file.filename).replace('.folia.xml','').replace('.xml','') + str("%034x" % random.getrandbits(128)) + '.folia.xml'
r = requests.post(self.url + '/pub/upload/', allow_redirects=False, files=[('file',(filename,file,'application/xml'))])
r = requests.post(self.url + '/pub/upload/', allow_redirects=False, files=[('file',(filename,file,'application/xml'))], data={'configuration':self.configuration,'mode':self.mode})
#FLAT redirects after upload, we catch the redirect rather than following it automatically following it, and return it ourselves as our redirect
if 'location' in r.headers:
return flask.redirect(r.headers['location'])
if 'Location' in r.headers:
if self.url and self.url[-1] == '/' and r.headers['Location'][0] == '/':
url = self.url[:-1] + r.headers['Location']
else:
url = self.url + r.headers['Location']
return flask.redirect(url)
else:
return str(r)
return "Invalid response from FLAT"

0 comments on commit 01794ab

Please sign in to comment.