'Returns data directly into the browser.
Sets the mime-type according to the extension.
Used by the twill tool test driver - used anywhere else? Would like to drop hid
argument and path if unneeded now. Likewise, would like to drop encoded_id=XXX
and use assume id is encoded (likely id wouldn\'t be coming in encoded if this
is used anywhere else though.)'

@web.expose
def display(self, trans, id=None, hid=None, tofile=None, toext='.txt', encoded_id=None, **kwd):

    if (hid is not None):
        try:
            hid = int(hid)
        except:
            return ("hid    '%s'    is    invalid" % str(hid))
        history = trans.get_history()
        for dataset in history.datasets:
            if (dataset.hid == hid):
                data = dataset
                break
        else:
            raise Exception(("No    dataset    with    hid    '%d'" % hid))
    else:
        if (encoded_id and (not id)):
            id = self.decode_id(encoded_id)
        try:
            data = trans.sa_session.query(self.app.model.HistoryDatasetAssociation).get(id)
        except:
            return ("Dataset    id    '%s'    is    invalid" % str(id))
    if data:
        current_user_roles = trans.get_current_user_roles()
        if trans.app.security_agent.can_access_dataset(current_user_roles, data.dataset):
            trans.response.set_content_type(data.get_mime())
            if tofile:
                fStat = os.stat(data.file_name)
                trans.response.headers['Content-Length'] = int(fStat.st_size)
                if (toext[0:1] != '.'):
                    toext = ('.' + toext)
                fname = data.name
                fname = ''.join(((((c in FILENAME_VALID_CHARS) and c) or '_') for c in fname))[0:150]
                trans.response.headers['Content-Disposition'] = ('attachment;    filename="GalaxyHistoryItem-%s-[%s]%s"' % (data.hid, fname, toext))
            trans.log_event(('Display    dataset    id:    %s' % str(id)))
            try:
                return open(data.file_name)
            except:
                return 'This    dataset    contains    no    content'
        else:
            return 'You    are    not    allowed    to    access    this    dataset'
    else:
        return ("No    dataset    with    id    '%s'" % str(id))
