Skip to content

Commit

Permalink
Implementing Forwarders #69
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Nov 19, 2018
1 parent 3559569 commit b14c0a4
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 7 deletions.
3 changes: 3 additions & 0 deletions clam/clamservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ def response(user, project, parameters, errormsg = "", datafile = False, oauth_a
accesstoken=Project.getaccesstoken(user,project),
interfaceoptions=settings.INTERFACEOPTIONS,
customhtml=customhtml,
forwarders=[ forwarder(project, getrooturl()) for forwarder in settings.FORWARDERS ],
allow_origin=settings.ALLOW_ORIGIN,
oauth_access_token=oauth_encrypt(oauth_access_token),
auth_type=auth_type()
Expand Down Expand Up @@ -2598,6 +2599,8 @@ def set_defaults():
settings.PROFILES = []
if 'INPUTSOURCES' not in settingkeys:
settings.INPUTSOURCES = []
if 'FORWARDERS' not in settingkeys:
settings.FORWARDERS = []
if 'PORT' not in settingkeys and not PORT:
settings.PORT = 80
if 'HOST' not in settingkeys and not HOST:
Expand Down
18 changes: 18 additions & 0 deletions clam/common/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,24 @@ def fromxml(node):
raise Exception("No condition found in ParameterCondition!")
return Action(*args, **kwargs)

class Forwarder(object):
def __init__(self, id, name, url, description="", type='zip'):
self.id = id
self.name = name
self.url = url
self.description = description
self.type = type

def __call__(self, project, baseurl, outputfile=None):
"""Return the forward link given a project and (optionally) an outputfile. If no outputfile was selected, a link is generator to download the entire output archive."""
if outputfile:
self.forwardlink = self.url.replace("$BACKLINK", outputfile.filename)
else:
self.forwardlink = self.url.replace("$BACKLINK", baseurl + '/' + project + '/output/' + self.type)
return self



def resolveinputfilename(filename, parameters, inputtemplate, nextseq = 0, project = None):
#parameters are local
if filename.find('$') != -1:
Expand Down
20 changes: 20 additions & 0 deletions clam/common/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ 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."""
raise NotImplementedError

class ForwardViewer(AbstractViewer):
"""The ForwardViewer calls a remote service and passes a backlink where the remote service can download an output file. The remote service is in turn expected to return a HTTP Redirect (302) response. It is implemented as a :class:`Forwarder`. See :ref:`forwarders`"""

def __init__(self, id, name, forwarder, **kwargs):
self.id = id
self.name = name
self.forwarder = forwarder
super(ForwardViewer,self).__init__(**kwargs)

def view(self, file, **kwargs):
self.forwarder(None, file)
r = requests.get(self.forwarder.forwardlink, allow_redirects=True)
if r.status_code == 302 and 'Location' in r.headers:
url = r.headers['Location']
return flask.redirect(url)
else:
return "Unexpected response from Forward service (HTTP " + str(r.status_code) + ", target was " + url + "): " + r.text




class SimpleTableViewer(AbstractViewer):
id = 'tableviewer'
Expand Down
7 changes: 7 additions & 0 deletions clam/config/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@
#Action(id='multiply',name='Multiply',parameters=[IntegerParameter(id='x',name='Value'),IntegerParameter(id='y',name='Multiplier'), function=lambda x,y: x*y ])
]

# ======= FORWARDERS =============

#Global forwarders call a remote service, passing a backlink for the remote service to download an archive of all the output data. The remote service is expected to return a redirect (HTTP 302) . CLAM will insert the backlink where you put $BACKLINK in the url:

#FORWARDERS = [
#Forwarder(id='otherservice', name="Other service", description="", url="https://my.service.com/grabfrom=$BACKLINK")
#]

# ======== DISPATCHING (ADVANCED! YOU CAN SAFELY SKIP THIS!) ========

Expand Down
13 changes: 13 additions & 0 deletions clam/static/interface.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
<xsl:template match="/clam/output">
<div id="output" class="box">
<h2>Output files</h2>

<p>(Download all as archive:
<xsl:choose>
<xsl:when test="/clam/@oauth_access_token = ''">
Expand All @@ -451,6 +452,18 @@
</xsl:otherwise>
</xsl:choose>
</p>

<xsl:if match="/clam/forwarders">
<p>
Forward all output to:
<ul>
<xsl:for-each select="/clam/forwarders">
<li><a href="{./url}"><xsl:value-of select="./name" /></a> - <xsl:value-of select="./description" /></li>
</xsl:for-each>
</ul>
</p>
</xsl:if>

<table id="outputfiles" class="files">
<thead>
<tr>
Expand Down
7 changes: 7 additions & 0 deletions clam/templates/response.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,11 @@
</input>
{% endif %}
{% endif %}
{% if statuscode == 2 and forwarders %}
<forwarders>
{% for forwarder in forwarders %}
<forwarder id="{{ forwarder.id }}" name="{{ forwarder.name }}" description="{{ forwarder.description }}" type="{{ forwarder.type }}" url="{{ forwarder.forwardlink }}" />
{% endfor %}
</forwarders>
{% endif %}
</clam>
54 changes: 54 additions & 0 deletions docs/source/serviceconfiguration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,13 @@ The following example defines a boolean parameter with a parameter flag:
Thus, if this parameter is set, the invoked command will have
``$PARAMETERS`` set to ``-l 1`` (plus any additional parameters).

Parameters API
~~~~~~~~~~~~~~~~~~~~~

.. automodule:: clam.common.parameters
:members:
:undoc-members:

.. _profile:

Profile specification
Expand Down Expand Up @@ -1082,6 +1089,8 @@ will be performed by 3rd party software in most cases.
Note that specific converters take specific parameters; consult the API
reference for details.

.. _viewers:

Viewers
~~~~~~~

Expand All @@ -1106,6 +1115,51 @@ The below example illustrates the use of the viewer
extension='.patterns.csv',
)
Another useful viewer is the :class:`ForwardViewer`. It forwards the viewing request to a remote service and passes a
backlink where the remote service can *download* the output file (assuming it has proper authorization!). The remote
service is expected to return a HTTP 302 Redirect response which CLAM will subsequently invoke.

.. code-block:: python
OutputTemplate('freqlist',CSVFormat,"Frequency list",
ForwardViewer(Forwarder(id="some_remote_service",name="Some Remote Frequency List Viewer")),
url="https://remote.service.com/?download=$BACKLINK")),
SetMetaField('encoding','utf-8'),
extension='.patterns.csv',
)
You can also use forwarders globally to redirect all output as an archive (zip/tar.gz/tar.bz2), see :ref:`forwarders`.


Viewer API
+++++++++++++++

.. automodule:: clam.common.viewers
:members:
:undoc-members:

.. _forwarders:

Forwarders
~~~~~~~~~~~~~~

To allow users to forward all output from one webservice to another, you can use Forwarders. The forwarder calls a remote service and passes a backlink where the remote service can *download* the output file (assuming it has proper authorization!). The remote
service is expected to return a HTTP 302 Redirect response which CLAM will subsequently invoke.

.. code-block:: python
FORWARDERS = [
Forwarder(id="some_remote_service",name="Some Remote service",type="zip", description="",
url="https://remote.service.com/?downloadarchive=$BACKLINK"
)
]

.. note::

* Forwarders can also be used as viewers for individual files. See :ref:`viewers`
* A forwarder does *NOT* perform any upload, it just passes a download link to a service, the remote
service must also have the necessary authorization to use it.

Working with pre-installed data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
7 changes: 0 additions & 7 deletions docs/source/viewers.rst

This file was deleted.

0 comments on commit b14c0a4

Please sign in to comment.