0

I need to attach a file generated by Apache POI on an Xpage to a notes document. I have been attempting to implement a solution as suggested by Knut Herrmann:

var temp = java.lang.System.getProperty("java.io.tmpdir");
var file = new java.io.File(temp + "YourFile.docx"); 
var fileOutputStream = new java.io.FileOutputStream(file);
xwpfdocument.write(fileOutputStream);
fileOutputStream.close();

var doc:NotesDocument = currentDocument.getDocument();
var rdoc:NotesDocument = database.createDocument();
rdoc.appendItemValue("Form", "frmRespTempl");
rdoc.appendItemValue("Subject", "Embedded Word Document");
var rtitem:RichTextItem = rdoc.createRichTextItem("Body");
rtitem.embedObject(lotus.domino.local.EmbeddedObject.EMBED_ATTACHMENT,"",file.getAbsolutePath(), null);
rdoc.makeResponse(doc);
rdoc.save();

POI for XPages - save Word document as attachment in rich text field

however, in order to make xwpfdocument.write(fileOutputStream) work, the java policy file needs to be modified which is a security risk.

I had no luck making the java solutions work either. Is there any other way to go about making this code work? What exactly is the risk of modifying the java policy?

Thanks

Community
  • 1
  • 1
  • 1
    You might want to take a different approach and store your document as mime. A mime part can be created using a stream without the need of a temporary file – stwissel Jul 21 '15 at 01:35
  • Thanks. That sounds like the way I would like to do it. Is there any documentation for this? – user4372614 Jul 21 '15 at 02:26
  • hi stwissel, i have used to following code to attach a file using a stream. The example only worked with a file already stored on the server. How can I make this pick up the output of poi........var stream:NotesStream = session.createStream(); session.setConvertMIME(false); var doc:NotesDocument = database.createDocument(); doc.replaceItemValue("Form", "Provider"); – user4372614 Jul 21 '15 at 03:16
  • var body:NotesMIMEEntity = doc.createMIMEEntity(); var header:NotesMIMEHeader = body.createHeader("Subject"); header.setHeaderVal("MIME image from GIF file"); stream.open("c:\\image\pic.gif", "binary"); body.setContentFromBytes(stream, "image/gif", NotesMIMEEntity.ENC_IDENTITY_BINARY); stream.close(); doc.save(true, true); session.setConvertMIME(true); – user4372614 Jul 21 '15 at 03:17

1 Answers1

0

Are you running the code in a browser or in the Notes Client because the code will never work in a browser if you want to send the file to the user side. It will work on the serverside.

If you want to attach a local document in a Notes client I would suggest you start an Notes agent with the code to embed the file instead.

Instead of modifying the java.policy file, I think you could write a class that you implement inside a jar file and place that jar file in the class path on the server and instance it from your XPage code.

Fredrik Norling
  • 3,461
  • 17
  • 21
  • Why won't the code work in a browser? Looks like the code is called from an XPage and tries to attach the Word document created by POI to a document in the current database. – Mark Leusink Jul 20 '15 at 08:39
  • All code runs on server like the SSJS code shown in question. You can work with local files on server with default java policy settings. – Knut Herrmann Jul 20 '15 at 09:41
  • This is to be run on a production server (with access entirely Xpages based). From your previous discussion I wasn't sure whether it was a good idea to modify the permissions in the policy settings. Is this correct? What is the risk? Can permissions be granted to only the functions which need to be called? – user4372614 Jul 20 '15 at 11:02
  • @MarkLeusink The code doesn't work because the default java permissions don't allow the code to write to the file system. I think? Is there another way you would go about implementing this functionality? – user4372614 Jul 20 '15 at 11:12