@Override public void setFlags(List<? extends Message> messages, final Set<Flag> flags, boolean value) throws MessagingException { if (!value || !flags.contains(Flag.DELETED)) { /* * The only flagging we support is setting the Deleted flag. */ return; } List<String> uids = new ArrayList<String>(); try { for (Message message : messages) { uids.add(message.getUid()); } indexUids(uids); } catch (IOException ioe) { throw new MessagingException("Could not get message number for uid " + uids, ioe); } for (Message message : messages) { Integer msgNum = mUidToMsgNumMap.get(message.getUid()); if (msgNum == null) { MessagingException me = new MessagingException( "Could not delete message " + message.getUid() + " because no msgNum found; permanent error"); me.setPermanentFailure(true); throw me; } executeSimpleCommand(String.format(DELE_COMMAND + " %s", msgNum)); } }
private void sendMessageTo(ArrayList<String> addresses, Message message) throws MessagingException { boolean possibleSend = false; close(); open(); message.setEncoding(m8bitEncodingAllowed ? "8bit" : null); // If the message has attachments and our server has told us about a limit on // the size of messages, count the message's size before sending it if (mLargestAcceptableMessage > 0 && ((LocalMessage) message).hasAttachments()) { if (message.calculateSize() > mLargestAcceptableMessage) { MessagingException me = new MessagingException("Message too large for server"); me.setPermanentFailure(possibleSend); throw me; } } Address[] from = message.getFrom(); try { // TODO: Add BODY=8BITMIME parameter if appropriate? executeSimpleCommand("MAIL FROM:" + "<" + from[0].getAddress() + ">"); for (String address : addresses) { executeSimpleCommand("RCPT TO:" + "<" + address + ">"); } executeSimpleCommand("DATA"); EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream( new SmtpDataStuffing( new LineWrapOutputStream(new BufferedOutputStream(mOut, 1024), 1000))); message.writeTo(msgOut); // We use BufferedOutputStream. So make sure to call flush() ! msgOut.flush(); possibleSend = true; // After the "\r\n." is attempted, we may have sent the message executeSimpleCommand("\r\n."); } catch (Exception e) { MessagingException me = new MessagingException("Unable to send message", e); // "5xx text" -responses are permanent failures String msg = e.getMessage(); if (msg != null && msg.startsWith("5")) { Log.w(K9.LOG_TAG, "handling 5xx SMTP error code as a permanent failure"); possibleSend = false; } me.setPermanentFailure(possibleSend); throw me; } finally { close(); } }
private void saslAuthPlain(String username, String password) throws MessagingException, AuthenticationFailedException, IOException { byte[] data = ("\000" + username + "\000" + password).getBytes(); data = new Base64().encode(data); try { executeSimpleCommand("AUTH PLAIN " + new String(data), true); } catch (MessagingException me) { if (me.getMessage().length() > 1 && me.getMessage().charAt(1) == '3') { throw new AuthenticationFailedException("AUTH PLAIN failed (" + me.getMessage() + ")"); } throw me; } }
private void saslAuthLogin(String username, String password) throws MessagingException, AuthenticationFailedException, IOException { try { executeSimpleCommand("AUTH LOGIN"); executeSimpleCommand(new String(Base64.encodeBase64(username.getBytes())), true); executeSimpleCommand(new String(Base64.encodeBase64(password.getBytes())), true); } catch (MessagingException me) { if (me.getMessage().length() > 1 && me.getMessage().charAt(1) == '3') { throw new AuthenticationFailedException("AUTH LOGIN failed (" + me.getMessage() + ")"); } throw me; } }
/** Creates topics and queues and starts listeners */ private void createDestinations() throws MessagingException { try { // Create Destinations based on properties Enumeration<?> propertyNames = m_connectionProperties.keys(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); if (propertyName.startsWith("topic.")) { String destinationName = m_connectionProperties.getProperty(propertyName); m_jmsManager.createDestination(destinationName, DestinationType.Topic); } else if (propertyName.startsWith("queue.")) { String destinationName = m_connectionProperties.getProperty(propertyName); m_jmsManager.createDestination(destinationName, DestinationType.Queue); } } // Get destination list List<Destination> destinations = m_jmsManager.getDestinations(); // If there are no Destinations, throw an exception if (destinations.size() == 0) { throw new MessagingException( "No destinations available for " + "subscription, make sure that there is at least one topic " + "or queue specified in the connection properties."); } // Subscribe for (Destination destination : destinations) { if (m_durable && destination instanceof Topic) { m_jmsManager.listenDurable((Topic) destination, m_messageSelector, this, null); } else { m_jmsManager.listen(destination, m_messageSelector, this); } } } catch (MessagingException me) { logger.error( "MessagingException encountered attempting to start " + "Messaging Client: " + m_clientId + ". Exception message: " + me.getMessage(), me); throw me; } }
/** * Stops the MessagingClient, shuts down connections. If the unsubscribe parameter is set to true, * all durable subscriptions will be removed. * * @param unsubscribe */ public void stop(boolean unsubscribe) throws MessagingException { try { if (unsubscribe) { m_jmsManager.unsubscribeAllDurable(); } m_jmsManager.close(); m_jmsManager = null; m_connected = false; } catch (MessagingException me) { logger.error( "Messaging Exception encountered attempting to stop " + "Messaging Client: " + m_clientId + ". Exception message: " + me.getMessage(), me); throw me; } }
private void connect() throws MessagingException { int retries = 0; while (m_jmsManager == null && retries < MAX_RETRIES) { try { m_jmsManager = new JMSManager(m_connectionProperties, m_clientId); } catch (MessagingException me) { Throwable rootCause = me.getCause(); while (rootCause.getCause() != null) { rootCause = rootCause.getCause(); } if (rootCause instanceof java.net.ConnectException) { try { sleep(RETRY_INTERVAL); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } retries++; } else { throw me; } } } if (m_jmsManager == null) { String errorMessage = "Unable to start JMS Messaging Client, " + MAX_RETRIES + " attempts were made, each attempt resulted in a " + "java.net.ConnectException. The messaging broker at " + m_connectionProperties.getProperty(Context.PROVIDER_URL) + " is not available"; throw new RuntimeException(errorMessage); } }
public void exceptionThrown(Exception e) { Throwable t = getExceptionType(e, RoutingException.class); if (t != null) { RoutingException re = (RoutingException) t; handleRoutingException(re.getUmoMessage(), re.getEndpoint(), e); return; } t = getExceptionType(e, MessagingException.class); if (t != null) { MessagingException me = (MessagingException) t; handleMessagingException(me.getUmoMessage(), e); return; } t = getExceptionType(e, LifecycleException.class); if (t != null) { LifecycleException le = (LifecycleException) t; handleLifecycleException(le.getComponent(), e); return; } handleStandardException(e); }