{
try
{
if (!entryContext.getMessageEntry().isMimeContent())
{
throw new AssertionNotApplicableException();
}
else
{
MimeParts mimeParts = entryContext.getMessageEntry().getMimeParts();
// If the message does not contain non-root MIME parts
// the assertion is not applicable
if (mimeParts.count()< 2)
throw new AssertionNotApplicableException();
// Getting an operation matched for a message
BindingOperation bindingOperation = validator.getOperationMatch(
entryContext.getEntry().getEntryType(),
entryContext.getMessageEntryDocument());
// If no one operation matches, the assertion is not applicable
if (bindingOperation == null)
throw new AssertionNotApplicableException();
// Getting the corresponding extensibility elements and message
List extElems;
Message message;
if (MessageEntry.TYPE_REQUEST
.equals(entryContext.getEntry().getEntryType()))
{
extElems = bindingOperation.getBindingInput() == null ? null
: bindingOperation.getBindingInput().getExtensibilityElements();
message = bindingOperation.getOperation().getInput() == null ? null
: bindingOperation.getOperation().getInput().getMessage();
}
else
{
extElems = bindingOperation.getBindingOutput() == null ? null
: bindingOperation.getBindingOutput().getExtensibilityElements();
message = bindingOperation.getOperation().getOutput() == null ? null
: bindingOperation.getOperation().getOutput().getMessage();
}
// A variable that indicates a binding contains at least one
// mime:content element that refers to global element declaration
boolean mimeContentFound = false;
// Going through the message MIME parts
Iterator iMimeParts = mimeParts.getParts().iterator();
int i = 0;
while (iMimeParts.hasNext())
{
i = i + 1;
// Getting a MIME part
MimePart mimePart = (MimePart)iMimeParts.next();
// Getting a part name form the Content-ID header
String partName = MIMEUtils.getMimeHeaderAttribute(
mimePart.getHeaders(), MIMEConstants.HEADER_CONTENT_ID);
try
{
int idx = partName.indexOf("=");
if((idx != -1) && partName.startsWith("<"))
partName = encodePartName(partName.substring(1, idx));
}
catch (Exception e)
{
// Could not extract a part name from the header,
// proceed with the next MIME part
continue;
}
// If the part is bound by a mime:content element
if (boundToMimeContent(extElems, partName) && message != null)
{
// Getting the corresponding part
Part part = (Part) message.getParts().get(partName);
QName refName;
// If the part refers to global element declaration
if (part != null && (refName = part.getElementName()) != null)
{
mimeContentFound = true;
// Trying to parse part content
Document doc = null;
try
{
doc = XMLUtils.parseXML(mimePart.getContent());
}
catch (Exception e) {}
// If the message is not an XML infoset, the assertion failed
if (doc == null)
{
throw new AssertionFailException("The bound message part of the "
+ "MIME part number " + (i + 1) + " is invalid XML infoset.");
}
// Creating a QName object of the root element
QName rootName = new QName(
doc.getDocumentElement().getNamespaceURI(),
doc.getDocumentElement().getLocalName());
// If the names of the root element and the referenced element
// are not equal, the assertion failed
if (!refName.equals(rootName))
{
throw new AssertionFailException("The root element name is "
+ rootName + ", the name of the referenced element is "
+ refName + ".");
}
}
}
}
// If no mime:contentS found, the assertion is not applicable
if (!mimeContentFound)
throw new AssertionNotApplicableException();
}
}
catch (AssertionNotApplicableException anae)
{
result = AssertionResult.RESULT_NOT_APPLICABLE;