FileDocCategorySizeDatePackage
Protocol.javaAPI DocphoneME MR2 API (J2ME)11714Wed May 02 18:00:14 BST 2007com.sun.midp.io.j2me.ssl

Protocol

public class Protocol extends Object implements ConnectionBaseInterface, SecureConnection
This class implements the necessary functionality for an SSL connection.

Fields Summary
private static SecurityToken
classSecurityToken
This class has a different security domain than the MIDlet suite
private com.sun.midp.io.j2me.socket.Protocol
tcpConnection
Underlying TCP connection.
private SSLStreamConnection
sslConnection
Underlying SSL connection.
Constructors Summary
Methods Summary
public voidclose()
Close the connection to the target.

exception
IOException If an I/O error occurs

        try {
            sslConnection.close();
        } finally {
            tcpConnection.close();
        }
    
public java.lang.StringgetAddress()
Gets the remote address to which the socket is bound. The address can be either the remote host name or the IP address(if available).

return
the remote address to which the socket is bound.
exception
IOException if the connection was closed

	return tcpConnection.getAddress();
    
public java.lang.StringgetLocalAddress()
Gets the local address to which the socket is bound.

The host address(IP number) that can be used to connect to this end of the socket connection from an external system. Since IP addresses may be dynamically assigned a remote application will need to be robust in the face of IP number reassignment.

The local hostname (if available) can be accessed from System.getProperty("microedition.hostname")

return
the local address to which the socket is bound.
exception
IOException if the connection was closed
see
ServerSocketConnection

	return tcpConnection.getLocalAddress();
    
public intgetLocalPort()
Returns the local port to which this socket is bound.

return
the local port number to which this socket is connected.
exception
IOException if the connection was closed
see
ServerSocketConnection

	return tcpConnection.getLocalPort(); 
    
public intgetPort()
Returns the remote port to which this socket is bound.

return
the remote port number to which this socket is connected.
exception
IOException if the connection was closed

	return tcpConnection.getPort(); 
    
public SecurityInfogetSecurityInfo()
Return the security information associated with this connection. If the connection is still in Setup state then the connection is initiated to establish the secure connection to the server. The method returns when the connection is established and the Certificate supplied by the server has been validated. The SecurityInfo is only returned if the connection has been successfully made to the server.

return
the security information associated with this open connection.
exception
CertificateException if the Certificate supplied by the server cannot be validated. The CertificateException will contain the information about the error and indicate the certificate in the validation chain with the error.
exception
IOException if an arbitrary connection failure occurs

        return sslConnection.getSecurityInfo();
    
public intgetSocketOption(byte option)
Get a socket option for the connection.

param
option socket option identifier (KEEPALIVE, LINGER, SNDBUF, RCVBUF, or DELAY)
return
positive numeric value for specified option or -1 if the value is not available.
exception
IllegalArgumentException if the option identifier is not valid
exception
IOException if the connection was closed
see
#setSocketOption

	return tcpConnection.getSocketOption(option);
    
public java.io.DataInputStreamopenDataInputStream()
Open and return a data input stream for a connection.

return
An input stream
exception
IOException If an I/O error occurs

        return sslConnection.openDataInputStream();
    
public java.io.DataOutputStreamopenDataOutputStream()
Open and return a data output stream for a connection.

return
An input stream
exception
IOException If an I/O error occurs

        return sslConnection.openDataOutputStream();
    
public java.io.InputStreamopenInputStream()
Returns an input stream.

return
an input stream for writing bytes to this port.
exception
IOException if an I/O error occurs when creating the output stream.

        return sslConnection.openInputStream();
    
public java.io.OutputStreamopenOutputStream()
Returns an output stream.

return
an output stream for writing bytes to this port.
exception
IOException if an I/O error occurs when creating the output stream.

        return sslConnection.openOutputStream();
    
public ConnectionopenPrim(java.lang.String name, int mode, boolean timeouts)
Connect to the underlying secure socket transport.

param
name the target of the connection
param
mode a flag that is true if the caller intends to write to the connection, ignored
param
timeouts a flag to indicate that the called wants timeout exceptions, ignored
return
SSL/TCP stream connection
exception
IOException is thrown if the connection cannot be opened
exception
IllegalArgumentException if the name is bad


                                                                                                                            
           
              
        Scheduler scheduler = Scheduler.getScheduler();
        MIDletSuite midletSuite = scheduler.getMIDletSuite();
        HttpUrl url;
        OutputStream tcpOutputStream;
        InputStream tcpInputStream;

        if (tcpConnection != null) {
            // This method should only be called once.
            throw new RuntimeException("Illegal state for operation");
        }

        try {
            midletSuite.checkForPermission(Permissions.SSL, "ssl:" + name);
        } catch (InterruptedException ie) {
            throw new InterruptedIOException(
                "Interrupted while trying to ask the user permission");
        }

        if (name.charAt(0) != '/" || name.charAt(1) != '/") {
            throw new IllegalArgumentException(
                      "Protocol must start with \"//\"");
        }

        url = new HttpUrl("ssl", name); // parse name into host and port

        /*
         * Since we reused the HttpUrl parser, we must make sure that
         * there was nothing past the authority in the URL.
         */
        if (url.path != null || url.query != null || url.fragment != null) {
            throw new IllegalArgumentException("Malformed address");
        }

        /*
         * JTWI security check, untrusted MIDlets cannot open port 443.
         * This is so they cannot perform HTTPS
         * requests on server without using the system code. The
         * system HTTP code will add a "UNTRUSTED/1.0" to the user agent
         * field for untrusted MIDlets.
         */
        if (!midletSuite.isTrusted() && url.port == 443) {
            throw new SecurityException(
                "Target port denied to untrusted applications");
        }
            
        tcpConnection = new com.sun.midp.io.j2me.socket.Protocol();
        tcpConnection.openPrim(classSecurityToken, "//" + url.authority);
        try {
            tcpOutputStream = tcpConnection.openOutputStream();
            try {
                tcpInputStream = tcpConnection.openInputStream();

                /*
                 * Porting note: This would be the place to connect to a 
                 *               SOCKS proxy if desired.
                 */

                try {
                    // Get the SSLStreamConnection
                    sslConnection = new SSLStreamConnection(url.host, url.port,
                                    tcpInputStream, tcpOutputStream,
                                    WebPublicKeyStore.getTrustedKeyStore());
                } catch (IOException e) {
                    tcpInputStream.close();
                    throw e;
                }    
            } catch (IOException e) {
                tcpOutputStream.close();
                throw e;
            }    
        } catch (IOException e) {
            tcpConnection.close();
            throw e;
        }

        return this;
    
public voidsetSocketOption(byte option, int value)
Set a socket option for the connection.

Options inform the low level networking code about intended usage patterns that the application will use in dealing with the socket connection.

param
option socket option identifier (KEEPALIVE, LINGER, SNDBUF, RCVBUF, or DELAY)
param
value numeric value for specified option (must be positive)
exception
IllegalArgumentException if the value is not valid (e.g. negative value)
exception
IOException if the connection was closed
see
#getSocketOption

	tcpConnection.setSocketOption(option, value);