Rafael Osipov Rafael Osipov - 3 months ago 12
Java Question

OutputStream in Domino web-agent does not write data correctly

On IBM Domino Server (version 8.5.3FP1 on Win32 platform) there are two web-agents, to generate PDF and RTF files by request.

Every agent generates RTF or PDF file in a temporary folder, then opens

OutputStream
instance to write this file to the client (browser, when save-file dialog appears).

All things work ok. Files are generated and saved correctly in temporary folder. But writing these files to OutputStream to let a user save it to the local disk, it does not work properly. Some files were written ok (small files, ~11Kb), but bigger files, ~34K were saved partially (sometimes 276 bytes saved, sometimes 4K bytes saved, etc).

I get OutputStream in my agents as follows:

final OutputStream os = this.getAgentOutputStream();


When file is generated and saved I use:

final FileInputStream fis = new FileInputStream(pdfFilePath);
IOUtils.copy(fis, os); // it is from Apache Commons IOUtils
fis.close();


Does not work.

Then I used this way instead:

final byte[] resultArray = FileUtils.readFileToByteArray(new File(pdfFilePath)); // result array has correct length and correct contents
os.write(resultArray);
os.flush();
os.close();


Does not work.

Then I used this way instead (tricky, but just for experimental purposes):

final byte[] resultArray = FileUtils.readFileToByteArray(new File(pdfFilePath)); // result array has correct length and correct contents
for (byte a:resultArray) {
os.write(a);
}

os.flush();
os.close();


Does. not. work.

Before sending data to output stream I have invoked:

java.io.PrintWriter pw = this.getAgentOutput();
pw.println("Content-type: application/pdf"); // also tried octet-stream, no effect
pw.println("Content-Disposition: attachment; filename=\"file.pdf\"");


And my question is as follows, folks. What is wrong with my approach? What I am doing wrong here? File is created and saved on server correctly. Output stream opened correctly, file read correctly. When I write to output stream there's no exception. Output stream flushed and closed correctly.

What is wrong? I am trying to solve this the whole day, but I did not find a clue.

Any ideas?

Answer

Seems that Domino has bug with agent OutputStream. Stream obtained via agentRef.getAgentOutputStream(); does not work properly and performs partial write.

Instead of using this way I decided to attach files to a NotesDocument instance, save it and provide user with link to attached files in this document.