Changeset 284

Show
Ignore:
Timestamp:
09/04/08 09:06:14 PM (4 months ago)
Author:
octorian
Message:

Merge of fixes

Location:
trunk/LogicMail/src/org/logicprobe/LogicMail
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/smtp/SmtpClient.java

    r237 r284  
    88 * 
    99 * 1. Redistributions of source code must retain the above copyright 
    10  *    notice, this list of conditions and the following disclaimer.  
     10 *    notice, this list of conditions and the following disclaimer. 
    1111 * 2. Redistributions in binary form must reproduce the above copyright 
    1212 *    notice, this list of conditions and the following disclaimer in the 
    13  *    documentation and/or other materials provided with the distribution.  
     13 *    documentation and/or other materials provided with the distribution. 
    1414 * 3. Neither the name of the project nor the names of its 
    1515 *    contributors may be used to endorse or promote products derived 
     
    2929 * OF THE POSSIBILITY OF SUCH DAMAGE. 
    3030 */ 
    31  
    3231package org.logicprobe.LogicMail.mail.smtp; 
    3332 
    34 import java.io.IOException; 
    35 import java.util.Calendar; 
    3633import org.logicprobe.LogicMail.AppInfo; 
    3734import org.logicprobe.LogicMail.conf.ConnectionConfig; 
     
    4946import org.logicprobe.LogicMail.util.StringParser; 
    5047 
     48import java.io.IOException; 
     49 
     50import java.util.Calendar; 
     51 
     52 
    5153/** 
    5254 * Implements an SMTP client 
    5355 */ 
    5456public class SmtpClient implements OutgoingMailClient { 
     57    private static String strCRLF = "\r\n"; 
    5558    private OutgoingConfig outgoingConfig; 
    5659    private Connection connection; 
     
    6164    private String password; 
    6265    private boolean configChanged; 
    63     private static String strCRLF = "\r\n"; 
    64      
     66    private MailSettingsListener mailSettingsListener = new MailSettingsListener() { 
     67            public void mailSettingsSaved(MailSettingsEvent e) { 
     68                mailSettings_MailSettingsSaved(e); 
     69            } 
     70        }; 
     71 
    6572    /** Creates a new instance of SmtpClient */ 
    6673    public SmtpClient(GlobalConfig globalConfig, OutgoingConfig outgoingConfig) { 
    6774        this.outgoingConfig = outgoingConfig; 
    68         connection = new Connection( 
    69             outgoingConfig.getServerName(), 
    70             outgoingConfig.getServerPort(), 
    71             outgoingConfig.getServerSSL(), 
    72             outgoingConfig.getDeviceSide()); 
     75        connection = new Connection(outgoingConfig.getServerName(), 
     76                outgoingConfig.getServerPort(), outgoingConfig.getServerSSL(), 
     77                outgoingConfig.getDeviceSide()); 
    7378        smtpProtocol = new SmtpProtocol(connection); 
    7479 
    75         if(outgoingConfig.getUseAuth() > 0) { 
     80        if (outgoingConfig.getUseAuth() > 0) { 
    7681            username = outgoingConfig.getServerUser(); 
    7782            password = outgoingConfig.getServerPass(); 
    78         } 
    79         else { 
     83        } else { 
    8084            username = null; 
    8185            password = null; 
    8286        } 
     87 
    8388        openStarted = false; 
    8489        configChanged = false; 
    8590        MailSettings.getInstance().addMailSettingsListener(mailSettingsListener); 
    8691    } 
    87     private MailSettingsListener mailSettingsListener = new MailSettingsListener() { 
    88                 public void mailSettingsSaved(MailSettingsEvent e) { 
    89                         mailSettings_MailSettingsSaved(e); 
    90                 } 
    91     }; 
    92      
     92 
    9393    private void mailSettings_MailSettingsSaved(MailSettingsEvent e) { 
    94                 if(MailSettings.getInstance().containsOutgoingConfig(outgoingConfig)) { 
    95                         // Refresh authentication information from the configuration 
    96                 username = outgoingConfig.getServerUser(); 
    97                 password = outgoingConfig.getServerPass(); 
    98                  
    99                 if(!isConnected()) { 
    100                         // Rebuild the connection to include new settings 
    101                     connection = new Connection( 
    102                                 outgoingConfig.getServerName(), 
    103                                 outgoingConfig.getServerPort(), 
    104                                 outgoingConfig.getServerSSL(), 
    105                                 outgoingConfig.getDeviceSide()); 
    106                     smtpProtocol = new SmtpProtocol(connection); 
    107                 } 
    108                 else { 
    109                         // Set a flag to make sure we rebuild the Connection object 
    110                         // the next time we close the connection. 
    111                         configChanged = true; 
    112                 } 
    113                 } 
    114                 else { 
    115                         // We have been deleted, so unregister to make sure we 
    116                         // no longer affect the system and can be garbage collected. 
    117                         MailSettings.getInstance().removeMailSettingsListener(mailSettingsListener); 
    118                 } 
     94        if (MailSettings.getInstance().containsOutgoingConfig(outgoingConfig)) { 
     95            // Refresh authentication information from the configuration 
     96            username = outgoingConfig.getServerUser(); 
     97            password = outgoingConfig.getServerPass(); 
     98 
     99            if (!isConnected()) { 
     100                // Rebuild the connection to include new settings 
     101                connection = new Connection(outgoingConfig.getServerName(), 
     102                        outgoingConfig.getServerPort(), 
     103                        outgoingConfig.getServerSSL(), 
     104                        outgoingConfig.getDeviceSide()); 
     105                smtpProtocol = new SmtpProtocol(connection); 
     106            } else { 
     107                // Set a flag to make sure we rebuild the Connection object 
     108                // the next time we close the connection. 
     109                configChanged = true; 
     110            } 
     111        } else { 
     112            // We have been deleted, so unregister to make sure we 
     113            // no longer affect the system and can be garbage collected. 
     114            MailSettings.getInstance().removeMailSettingsListener(mailSettingsListener); 
     115        } 
    119116    } 
    120117 
    121118    public boolean open() throws IOException, MailException { 
    122         if(!openStarted) { 
     119        if (!openStarted) { 
    123120            connection.open(); 
    124121 
    125122            // Eat the initial server response 
    126123            connection.receive(); 
     124 
    127125            String hostname = System.getProperty("microedition.hostname"); 
    128             if(hostname == null) { 
    129                 hostname = "localhost"; 
     126 
     127            if (hostname == null) { 
     128                hostname = connection.getLocalAddress(); 
    130129            } 
    131130 
     
    133132            openStarted = true; 
    134133        } 
    135         if(outgoingConfig.getUseAuth() > 0) { 
    136             boolean result = smtpProtocol.executeAuth(outgoingConfig.getUseAuth(), username, password); 
    137             if(!result) { 
     134 
     135        if (outgoingConfig.getUseAuth() > 0) { 
     136            boolean result = smtpProtocol.executeAuth(outgoingConfig.getUseAuth(), 
     137                    username, password); 
     138 
     139            if (!result) { 
    138140                return false; 
    139141            } 
    140142        } 
     143 
    141144        isFresh = true; 
    142145        openStarted = false; 
     146 
    143147        return true; 
    144148    } 
     
    146150    public void close() throws IOException, MailException { 
    147151        try { 
    148            smtpProtocol.executeQuit(); 
    149         } catch (Exception exp) { } 
     152            smtpProtocol.executeQuit(); 
     153        } catch (Exception exp) { 
     154        } 
     155 
    150156        connection.close(); 
    151          
    152         if(configChanged) { 
    153                 // Rebuild the connection to include new settings 
    154             connection = new Connection( 
    155                         outgoingConfig.getServerName(), 
    156                         outgoingConfig.getServerPort(), 
    157                         outgoingConfig.getServerSSL(), 
    158                         outgoingConfig.getDeviceSide()); 
     157 
     158        if (configChanged) { 
     159            // Rebuild the connection to include new settings 
     160            connection = new Connection(outgoingConfig.getServerName(), 
     161                    outgoingConfig.getServerPort(), 
     162                    outgoingConfig.getServerSSL(), 
     163                    outgoingConfig.getDeviceSide()); 
    159164            smtpProtocol = new SmtpProtocol(connection); 
    160165            configChanged = false; 
     
    163168 
    164169    public ConnectionConfig getConnectionConfig() { 
    165                 return outgoingConfig; 
    166         } 
     170        return outgoingConfig; 
     171    } 
    167172 
    168173    public boolean isConnected() { 
     
    186191    } 
    187192 
    188     public String sendMessage(Message message) throws IOException, MailException { 
    189         if(!isFresh) { 
     193    public String sendMessage(Message message) 
     194        throws IOException, MailException { 
     195        if (!isFresh) { 
    190196            smtpProtocol.executeReset(); 
    191197        } 
     198 
    192199        isFresh = false; 
    193          
     200 
    194201        // serialize the message 
    195202        MessageMimeConverter messageMime = new MessageMimeConverter(); 
    196203        message.getBody().accept(messageMime); 
     204 
    197205        String mimeStr = messageMime.toMimeString(); 
    198          
     206 
    199207        MessageEnvelope env = message.getEnvelope(); 
    200208        StringBuffer buffer = new StringBuffer(); 
     
    203211        buffer.append("From: " + makeCsvString(env.from) + strCRLF); 
    204212        buffer.append("To: " + makeCsvString(env.to) + strCRLF); 
    205         if(env.cc != null && env.cc.length > 0) { 
     213 
     214        if ((env.cc != null) && (env.cc.length > 0)) { 
    206215            buffer.append("Cc: " + makeCsvString(env.cc) + strCRLF); 
    207216        } 
    208         if(env.replyTo != null && env.replyTo.length > 0) { 
     217 
     218        if ((env.replyTo != null) && (env.replyTo.length > 0)) { 
    209219            buffer.append("Reply-To: " + makeCsvString(env.replyTo) + strCRLF); 
    210220        } 
    211         buffer.append("Date: " + StringParser.createDateString(Calendar.getInstance().getTime()) + strCRLF); 
    212         buffer.append("User-Agent: "+AppInfo.getName()+"/"+AppInfo.getVersion() + strCRLF); 
     221 
     222        buffer.append("Date: " + 
     223            StringParser.createDateString(Calendar.getInstance().getTime()) + 
     224            strCRLF); 
     225        buffer.append("User-Agent: " + AppInfo.getName() + "/" + 
     226            AppInfo.getVersion() + strCRLF); 
    213227        buffer.append("Subject: " + env.subject + strCRLF); 
    214          
    215         if(env.inReplyTo != null) { 
     228 
     229        if (env.inReplyTo != null) { 
    216230            buffer.append("In-Reply-To: " + env.inReplyTo + strCRLF); 
    217231        } 
    218          
     232 
    219233        // Add the body 
    220234        buffer.append(mimeStr); 
    221235 
    222236        // Send the message 
    223         if(!smtpProtocol.executeMail(stripEmail(env.from[0]))) { 
     237        if (!smtpProtocol.executeMail(stripEmail(env.from[0]))) { 
    224238            throw new MailException("Error with sender"); 
    225239        } 
    226         for(int i=0; i<env.to.length; i++) { 
    227             if(!smtpProtocol.executeRecipient(stripEmail(env.to[i]))) { 
     240 
     241        for (int i = 0; i < env.to.length; i++) { 
     242            if (!smtpProtocol.executeRecipient(stripEmail(env.to[i]))) { 
    228243                throw new MailException("Error with recipient"); 
    229244            } 
    230245        } 
    231         if(env.cc != null) { 
    232             for(int i=0; i<env.cc.length; i++) { 
    233                 if(!smtpProtocol.executeRecipient(stripEmail(env.cc[i]))) { 
     246 
     247        if (env.cc != null) { 
     248            for (int i = 0; i < env.cc.length; i++) { 
     249                if (!smtpProtocol.executeRecipient(stripEmail(env.cc[i]))) { 
    234250                    throw new MailException("Error with recipient"); 
    235251                } 
    236252            } 
    237253        } 
    238         if(env.bcc != null) { 
    239             for(int i=0; i<env.bcc.length; i++) { 
    240                 if(!smtpProtocol.executeRecipient(stripEmail(env.bcc[i]))) { 
     254 
     255        if (env.bcc != null) { 
     256            for (int i = 0; i < env.bcc.length; i++) { 
     257                if (!smtpProtocol.executeRecipient(stripEmail(env.bcc[i]))) { 
    241258                    throw new MailException("Error with recipient"); 
    242259                } 
    243260            } 
    244261        } 
    245          
     262 
    246263        String rawMessage = buffer.toString(); 
    247         if(!smtpProtocol.executeData(rawMessage)) { 
     264 
     265        if (!smtpProtocol.executeData(rawMessage)) { 
    248266            throw new MailException("Error sending message"); 
    249267        } 
    250          
     268 
    251269        return rawMessage; 
    252270    } 
    253      
     271 
    254272    private static String makeCsvString(String[] input) { 
    255         if(input == null || input.length == 0) 
     273        if ((input == null) || (input.length == 0)) { 
    256274            return ""; 
    257         else if(input.length == 1) 
     275        } else if (input.length == 1) { 
    258276            return input[0]; 
    259         else { 
     277        } else { 
    260278            StringBuffer buffer = new StringBuffer(); 
    261             for(int i=0; i<input.length-1; i++) { 
     279 
     280            for (int i = 0; i < (input.length - 1); i++) { 
    262281                buffer.append(input[i]); 
    263282                buffer.append(", "); 
    264283            } 
    265             buffer.append(input[input.length-1]); 
     284 
     285            buffer.append(input[input.length - 1]); 
     286 
    266287            return buffer.toString(); 
    267288        } 
    268289    } 
    269      
     290 
    270291    private static String stripEmail(String input) { 
    271292        int p = input.indexOf('<'); 
    272293        int q = input.indexOf('>'); 
    273         if(p == -1 || q == -1 || q <= p) 
     294 
     295        if ((p == -1) || (q == -1) || (q <= p)) { 
    274296            return input; 
    275         else 
    276             return input.substring(p+1, q); 
     297        } else { 
     298            return input.substring(p + 1, q); 
     299        } 
    277300    } 
    278301} 
  • trunk/LogicMail/src/org/logicprobe/LogicMail/util/StringParser.java

    r279 r284  
    5454 */ 
    5555public class StringParser { 
     56    private static final long ONE_SECOND = 1000; 
     57    private static final long ONE_MINUTE = ONE_SECOND * 60; 
     58    private static final long ONE_HOUR = ONE_MINUTE * 60; 
     59 
    5660    private StringParser() { 
    5761    } 
     
    432436        buf.append(' '); 
    433437 
    434         int tzOffset = (cal.getTimeZone().getRawOffset()) / 36000; 
     438        int tzOffset = cal.getTimeZone().getOffset(1, cal.get(Calendar.YEAR), 
     439                cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 
     440                cal.get(Calendar.DAY_OF_WEEK), 
     441                (int) (((long) cal.get(Calendar.HOUR_OF_DAY) * ONE_HOUR) + 
     442                (cal.get(Calendar.MINUTE) * ONE_MINUTE) + 
     443                (cal.get(Calendar.SECOND) * ONE_SECOND))) / 36000; 
    435444 
    436445        if (tzOffset < 0) {