Ignore:
Timestamp:
08/01/09 11:09:20 (3 years ago)
Author:
octorian
Message:

Method and wrapper to hopefully support enabling STARTTLS mode for open stream connections. (Protocol-level support still needed)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/util/Connection.java

    r471 r477  
    6060package org.logicprobe.LogicMail.util; 
    6161 
     62import net.rim.device.api.crypto.tls.tls10.TLS10Connection; 
    6263import net.rim.device.api.system.EventLogger; 
    6364import net.rim.device.api.ui.UiApplication; 
    6465import net.rim.device.api.ui.component.Dialog; 
    6566import net.rim.device.api.util.DataBuffer; 
     67import net.rim.device.cldc.io.ssl.TLSException; 
    6668 
    6769import org.logicprobe.LogicMail.AppInfo; 
     
    6971import org.logicprobe.LogicMail.conf.MailSettings; 
    7072 
     73import java.io.DataInputStream; 
     74import java.io.DataOutputStream; 
    7175import java.io.IOException; 
    7276import java.io.InputStream; 
     
    614618        return result; 
    615619    } 
     620 
     621        /** 
     622         * Switches the underlying connection to SSL mode, as commonly done after 
     623         * sending a <tt>STARTTLS</tt> command to the server. 
     624         *  
     625         * @throws IOException Signals that an I/O exception has occurred. 
     626         */ 
     627        public void startTLS() throws IOException { 
     628                // Shortcut the method if we're already in SSL mode 
     629                if(socket instanceof TLS10Connection) { return; } 
     630                 
     631                try { 
     632                        TLS10Connection tlsSocket = new TLS10Connection( 
     633                                        new StreamConnectionWrapper( 
     634                                                socket, 
     635                                                (DataInputStream)input, 
     636                                                (DataOutputStream)output), 
     637                                        serverName + ':' + serverPort, 
     638                                        true); 
     639                         
     640                        socket = tlsSocket; 
     641                        input = socket.openDataInputStream(); 
     642                        output = socket.openDataOutputStream(); 
     643                } catch (IOException e) { 
     644            EventLogger.logEvent(AppInfo.GUID, 
     645                    ("Unable to switch to TLS mode: " + e.getMessage()).getBytes(), EventLogger.ERROR); 
     646            throw new IOException("Unable to switch to TLS mode"); 
     647                } catch (TLSException e) { 
     648            EventLogger.logEvent(AppInfo.GUID, 
     649                    ("Unable to switch to TLS mode: " + e.getMessage()).getBytes(), EventLogger.ERROR); 
     650            throw new IOException("Unable to switch to TLS mode"); 
     651                } 
     652        } 
     653         
     654        /** 
     655         * Decorator to wrap an existing stream connection so its I/O streams 
     656         * can be reopened without throwing exceptions. 
     657         */ 
     658        private static class StreamConnectionWrapper implements StreamConnection { 
     659                private StreamConnection stream; 
     660                private DataInputStream dataInputStream; 
     661                private DataOutputStream dataOutputStream; 
     662                 
     663                public StreamConnectionWrapper(StreamConnection stream, DataInputStream dataInputStream, DataOutputStream dataOutputStream) { 
     664                        this.stream = stream; 
     665                        this.dataInputStream = dataInputStream; 
     666                        this.dataOutputStream = dataOutputStream; 
     667                } 
     668                 
     669                public DataInputStream openDataInputStream() throws IOException { 
     670                        return dataInputStream; 
     671                } 
     672                public InputStream openInputStream() throws IOException { 
     673                        return dataInputStream; 
     674                } 
     675                public void close() throws IOException { 
     676                        stream.close(); 
     677                } 
     678                public DataOutputStream openDataOutputStream() throws IOException { 
     679                        return dataOutputStream; 
     680                } 
     681                public OutputStream openOutputStream() throws IOException { 
     682                        return dataOutputStream; 
     683                } 
     684        } 
    616685} 
Note: See TracChangeset for help on using the changeset viewer.