'Access to external display applications'

@web.expose
def display_application(self, trans, dataset_id=None, user_id=None, app_name=None, link_name=None, app_action=None, action_param=None, action_param_extra=None, **kwds):

    app_kwds = {}
    for (name, value) in dict(kwds).iteritems():
        if name.startswith('app_'):
            app_kwds[name[len('app_'):]] = value
            del kwds[name]
    if kwds:
        log.debug(('Unexpected    Keywords    passed    to    display_application:    %s' % kwds))
    (data, user) = decode_dataset_user(trans, dataset_id, user_id)
    if (not data):
        raise paste.httpexceptions.HTTPRequestRangeNotSatisfiable(('Invalid    reference    dataset    id:    %s.' % str(dataset_id)))
    if (user is None):
        user = trans.user
    if user:
        user_roles = user.all_roles()
    else:
        user_roles = []
    app_name = urllib.unquote_plus(app_name)
    link_name = urllib.unquote_plus(link_name)
    if (None in [app_name, link_name]):
        return trans.show_error_message('A    display    application    name    and    link    name    must    be    provided.')
    if self._can_access_dataset(trans, data, additional_roles=user_roles):
        msg = []
        preparable_steps = []
        refresh = False
        display_app = trans.app.datatypes_registry.display_applications.get(app_name)
        if (not display_app):
            log.debug('Unknown    display    application    has    been    requested:    %s', app_name)
            return paste.httpexceptions.HTTPNotFound(('The    requested    display    application    (%s)    is    not    available.' % app_name))
        (dataset_hash, user_hash) = encode_dataset_user(trans, data, user)
        try:
            display_link = display_app.get_link(link_name, data, dataset_hash, user_hash, trans, app_kwds)
        except Exception as e:
            log.debug('Error    generating    display_link:    %s', e)
            return paste.httpexceptions.HTTPConflict(('Error    generating    display_link:    %s' % e))
        if (not display_link):
            log.debug('Unknown    display    link    has    been    requested:    %s', link_name)
            return paste.httpexceptions.HTTPNotFound(('Unknown    display    link    has    been    requested:    %s' % link_name))
        if (data.state == data.states.ERROR):
            msg.append(('This    dataset    is    in    an    error    state,    you    cannot    view    it    at    an    external    display    application.', 'error'))
        elif data.deleted:
            msg.append(('This    dataset    has    been    deleted,    you    cannot    view    it    at    an    external    display    application.', 'error'))
        elif (data.state != data.states.OK):
            msg.append(('You    must    wait    for    this    dataset    to    be    created    before    you    can    view    it    at    an    external    display    application.', 'info'))
            refresh = True
        elif display_link.display_ready():
            if (app_action in ['data', 'param']):
                assert action_param, 'An    action    param    must    be    provided    for    a    data    or    param    action'
                try:
                    action_param = display_link.get_param_name_by_url(action_param)
                except ValueError as e:
                    log.debug(e)
                    return paste.httpexceptions.HTTPNotFound(str(e))
                value = display_link.get_param_value(action_param)
                assert value, ('An    invalid    parameter    name    was    provided:    %s' % action_param)
                assert value.parameter.viewable, 'This    parameter    is    not    viewable.'
                if (value.parameter.type == 'data'):
                    try:
                        if action_param_extra:
                            assert value.parameter.allow_extra_files_access, ('Extra    file    content    requested    (%s),    but    allow_extra_files_access    is    False.' % action_param_extra)
                            file_name = os.path.join(value.extra_files_path, action_param_extra)
                        else:
                            file_name = value.file_name
                        content_length = os.path.getsize(file_name)
                        rval = open(file_name)
                    except OSError as e:
                        log.debug('Unable    to    access    requested    file    in    display    application:    %s', e)
                        return paste.httpexceptions.HTTPNotFound('This    file    is    no    longer    available.')
                else:
                    rval = str(value)
                    content_length = len(rval)
                trans.response.set_content_type(value.mime_type(action_param_extra=action_param_extra))
                trans.response.headers['Content-Length'] = content_length
                return rval
            elif (app_action is None):
                trans.response.headers['Cache-Control'] = ['no-cache', 'max-age=0', 'no-store', 'must-revalidate']
                return trans.response.send_redirect(display_link.display_url())
            else:
                msg.append((('Invalid    action    provided:    %s' % app_action), 'error'))
        elif (app_action is None):
            if (trans.history != data.history):
                msg.append(('You    must    import    this    dataset    into    your    current    history    before    you    can    view    it    at    the    desired    display    application.', 'error'))
            else:
                refresh = True
                msg.append(('Launching    this    display    application    required    additional    datasets    to    be    generated,    you    can    view    the    status    of    these    jobs    below.    ', 'info'))
                if (not display_link.preparing_display()):
                    display_link.prepare_display()
                preparable_steps = display_link.get_prepare_steps()
        else:
            raise Exception(('Attempted    a    view    action    (%s)    on    a    non-ready    display    application' % app_action))
        return trans.fill_template_mako('dataset/display_application/display.mako', msg=msg, display_app=display_app, display_link=display_link, refresh=refresh, preparable_steps=preparable_steps)
    return trans.show_error_message('You    do    not    have    permission    to    view    this    dataset    at    an    external    display    application.')
