001: // %Z%%M% %I% "%W% %E% Sun Microsystems"
002:
003: package com.sun.portal.netlet.client.common;
004:
005: import com.sun.portal.netlet.econnection.Base64;
006: import java.awt.*;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.io.*;
010: import java.util.StringTokenizer;
011:
012: public class ProxySConn extends Connection implements ActionListener {
013:
014: private DataInputStream in_b;
015: private DataOutputStream out_b;
016:
017: private String dhost;
018: private int dport;
019:
020: private ProxyAuthDialog pad;
021: private String puser;
022: private String ppass;
023:
024: /**
025: * Create plain socket only for connecting applet to local user's FTP client.
026: * Do not use this constructor for any other purpose.
027: */
028: public ProxySConn(int pp, String ph, boolean toClient) {
029: super (pp, ph);
030: }
031:
032: public ProxySConn(int pp, String ph, String cipherName) {
033: super (pp, ph);
034: NetletSocketFactory nsf = NetletSocketFactory
035: .getNetletSocketFactory(ClientConfig.getDestProtocol(),
036: ClientConfig.isJSSEEnabled(), ClientConfig
037: .isPDCEnabled());
038:
039: if (cipherName != null) {
040: conn = nsf.getSocket(conn, ph, pp,
041: parseCipherList(cipherName));
042: } else {
043: conn = nsf.getSocket(conn, ph, pp, null);
044: }
045:
046: }
047:
048: public ProxySConn(int pp, String ph, int dp, String dh,
049: String cipherName) throws ProxyAuthNeededException,
050: ProxyAuthFailedException {
051: this (pp, ph, dp, dh, false, cipherName);
052: }
053:
054: public ProxySConn(int pp, String ph, int dp, String dh,
055: boolean doauth, String cipherName)
056: throws ProxyAuthNeededException, ProxyAuthFailedException {
057:
058: super (pp, ph);
059: dhost = dh;
060: dport = dp;
061: if (conn != null) {
062: try {
063: in_b = new DataInputStream(conn.getInputStream());
064: out_b = new DataOutputStream(conn.getOutputStream());
065: } catch (IOException e) {
066: System.out.println("ProxySConn setup IOE: " + e);
067: try {
068: conn.close();
069: } catch (IOException ie) {
070: }
071: conn = null;
072: return;
073: }
074: if (doProxyConnect(doauth) != 0) {
075: try {
076: conn.close();
077: } catch (IOException ie) {
078: }
079: conn = null;
080: }
081:
082: NetletSocketFactory nsf = NetletSocketFactory
083: .getNetletSocketFactory(ClientConfig
084: .getDestProtocol(), ClientConfig
085: .isJSSEEnabled(), ClientConfig
086: .isPDCEnabled());
087:
088: if (cipherName != null) {
089: conn = nsf.getSocket(conn, dh, dp,
090: parseCipherList(cipherName));
091: } else {
092: conn = nsf.getSocket(conn, dh, dp, null);
093: }
094: }
095: }
096:
097: /*
098: * connect to web proxy server. deal with proxy-auth if we can
099: * @param doauth put of proxy auth dialog and do proxy auth
100: */
101: public int doProxyConnect(boolean doauth)
102: throws ProxyAuthNeededException, ProxyAuthFailedException {
103: int rc = 0;
104:
105: String header = "CONNECT " + dhost + ":" + dport
106: + " HTTP/1.0\r\n";
107:
108: try {
109: // either force it, or check to see if it's been done already
110: if (doauth || (ClientConfig.getProxyAuth())) {
111: // check for pre-entered info
112: puser = ClientConfig.getProxyAuthUsername();
113: ppass = ClientConfig.getProxyAuthPassword();
114:
115: if (puser == null || ppass == null) {
116: pad = new ProxyAuthDialog((new Frame()), this );
117: System.out
118: .println("Netlet showing proxy authentication dialog");
119: pad.showWarning();
120: pad.waitForAction();
121: }
122:
123: // if either are null, user cancelled
124: // disallow hi-byte chars for now, we won't be able to test this, so we can't support it
125: if (puser == null || ppass == null) {
126: throw new ProxyAuthFailedException(
127: ResourceProperties.getString("psconn.1"));
128: // Proxy authentication no username/password
129: }
130:
131: header += "Proxy-Authorization: Basic "
132: + Base64.encode(puser + ":" + ppass)
133: + "\r\n\r\n";
134: } else {
135: header += "\r\n";
136: }
137:
138: out_b.write(header.getBytes(), 0, header.length());
139:
140: BufferedReader br = new BufferedReader(
141: new InputStreamReader(in_b));
142: String line = "";
143: while (true) {
144: line = br.readLine();
145: if (line == null || line.length() < 3)
146: break;
147: // looking for: HTTP/1.0 407 Proxy-Authentication Required
148: if (line.startsWith("HTTP/1")) {
149: int s = line.indexOf(" ");
150: String num = line.substring(s + 1);
151: if (num.startsWith("407")) {
152: // jump out of here if we need to do auth (not shown in dialog)
153: throw new ProxyAuthNeededException(
154: "Proxy Authentication Required");
155: } else if (num.startsWith("401")) {
156: throw new ProxyAuthFailedException(
157: ResourceProperties
158: .getString("psconn.3"));
159: // Proxy Digest Authentication Not Supported
160: }
161: }
162: }
163: } catch (StringIndexOutOfBoundsException e) {
164: System.out.println("ProxySConn SIOBE connecting to proxy: "
165: + e);
166: rc = -1;
167: } catch (IOException e) {
168: System.out.println("ProxySConn IOE connecting to proxy: "
169: + e);
170: rc = -1;
171: }
172:
173: return (rc);
174: }
175:
176: /*
177: * deal with events from ProxyAuthDialog
178: */
179: public void actionPerformed(ActionEvent evt) {
180: // return in password field to submit has 'cancel' from setActionCommand(),
181: // so ignore 'cancel' since it's only event we don't want
182: if (!"cancel".equals(evt.getActionCommand())) {
183: puser = ClientUtil.rfc1522(pad.getUsername());
184: ppass = ClientUtil.rfc1522(pad.getPassword());
185: ClientConfig.setProxyAuthUsername(puser);
186: ClientConfig.setProxyAuthPassword(ppass);
187: }
188: // cancel falls through
189: pad.notifyAction();
190: pad.setVisible(false);
191: pad.dispose();
192: }
193:
194: private String[] parseCipherList(String cipherList) {
195: StringTokenizer stz = new StringTokenizer(cipherList, "|");
196: String[] cipherArray = new String[stz.countTokens()];
197: int i = 0;
198: while (stz.hasMoreElements()) {
199: cipherArray[i] = stz.nextElement().toString();
200: }
201: return cipherArray;
202: }
203: }
|