Search code examples
javaxpagesxpages-ssjs

open pdf file with sessionAsSigner


I have a database where the user doesn't has access to. Still I can go to the database and "read" the documents with for example

var db:NotesDatabase = sessionAsSigner.getDatabase("","somedir/some.nsf");

In this database there's a pdf file I would like to open or download. I have the filename and the unid . If the user had acces to the database I could do it with

http(s)://[yourserver]/[application.nsf] /xsp/.ibmmodres/domino/OpenAttachment/ [application.nsf]/[UNID|/$File/[AttachmentName]?Open

How can I do it with sessionAsSigner without putting a $PublicAccess=1 field on the form ?

edit: the pdf file is stored as attachment in a richtextfield

second edit

I'm trying to use the XSnippet from Naveen and made some changes

The error message I get is : 'OutStream' not found

The code I tried is :

response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + zipFileName);

var embeddedObj:NotesEmbeddedObject = null;
var bufferInStream:java.io.BufferedInputStream = null;
var outStream:java.io.OutputStream = response.getOutputStream();

embeddedObj = downloadDocument.getAttachment(fileName);
  if (embeddedObj != null) {
    bufferInStream = new     java.io.BufferedInputStream(embeddedObj.getInputStream());
    var bufferLength = bufferInStream.available();
    var data = new byte[bufferLength];
    bufferInStream.read(data, 0, bufferLength); // Read the attachment data

ON THE NEXT LINE IS THE PROBLEM

OutStream.write(data); // Write attachment into pdf

bufferInStream.close();
embeddedObj.recycle();
  }


downloadDocument.recycle();

outStream.flush();
outStream.close();
facesContext.responseComplete();

Solution

  • Create an XAgent (= XPage without rendering) which takes datebase + documentid + filename as URL parameters and delivers the file as response OutputStream.

    The URL would be

    http(s)://[yourserver]/download.nsf/download.xsp?db=[application.nsf]&unid=[UNID]&attname=[AttachmentName]
    

    for an XAgent download.xsp in a database download.nsf.

    The code behind the XAgent runs as sessionAsSigner and is able to read the file even the user itself has no right to access file's database.

    Use Eric's blog (+ Java code) as a starting point. Replace "application/json" with "application/pdf" and stream pdf file instead of json data.

    As an alternative you can adapt this XSnippet code from Thomas Adrian. Use download() together with grabFile() to write your pdf-File to OutputStream.

    Instead of extracting attachment file to path and reading it from there you can stream the attachment right from document to response's OutputStream. Here is an XSnippet from Naveen Maurya as a good example.