Document.getItemValue() doesn't work?

I have a java agent in which I’m trying to filter email based on the “Received” header. Here is the code I’m using:

Vector receivedVec = doc.getItemValue(“Received”);

Iterator iter = receivedVec.iterator();

while (iter.hasNext())

{

String val = (String)iter.next();

log.logAction(“Received=” + val);

if (val.toLowerCase().indexOf(“gnats@localhost”) != -1)

{

move = true;

break;

}

}

The problem is getItemValue() is not returning all of the Received headers. When I look at the document properties after the doc has been delivered to my inbox, I can see there are two Received headers, and yet getItemValue() only returns a Vector of size 1, which contains only the first Received header. How can I get all of the Received headers?

Subject: Document.getItemValue() doesn’t work?

It seems there is a bug in Notes. I ran the following agent code on an email with three Received headers:

Vector items = doc.getItems();

Iterator iter = items.iterator();

while (iter.hasNext())

{

Item item = (Item)iter.next();

log.logAction(“item=” + item);

if (item.getName().equals(“Received”))

{

String val = item.getValueString();

log.logAction("item val=" + val);

}

}

It correctly prints out three items, but each item has the same value (the value of the first Received header) rather than the value of each header. How can I have IBM verify this and submit it as a bug? Is there any way I can get the values of all Received headers?

Subject: RE: Document.getItemValue() doesn’t work?

Help file excerpt: “If multiple items have the same name, this method returns the value of the first item.”

There are always multiple duplicate Received fields (NotesItem’s) on an email. That’s the problem you’re seeing.

6.5 has something new:

Vector received = doc.getReceivedItemText();

Try that.

Also, here’s an excerpt from the Domino 6.5 help file on Document.getFirstItem.

“If multiple items in a document have the same name, programmatic access is limited to the first item. The remaining items yield invalid data. A work-around is to get the first item, process it, remove it, again get the first item (which was the second item), and so on until you process all the items with the same name. If you do not save the document, the items are not actually removed. However, the recommendation is that you avoid creating multiple items with the same name.”

Subject: RE: Document.getItemValue() doesn’t work?

Interesting. It seems they do have a bug and fixed it by changing the documentation. Unfortunately, the server I run on is version 6, so I’ll give this workaround a shot. BTW, the version 6 Document.getFirstItem() docs say the following (which is why I thought what I was doing would work):

“A document may contain more than one item of the same name. To access other than the first item, use the Items property.”

Subject: RE: Document.getItemValue() doesn’t work?

Ok, I finally got it working. For anyone that wants to filter based on the Received headers, here is the method I’m using to get them:

private List getReceivedHeaders(Document doc, Log log)

throws NotesException

{

ArrayList savedItems = new ArrayList();



Item item = doc.getFirstItem("Received");

while (item != null)

{

  savedItems.add(item.getValueString());

  

  // to get around a lotus bug and retrieve all the values, we have to

  // remove the item, then retrieve the next item

  item.remove();

  item = doc.getFirstItem("Received");

}



// restore the items

Iterator iter = savedItems.iterator();

while (iter.hasNext())

{

  String savedItem = (String)iter.next();

  doc.appendItemValue("Received", savedItem);

}



return savedItems;

}

Subject: RE: Document.getItemValue() doesn’t work?

Its a bit drastic to remove mail headers isn’t it. R6 has a new method

stringArray = notesDocument.GetReceivedItemText( )

which may do what you want.

Subject: RE: Document.getItemValue() doesn’t work?

Is that R6, or R6.5? I don’t have that method in the R6 I’m using.

Subject: RE: Document.getItemValue() doesn’t work?

It’s not a bug. getItemValue() does exactly what it says it does. As I tried to explain to you, there are other methods you should be using.

Subject: RE: Document.getItemValue() doesn’t work?

I disagree. If there are three Received headers and you get all the items of a document using the getItems() method, you’ll get three items for the Received headers. However, they all hold the value of the first header. If it’s not considered a bug, it’s very bad design at the very least.

Subject: RE: Document.getItemValue() doesn’t work?

“However, they all hold the value of the first header”

No. They don’t. You’re only looking at the first “Received” Item in the Document. To get a handle to the other Items, use our suggestions from this discussion thread.

The only time you’d see multiple Items on a Document, is on mail documents. They have multiple Items, all called “Received”. Lotus knew this was a concern for developers, and gave us new methods in ND6.5.

Subject: RE: Document.getItemValue() doesn’t work?

“You’re only looking at the first “Received” Item in the Document.”

No, my code was using getItems to retrieve all the Items in the document. The R6 documentation says I should be able to retrieve all of the Received headers this way. I’m guessing the Notes developers discovered this doesn’t work and the easiest way to fix it was to add a new method in R6.5. They also added the workaround for this bug in the R6.5 docs.

“To get a handle to the other Items, use our suggestions from this discussion thread.”

I used the workaround for the bug, and it works, but it’s definitely a hack.

“The only time you’d see multiple Items on a Document, is on mail documents. They have multiple Items, all called “Received”. Lotus knew this was a concern for developers, and gave us new methods in ND6.5.”

I’m working with mail documents. Do you honestly believe getting 3 Item objects back all the same rather than the 3 distinct Items that are actually in the document is correct behavior?!?