com.sun.mail.iap.ProtocolException Java Examples

The following examples show how to use com.sun.mail.iap.ProtocolException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultFolder.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
@Override
   public synchronized Folder[] list(final String pattern)
			throws MessagingException {
ListInfo[] li = null;

li = (ListInfo[])doCommand(new ProtocolCommand() {
    @Override
    public Object doCommand(IMAPProtocol p) throws ProtocolException {
	return p.list("", pattern);
    }
});

if (li == null)
    return new Folder[0];

IMAPFolder[] folders = new IMAPFolder[li.length];
for (int i = 0; i < folders.length; i++)
    folders[i] = ((IMAPStore)store).newIMAPFolder(li[i]);
return folders;
   }
 
Example #2
Source File: DefaultFolder.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
@Override
   public synchronized Folder[] listSubscribed(final String pattern)
			throws MessagingException {
ListInfo[] li = null;

li = (ListInfo[])doCommand(new ProtocolCommand() {
    @Override
    public Object doCommand(IMAPProtocol p) throws ProtocolException {
	return p.lsub("", pattern);
    }
});

if (li == null)
    return new Folder[0];

IMAPFolder[] folders = new IMAPFolder[li.length];
for (int i = 0; i < folders.length; i++)
    folders[i] = ((IMAPStore)store).newIMAPFolder(li[i]);
return folders;
   }
 
Example #3
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static RFC822DATA getRFC822Message(final IMAPFolder folder, final long uid) throws MessagingException
{
    return (RFC822DATA) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            Response[] r = p.command("UID FETCH " + uid + " (RFC822)", null);
            logResponse(r);
            Response response = r[r.length - 1];
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message in RFC822 format");
            }

            FetchResponse fetchResponse = (FetchResponse) r[0];
            return fetchResponse.getItem(RFC822DATA.class);
        }
    });
   
}
 
Example #4
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns BODY object containing desired message fragment
 * 
 * @param folder Folder containing the message
 * @param uid Message UID
 * @param from starting byte
 * @param count bytes to read
 * @return BODY containing desired message fragment
 * @throws MessagingException
 */
private static BODY getMessageBodyPart(IMAPFolder folder, final Long uid, final Integer from, final Integer count) throws MessagingException
{
    return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            Response[] r = p.command("UID FETCH " + uid + " (FLAGS BODY.PEEK[]<" + from + "." + count + ">)", null);
            logResponse(r);
            Response response = r[r.length - 1];

            // Grab response
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message part <" + from + "." + count + ">");
            }

            FetchResponse fetchResponse = (FetchResponse) r[0];
            BODY body = (BODY) fetchResponse.getItem(com.sun.mail.imap.protocol.BODY.class);
            return body;
        }
    });

}
 
Example #5
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns a full message body
 * 
 * @param folder Folder containing the message
 * @param uid Message UID
 * @return Returns size of the message
 * @throws MessagingException
 */
private static BODY getMessageBody(IMAPFolder folder, final Long uid) throws MessagingException
{
    return (BODY) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            Response[] r = p.command("UID FETCH " + uid + " (FLAGS BODY.PEEK[])", null);
            logResponse(r);
            Response response = r[r.length - 1];

            // Grab response
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message size");
            }
            FetchResponse fetchResponse = (FetchResponse) r[0];
            BODY body = (BODY) fetchResponse.getItem(BODY.class);
            return body;
        }
    });
}
 
Example #6
Source File: IdleNotificationWorker.java    From camunda-bpm-mail with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() {
  runnning = false;

  // perform a NOOP to interrupt IDLE
  try {
    folder.doCommand(new IMAPFolder.ProtocolCommand() {
      public Object doCommand(IMAPProtocol p) throws ProtocolException {
            p.simpleCommand("NOOP", null);
            return null;
        }
    });
  } catch (MessagingException e) {
    // ignore
  }
}
 
Example #7
Source File: GmailStore.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
protected IMAPProtocol newIMAPProtocol(String host, int port)
			throws IOException, ProtocolException {
return new GmailProtocol(name, host, port, 
				    session.getProperties(),
				    isSSL,
				    logger
				   );
   }
 
Example #8
Source File: SaslAuthenticator.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
public boolean authenticate(String[] mechs, String realm, String authzid,
String u, String p) throws ProtocolException;
 
Example #9
Source File: IMAPNestedMessage.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
@Override
   protected IMAPProtocol getProtocol()
		throws ProtocolException, FolderClosedException {
return msg.getProtocol();
   }
 
Example #10
Source File: ImapMessageTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns the UID of the first message in folder
 * 
 * @param folder Folder containing the message
 * @param msn message sequence number
 * @return UID of the first message
 * @throws MessagingException
 */
private static Long getMessageUid(IMAPFolder folder, final int msn) throws MessagingException
{
    return (Long) folder.doCommand(new IMAPFolder.ProtocolCommand()
    {
        public Object doCommand(IMAPProtocol p) throws ProtocolException
        {
            String command = "FETCH " + msn + " (UID)";
            Response[] r = p.command(command, null);
            logResponse(r);
            Response response = r[r.length - 1];

            // Grab response
            if (!response.isOK())
            {
                throw new ProtocolException("Unable to retrieve message UID");
            }
            
            for(int i = 0 ; i < r.length; i++)
            {
                if(r[i] instanceof FetchResponse)
                {
                    FetchResponse fetchResponse = (FetchResponse) r[0];
                    UID uid = (UID) fetchResponse.getItem(UID.class);
                    logger.debug("SECNUM=" + uid.seqnum + ", UID="+uid.uid);
                    return uid.uid;
                }
            }
            
            /**
              * Uh-oh - this is where we would intermittently fall over with a class cast exception.
              * The following code probes why we don't have a FetchResponse
              */
            StringBuffer sb = new StringBuffer();
            sb.append("command="+command);
            sb.append('\n');
            sb.append("resp length=" + r.length);
            sb.append('\n');
            for(int i = 0 ; i < r.length; i++)
            {
                logger.error(r[i]);
                sb.append("class=" + r[i].getClass().getName());
                IMAPResponse unexpected = (IMAPResponse)r[i];
                sb.append("key=" + unexpected.getKey());
                sb.append("number=" + unexpected.getNumber());
                sb.append("rest=" + unexpected.getRest());
             
                sb.append("r[" + i + "]=" + r[i] + '\n');
            }
            throw new ProtocolException("getMessageUid: "+ sb.toString());
        }
    });
}
 
Example #11
Source File: ImapProtocolTest.java    From greenmail with Apache License 2.0 4 votes vote down vote up
@Test
public void testFetchSpaceBeforeSize() throws MessagingException {
    store.connect("foo@localhost", "pwd");
    try {
        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        // Fetch without partial as reference
        Response[] ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) {
                return protocol.command("UID FETCH 1 (BODY[HEADER])", null);
            }
        });
        FetchResponse fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(3, fetchResponse.getItemCount()); // UID, BODY, FLAGS

        BODY body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        final String content = new String(body.getByteArray().getNewBytes());

        UID uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);

        // partial size only
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
                return protocol.command("UID FETCH 1 (BODY[HEADER]<50>)", null);
            }
        });
        fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID, BODY

        body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        assertEquals(50, body.getByteArray().getCount());

        uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);

        // partial size and zero offset
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
                return protocol.command("UID FETCH 1 (BODY[HEADER]<0.30>)", null);
            }
        });
        fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID , BODY

        body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        assertEquals(30, body.getByteArray().getCount());

        uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);

        // partial size and non zero offset
        ret = (Response[]) folder.doCommand(new IMAPFolder.ProtocolCommand() {
            @Override
            public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
                return protocol.command("UID FETCH 1 (BODY[HEADER]<10.30>)", null);
            }
        });
        fetchResponse = (FetchResponse) ret[0];
        assertFalse(fetchResponse.isBAD());
        assertEquals(2, fetchResponse.getItemCount()); // UID and SIZE

        body = fetchResponse.getItem(BODY.class);
        assertTrue(body.isHeader());
        final ByteArray byteArray = body.getByteArray();
        assertEquals(30, byteArray.getCount());
        assertEquals(content.substring(10, 10 + 30), new String(byteArray.getNewBytes()));

        uid = fetchResponse.getItem(UID.class);
        assertEquals(folder.getUID(folder.getMessage(1)), uid.uid);
    } finally {
        store.close();
    }
}
 
Example #12
Source File: IMAPMockFolder.java    From javamail-mock2 with Apache License 2.0 3 votes vote down vote up
@Override
protected synchronized Object doProtocolCommand(final ProtocolCommand cmd) throws ProtocolException {

    throw new ProtocolException(
            "no protocol for mock class - you should never see this exception. Please file a bugrfeport and include stacktrace");

}