private Message[] retrieveMessages() throws MessagingException {
Message[] messages;
final SortTerm[] sortTerm = getEndpoint().getSortTerm();
final SearchTerm searchTerm = computeSearchTerm();
if (sortTerm != null && serverCanSort) {
final IMAPFolder imapFolder = (IMAPFolder) folder;
if (searchTerm != null) {
// Sort and search using server capability
messages = imapFolder.getSortedMessages(sortTerm, searchTerm);
} else {
// Only sort using server capability
messages = imapFolder.getSortedMessages(sortTerm);
}
} else {
if (searchTerm != null) {
// Only search
messages = folder.search(searchTerm);