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

时间:2017-12-01 20:46:36

标签: grails restful-authentication documentum

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

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中检索该文档。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是我实施的解决方案。这是一种使用配置文件中的身份验证凭据在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()
}
&#13;
&#13;
&#13;

该方法有三个参数:url,name,format。

Url是文档中文件的位置。

名称是下载客户端的名称

Format是要下载的文件类型。就我而言,我必须使用它来获得文件所需的适当扩展。