// Get the store
Store store = session.getStore(getServerType());
store.connect(getServer(), getPortAsInt(), getUserName(), getPassword());
// Get folder
Folder folder = store.getFolder(getFolder());
if (deleteMessages) {
folder.open(Folder.READ_WRITE);
} else {
folder.open(Folder.READ_ONLY);
}
// Get directory
Message messages[] = folder.getMessages();
StringBuilder pdata = new StringBuilder();
pdata.append(messages.length);
pdata.append(" messages found\n");
parent.setResponseData(pdata.toString(),null);
parent.setDataType(SampleResult.TEXT);
parent.setContentType("text/plain"); // $NON-NLS-1$
int n = getNumMessages();
if (n == ALL_MESSAGES || n > messages.length) {
n = messages.length;
}
parent.setSampleCount(n); // TODO is this sensible?
busy = true;
for (int i = 0; busy && i < n; i++) {
StringBuilder cdata = new StringBuilder();
SampleResult child = new SampleResult();
child.sampleStart();
Message message = messages[i];
cdata.append("Message "); // $NON-NLS-1$
cdata.append(message.getMessageNumber());
child.setSampleLabel(cdata.toString());
child.setSamplerData(cdata.toString());
cdata.setLength(0);
final String contentType = message.getContentType();
child.setContentType(contentType);// Store the content-type
child.setDataEncoding(RFC_822_DEFAULT_ENCODING); // RFC 822 uses ascii per default
child.setEncodingAndType(contentType);// Parse the content-type
if (isStoreMimeMessage()) {
// Don't save headers - they are already in the raw message
ByteArrayOutputStream bout = new ByteArrayOutputStream();
message.writeTo(bout);
child.setResponseData(bout.toByteArray()); // Save raw message
child.setDataType(SampleResult.TEXT);
} else {
@SuppressWarnings("unchecked") // Javadoc for the API says this is OK
Enumeration<Header> hdrs = message.getAllHeaders();
while(hdrs.hasMoreElements()){
Header hdr = hdrs.nextElement();
String value = hdr.getValue();
try {
value = MimeUtility.decodeText(value);
} catch (UnsupportedEncodingException uce) {
// ignored
}
cdata.append(hdr.getName()).append(": ").append(value).append("\n");
}
child.setResponseHeaders(cdata.toString());
cdata.setLength(0);
appendMessageData(child, message);
}
if (deleteMessages) {
message.setFlag(Flags.Flag.DELETED, true);
}
child.setResponseOK();
if (child.getEndTime()==0){// Avoid double-call if addSubResult was called.
child.sampleEnd();
}
parent.addSubResult(child);
}
// Close connection
folder.close(true);
store.close();
parent.setResponseCodeOK();
parent.setResponseMessageOK();
isOK = true;