0

as per a suggestion recently received from Stwissel, I am using the following code to convert a file to mime and attach to a notes document during the post generation process of POI4Xpages.

EDITED NEW CODE:

The following code attaches to document, but throws an error: 502 Bad Gateway - The server returned an invalid or incomplete response.

public void saveExcel(Workbook a, Document newDoc) throws NotesException, IOException{

    newDoc.replaceItemValue("Form", "Provider");

    // Create the stream
    Session session = DominoUtils.getCurrentSession();
    Stream stream = session.createStream();
    // Write the workbook to a ByteArrayOutputStream
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    a.write(bos);
    // Convert the output stream to an input stream
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    stream.setContents(is);



MIMEEntity m = newDoc.createMIMEEntity("body");
MIMEHeader header = m.createHeader("content-disposition");
header.setHeaderVal("Mime attachment");
m.setContentFromBytes(stream, "application/vnd.ms-excel", MIMEEntity.ENC_IDENTITY_BINARY); 
m.decodeContent();
newDoc.save(true, true);
}

ORIGINAL CODE:

var stream:NotesStream = session.createStream();

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "Provider");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Subject");
header.setHeaderVal("MIME attachment");
if (stream.open("c:\\notes\\data\\abc.xlsx", "binary")) {
    if (stream.getBytes() != 0) {
        body.setContentFromBytes(stream, "application/vnd.ms-excel",
        NotesMIMEEntity.ENC_IDENTITY_BINARY);
    } else requestScope.status = "File was not found.";
} else requestScope.status = "Could not open file.";
stream.close();
doc.save(true, true);
// Restore conversion
session.setConvertMIME(true);

However, this code is only attaching a file which is already stored on the server's local directory. How can I get this code to take the POI fileOutputStream and attach that?

  • Your example is missing the code you're using for POI. – muenzpraeger Jul 28 '15 at 02:54
  • Hi, the code for poi works just fine. I'm really only interested in the post generation attachment to a notes document. The poi already generates an .xlsx file, all i need is to determine how to write poi to a notes stream? I haven't been able to achieve that. – user4372614 Jul 28 '15 at 03:04
  • The answer to this stackoverflow should help you. http://stackoverflow.com/questions/17425810/write-contents-of-inputstream-to-a-richtextitem-and-attach-to-a-notes-document-i – muenzpraeger Jul 28 '15 at 03:21
  • I definitely CAN attach an inputStream's contents to the document (its just that I can only figure out how to do this with a file on the local system) eg. var is = new java.io.FileInputStream("c:\\notes\\abc.xlsx"); var stream:NotesStream = session.createStream(); stream.setContents(is); What I cant figure out how to do is set the output of POI (via the postGeneration process) to the inputStream. eg. cant make the code below work var file = new java.io.File("file.xlsx"); var is = new java.io.FileInputStream(file); var stream:NotesStream = session.createStream(); stream.setContents(is); – user4372614 Jul 28 '15 at 03:38
  • First: you aren't posting what error message you got. So it's hard to guess. As said before - check the link I've posted. You try to pass an InputStream as argument to .setContent() which isn't valid. You've to create a byte array and pass that as the argument. – muenzpraeger Jul 28 '15 at 04:15
  • There is no error message (It just doesn't do anything). And yes, i can pass java.io.FileInputStream to stream.setContent(). That does work. To clarify, what I am having trouble with is setting the output of poi to an inputStream – user4372614 Jul 28 '15 at 04:27
  • So you mean you have a Workbook object (via WorkbookProcessor.processWorkbook())? As said, still missing the code you're using. – muenzpraeger Jul 28 '15 at 05:30
  • 1
    Is this what you are looking for: http://stackoverflow.com/a/14540635/2065611 ? – Knut Herrmann Jul 28 '15 at 05:32
  • this is the route i am trying to take, but that answer doesn't go into detail about how I cant get a handle on the generated file (in memory) and pass it to the fileInputStream. How do you get a handle on the file in POI's postGeneration() method? – user4372614 Jul 28 '15 at 05:51

1 Answers1

1

It's a mix of what what Knut and I've commented about. The important part is that you'll use the write() method to pass the workbook data to an output stream.

// Create the stream
Stream stream = session.createStream();
// Write the workbook (you haven't clarified) to a ByteArrayOutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
// Convert the output stream to an input stream
InputStream is = new ByteArrayInputStream(bos.toByteArray());
stream.setContents(is);
muenzpraeger
  • 373
  • 1
  • 5
  • I think there is a little bit of a mix up. I'm only writing code for the POI4Xpages postGeneration() event (and that's currently in javascript not java). I'm not using a bean to generate the document, its just a few bookmark tags. That's why I've been saying there is no code. – user4372614 Jul 28 '15 at 06:30
  • 1
    https://guedebyte.wordpress.com/2013/01/25/poi-4-xpages-version-1-1-0-released/ - the end of this blog post shows that the "workbook" variable is available. – muenzpraeger Jul 28 '15 at 06:37
  • Code is along these lines <![CDATA["Amount1"]]> – user4372614 Jul 28 '15 at 06:38
  • Thanks. I'll have a look and get back – user4372614 Jul 28 '15 at 06:39
  • Here's a link how to use Java from SSJS http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Work_with_Java#Tips+for+accessing+Java+via+SSJS – muenzpraeger Jul 28 '15 at 06:41
  • Is it possible to do what I want just in SSJS? getting the file to an input stream? – user4372614 Jul 28 '15 at 06:56
  • SSJS is in fact Java (it's a bad IBM-mutation of "something" for making things "simple" which I personally wouldn't advise to use). If you want the underlying Java object you'll need some Java. – muenzpraeger Jul 28 '15 at 07:18
  • Use a bean. SSJS has NO, ZERO, ZILCH advantage here. You only risk "variable guessing". It isn't that hard. In POI4XPages to write to a stream, not a file. That the stream you were writing to is a FileOutputstream is your choice – stwissel Jul 28 '15 at 07:19
  • Thanks to you both for the help. I'll give the java a shot and get back to you with the results. On a side note, what do you mean that writing to a fileOutputStream is a choice? What else could I write to?? Thanks – user4372614 Jul 28 '15 at 07:23
  • Ok, modified a bit (see edited question), and the code now attaches the file to the document. But, it also throws a 502 error saying the server returned an invalid or incomplete response. Any ideas? – user4372614 Jul 28 '15 at 23:48
  • also, it generates 2 notes documents?? – user4372614 Jul 28 '15 at 23:57