从 URL 下载内容而不提示在 Grails 中进行身份验证

Downloading content from URL without prompting for authentication in Grails

我正在开发一个用 Grails 编写的网络应用程序。我有一个用于显示特定报告的页面,这些报告可以包含附件。附件存储在 documentum 中,目前,当用户单击它时,它只是一个 link 到 documentum 中存储附件的位置,并提示用户输入他的凭据。我的应用程序在配置文件中存储了 documentum 凭据,因此我想使用这些凭据而不是强制用户输入他自己的凭据。我正在使用 RESTful 服务来检索 link,但我正在尝试找到一种使用 link 直接下载到用户计算机的方法。

private def getFileInfo(def id, def subject) {
  // product show view needs the following four lists to display the document information correctly
  def ATRReportInstance = ATRReport.findByTrackingNumber(id)
  def linkList = []
  def nameList = []
  def formatList = []
  def idList = []
  // open up a connection to the documentum server
  def doc = connectToDocumentum()

  if (!doc) return
  def rest = doc.rest
  def response = doc.response
  if (response.status == 200) {
    // retrieve the folder for this product (the name of this folder is the product's ID)
    def rObjectId = rest.get(documentumServer + "/repositories/" + documentumfilestore + "?dql=select r_object_id from dm_folder where any r_folder_path='" + atrreportfolderpath + "/" + id + "'") {
      auth authuser, authpass
    }
    // get the folder's ID from the folder object retrieved above
    def folderObjectID
    rObjectId.json.entries.each {
      entry - >
        folderObjectID = entry.content.properties.r_object_id
    }
    // get all of the documents in the product's MSDS folder using the folder ID retrieved above
    def resp = rest.get(documentumServer + "/repositories/" + documentumfilestore + "?dql=select r_object_id, object_name, a_content_type, subject from cbs_document where any i_folder_id= '" + folderObjectID + "'") {
      auth authuser, authpass
    }
    // cycle through the documents above to populate the four MSDS document information lists
    def x = 0
    resp.json.entries.each {
      entry - >
        if (entry.content.properties.subject == subject) {
          // get the document's content object from the document's ID
          def content = rest.get(documentumServer + "/repositories/" + documentumfilestore + "/objects/" + entry.content.properties.r_object_id + "/contents/content" + "?media-url-policy=local") {
            auth authuser, authpass
          }
          if (entry.content.properties.r_object_id != null && ATRReportInstance.inactiveFiles != null && ATRReportInstance.inactiveFiles.contains(entry.content.properties.r_object_id.toString())) {} else {
            linkList[x] = getLink(content.json.links, "enclosure")
            if (linkList[x].contains("format=msg"))
              linkList[x] = linkList[x].toString().substring(0, linkList[x].toString().indexOf("content-media")) + "content-media.msg"

            formatList[x] = entry.content.properties.a_content_type
            nameList[x] = entry.content.properties.object_name
            idList[x] = entry.content.properties.r_object_id
            x++
          }
        }
    }
    return [linkList: linkList, nameList: nameList, formatList: formatList, idList: idList]
  } else {
    // return null if documentum is unavailable
    flash.message = message(code: 'error.documentum.unavailable')
    return null
  }
}

我正在考虑编写另一个可以接收 URL 并将文档下载给用户的函数,但我不知道如何在 Grails 中检索该文档。

如果您想绕过登录,您可以设置一个 SSO 解决方案(需要为 DCTM 做一些工作)或执行您建议的功能。但是,您在执行此操作时应考虑许可条款。

这是我实施并有效的解决方案。这是一种使用在配置文件中找到的身份验证凭据在 documentum 中下载文件的方法。

def exportAttachment() {
  //uses parameters from gsp file
  def url = params.url
  def name = params.name
  def format = params.format
  def extension

  //find proper extension
  for (s in documentumExtMap) {
    if (s.value.equals(format)) {
      extension = s.key
    }
  }

  def connection = new URL(url).openConnection()
  def remoteAuth = "Basic " + "${authuser}:${authpass}".bytes.encodeBase64()
  connection.setRequestProperty("Authorization", remoteAuth)

  def dataStream = connection.inputStream

  response.setContentType("application/octet-stream")
  response.setHeader('Content-disposition', 'Attachment; filename=' + name + '.' + extension)
  response.outputStream << dataStream
  response.outputStream.flush()
}

该方法有三个参数:url、名称、格式。

Url 是文件在 documentum 中的位置。

Name为下载客户端名称

格式是正在下载的文件类型。就我而言,我必须使用它来获得文件所需的正确扩展名。