001: /*
002: * KSSL is part of J2ME and does not have the concept of Sockets, instead they are
003: * refered to as Streams. A simple adapter that makes KSSL Stream compactabile with
004: * J2SE Sockets.
005: *
006: * @author Rajesh T
007: * @date 16-July-2003
008: * @version 1.0
009: */
010:
011: package com.sun.portal.netlet.client.common;
012:
013: import java.net.*;
014: import java.io.*;
015: import java.util.*;
016:
017: import com.sun.portal.kssl.SSLStreamConnection;
018:
019: public class KSSLSocket extends Socket {
020:
021: private SSLStreamConnection sslconn = null;
022:
023: /*
024: * Creates a raw Socket and wraps aroung KSSL implementation. This will not
025: * report unconnected sockets.
026: */
027:
028: public KSSLSocket(String destHost, int destPort,
029: String[] cipherSuites) throws UnknownHostException,
030: IOException {
031:
032: super (destHost, destPort);
033:
034: if (cipherSuites != null) {
035: String[] ksslCiphers = getKSSLCipherSuites(cipherSuites);
036: if (ksslCiphers != null) {
037: sslconn = new SSLStreamConnection(destHost, destPort,
038: super .getInputStream(),
039: super .getOutputStream(), ksslCiphers);
040: } else {
041:
042: sslconn = new SSLStreamConnection(destHost, destPort,
043: super .getInputStream(), super .getOutputStream());
044: }
045: } else {
046: sslconn = new SSLStreamConnection(destHost, destPort, super
047: .getInputStream(), super .getOutputStream());
048: }
049:
050: try {
051: this .setTcpNoDelay(true);
052: } catch (Exception e) {
053: e.printStackTrace();
054: }
055:
056: }
057:
058: /*
059: * Adding a Socket argument constructor to J2SE Socket. Printing this Socket will
060: * report a extra unconnected Socket.
061: */
062:
063: public KSSLSocket(Socket tunnelSocket, String destHost,
064: int destPort, String[] cipherSuites)
065: throws UnknownHostException, IOException {
066:
067: super ();
068:
069: if (cipherSuites != null) {
070: String[] ksslCiphers = getKSSLCipherSuites(cipherSuites);
071:
072: if (ksslCiphers != null) {
073: sslconn = new SSLStreamConnection(destHost, destPort,
074: tunnelSocket.getInputStream(), tunnelSocket
075: .getOutputStream(), ksslCiphers);
076: } else {
077: sslconn = new SSLStreamConnection(destHost, destPort,
078: tunnelSocket.getInputStream(), tunnelSocket
079: .getOutputStream());
080: }
081: } else {
082: sslconn = new SSLStreamConnection(destHost, destPort,
083: tunnelSocket.getInputStream(), tunnelSocket
084: .getOutputStream());
085: }
086: }
087:
088: /*
089: * Override and return the actual KSSL's Streams
090: */
091:
092: public InputStream getInputStream() throws IOException {
093: return sslconn.openInputStream();
094: }
095:
096: /*
097: * Override and return the actual KSSL's Streams
098: */
099:
100: public OutputStream getOutputStream() throws IOException {
101: return sslconn.openOutputStream();
102: }
103:
104: /*
105: */
106: public void close() throws IOException {
107: // super.close();
108: sslconn.close();
109: }
110:
111: private String[] getKSSLCipherSuites(String cipherSuites[]) {
112: Vector ksslCiphers = new Vector();
113:
114: for (int i = 0; i < cipherSuites.length; i++) {
115: if (cipherSuites[i] != null && cipherSuites[i] != "null"
116: && cipherSuites[i].startsWith("KSSL_")) {
117: ksslCiphers.addElement(new String(cipherSuites[i]));
118: }
119: }
120: if (ksslCiphers.size() == 0) {
121: return null;
122: }
123:
124: String arrayofKSSLCiphers[] = new String[ksslCiphers.size()];
125: for (int j = 0; j < ksslCiphers.size(); j++) {
126: arrayofKSSLCiphers[j] = ksslCiphers.elementAt(j).toString();
127: }
128: return arrayofKSSLCiphers;
129: }
130: }
|