/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.activation.*;
/**
* Test program for IDLE support in IMAP provider.
*
* Run several threads that access the folder's connection
* while another thread runs the IDLE command. A timer
* thread checks that each thread is making progress, to
* detect deadlock.
*
* XXX - Should have another thread add messages to the folder
* to test the new message notification works properly.
*/
public class testidle {
public static boolean showStructure = false;
public static boolean saveAttachments = false;
public static int totalTime;
"Usage: testidle <host> <user> <password> <mbox> <time>");
}
try {
// Get a Session object
// session.setDebug(true);
// Get a Store object
public void notification(StoreEvent e) {
e.getMessageType() + ", message " +
e.getMessage());
}
});
// Connect
// Open a Folder
}
// Add messageCountListener to listen for new messages
// Just dump out the new messages
try {
} catch (IOException ioex) {
} catch (MessagingException mex) {
}
}
}
});
new Thread("timer") {
public void run() {
timer();
}
}.start();
new Thread("message reader") {
public void run() {
readMessages();
}
}.start();
new Thread("folder reader") {
public void run() {
readFolder();
}
}.start();
new Thread("store idle") {
public void run() {
storeIdle();
}
}.start();
/*
* make sure two threads running idle works properly
new Thread("idle") {
public void run() {
try {
doIdle();
} catch (MessagingException mex) { }
}
}.start();
*/
doIdle();
}
}
/**
* Run the idle command until told to stop.
*/
boolean supportsIdle = false;
try {
if (folder instanceof IMAPFolder) {
f.idle();
supportsIdle = true;
}
} catch (FolderClosedException fex) {
throw fex;
} catch (MessagingException mex) {
supportsIdle = false;
}
f.idle();
/*
System.out.println("IDLE done in " +
Thread.currentThread().getName());
*/
} else {
try {
} catch (InterruptedException ex) { }
// This is to force the IMAP server to send us
// EXISTS notifications.
}
}
}
/**
* Monitor progress of threads and tell them to stop
* when their time is up.
*/
public static void timer() {
try {
} catch (InterruptedException ex) { }
}
}
try {
} catch (MessagingException mex) { }
}
/**
* Read all the messages in the folder and access all their content.
*/
public static void readMessages() {
try {
for (int i = 1; i <= cnt; i++) {
//System.out.println("dump " + i);
try {
} catch (InterruptedException ex) { }
}
}
}
}
/**
* Perform folder commands to compete with the message
* access commands.
*/
public static void readFolder() {
try {
try {
} catch (InterruptedException ex) { }
}
} catch (MessagingException mex) {
}
}
/**
* Run the idle command until told to stop.
*/
public static void storeIdle() {
boolean supportsIdle = false;
try {
s.idle();
supportsIdle = true;
}
} catch (MessagingException mex) {
supportsIdle = false;
}
try {
s.idle();
/*
*/
/*
*/
} else {
try {
} catch (InterruptedException ex) { }
}
}
} catch (MessagingException mex) {
}
}
/**
* Dump contents of message part.
* (Copied from msgshow.java)
*/
if (p instanceof Message)
dumpEnvelope((Message)p);
/** Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream))
is = new BufferedInputStream(is);
int c;
while ((c = is.read()) != -1)
System.out.write(c);
**/
try {
} catch (ParseException pex) {
}
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
if (p.isMimeType("text/plain")) {
pr("This is plain text");
pr("---------------------------");
if (!showStructure && !saveAttachments)
} else if (p.isMimeType("multipart/*")) {
pr("This is a Multipart");
pr("---------------------------");
level++;
for (int i = 0; i < count; i++)
level--;
} else if (p.isMimeType("message/rfc822")) {
pr("This is a Nested Message");
pr("---------------------------");
level++;
level--;
} else {
if (!showStructure && !saveAttachments) {
/*
* If we actually want to see the data, and it's not a
* MIME type we know, fetch it and check its Java type.
*/
Object o = p.getContent();
if (o instanceof String) {
pr("This is a string");
pr("---------------------------");
} else if (o instanceof InputStream) {
pr("This is just an input stream");
pr("---------------------------");
int c;
} else {
pr("This is an unknown type");
pr("---------------------------");
}
} else {
// just a separator
pr("---------------------------");
}
}
/*
* If we're saving attachments, write out anything that
* looks like an attachment into an appropriately named
* file. Don't overwrite existing files to prevent
* mistakes.
*/
// many mailers don't include a Content-Disposition
try {
if (f.exists())
// XXX - could try a series of names
throw new IOException("file exists");
((MimeBodyPart)p).saveFile(f);
} catch (IOException ex) {
}
pr("---------------------------");
}
}
}
pr("This is the message envelope");
pr("---------------------------");
Address[] a;
// FROM
for (int j = 0; j < a.length; j++)
}
// TO
for (int j = 0; j < a.length; j++) {
}
}
}
// SUBJECT
// DATE
Date d = m.getSentDate();
pr("SendDate: " +
// FLAGS
boolean first = true;
String s;
s = "\\Answered";
s = "\\Deleted";
s = "\\Draft";
s = "\\Flagged";
s = "\\Recent";
s = "\\Seen";
else
continue; // skip it
if (first)
first = false;
else
}
if (first)
first = false;
else
}
// X-MAILER
else
pr("X-Mailer NOT available");
}
/**
* Print a, possibly indented, string.
*/
/*
if (showStructure)
System.out.print(indentStr.substring(0, level * 2));
System.out.println(s);
*/
}
}