Java agent: how to print to agent output contents of a file

Hi!

I have an agent (imported Java) that creates pdf from Notes document using Apache Fop. In case of success I would like to write to agentsOutput contents of my pdf.

I use this code to get agent output:

PrintWriter pw = getAgentOutput();

And this is my method that prints to output:

public void writeFileToPw(PrintWriter pw, String fileName) {

	String s;

	try {

		BufferedReader in = new BufferedReader(new FileReader(fileName));

		while ((s = in.readLine()) != null) {

			pw.write(s);

			pw.println();

		}

		in.close();

		pw.close();

	} catch (Exception e) {

		logger.fatal("Error writing AgentOutput", e);

	}

}

The problem is that this methos outputs the file as html (treats the contents of my pdf as text) not as application/pdf (binary data).

If I write to AgentOutput url of a file (for example if I put it in Domino/html folder to be publicly accessed over http), works fine but the problem is that these documents must not be publicly accessed.

Any suggestions greatly appreciated!

Jelka

Subject: Java agent: how to print to agent output contents of a file

The first thing you need to do if you’re going to be writing anything except HTML to the agentOutput, is to set the content type, as such:

getAgentOutput().println(“Content-Type: application/pdf”)

But, Mr Frunobulax is right, the PrintWriter that getAgentOutput returns is only used for characters, not bytes, and I do believe PDF files have raw byte codes. (From the JavaDocs - “It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.”)

An alternative way of getting this to work would be to write the PDF as an attachment to a document, and then redirect the browser to the $FILE element for that attachment.

Another way of doing this would be to write the file to the /domino/html folder under your servers notes data directory, and point the user to that. You just have to make sure you clean up the old PDF files occasionally.

  • Steve

Subject: Java agent: how to print to agent output contents of a file

I don’t think you can do it.

The PrintWriter is a class for writing to character streams.

You need to write the raw bytes of your pdf which you can’t do with a PrintStream.

Frunobulax