001: package com.sun.portal.netlet.client.common;
002:
003: import com.sun.portal.netlet.econnection.Base64;
004:
005: import java.io.*;
006: import java.net.*;
007: import java.util.PropertyResourceBundle;
008:
009: public class ClientUtil {
010:
011: public static InputStream netletConfig(String func) {
012: InputStream is = null;
013: try {
014: String jsDocumentDomain = ClientConfig
015: .getParam("jsDocumentDomain");
016: URL url = null;
017: if (jsDocumentDomain != null) {
018: url = new URL(ClientConfig.getTargetURL() + "?func="
019: + func + "&document.domain=" + jsDocumentDomain);
020: } else {
021: url = new URL(ClientConfig.getTargetURL() + "?func="
022: + func);
023: }
024: System.out.println("Netlet config: " + url.toString());
025: URLConnection uconn = url.openConnection();
026: uconn.setRequestProperty("Cookie", ClientConfig
027: .getCookieName()
028: + "="
029: + URLEncoder.encode(ClientConfig.getSessionID()));
030: uconn.setRequestProperty("PS-Netlet", "enabled=true");
031: uconn.setDoInput(true);
032: uconn.setUseCaches(false);
033: uconn.setAllowUserInteraction(false);
034: //reverting to original code which was altered as a fix for bug : 6272139
035: // if(!func.equalsIgnoreCase("unload") && !func.equalsIgnoreCase("stopped")) {
036: is = uconn.getInputStream();
037: // }
038: } catch (MalformedURLException e) {
039: e.printStackTrace();
040: System.err.println("MalformedURLException " + e);
041: } catch (java.io.IOException e) {
042: e.printStackTrace();
043: System.err.println("IOException " + e);
044: }
045:
046: return is;
047: }
048:
049: public static InputStream sendByPost(String func, String postBody) {
050: InputStream is = null;
051: try {
052: String jsDocumentDomain = ClientConfig
053: .getParam("jsDocumentDomain");
054: URL url = null;
055: if (jsDocumentDomain != null) {
056: url = new URL(ClientConfig.getTargetURL() + "?func="
057: + func + "&document.domain=" + jsDocumentDomain);
058: } else {
059: url = new URL(ClientConfig.getTargetURL() + "?func="
060: + func);
061: }
062: System.out.println("Post URL -> " + url.toString());
063: URLConnection uconn = url.openConnection();
064: uconn.setRequestProperty("Cookie", ClientConfig
065: .getCookieName()
066: + "="
067: + URLEncoder.encode(ClientConfig.getSessionID()));
068: uconn.setRequestProperty("PS-Netlet", "enabled=true");
069: uconn.setDoOutput(true);
070: uconn.setDoInput(true);
071: uconn.setUseCaches(false);
072: uconn.setAllowUserInteraction(false);
073: DataOutputStream out = new DataOutputStream(uconn
074: .getOutputStream());
075: out.writeBytes(postBody);
076: out.flush();
077: out.close();
078: out = null;
079: is = uconn.getInputStream();
080: } catch (java.io.IOException e) {
081: System.out
082: .println("sendByPost: Unable to connect to server -> "
083: + e);
084: }
085: return is;
086: }
087:
088: // rfc1522 encoding for proxy auth username/password (rfc2068)
089: public static String rfc1522(String in) {
090: boolean hi = false;
091: for (int c = 0; c < in.length(); c++) {
092: int tc = (int) in.charAt(c);
093: if (tc > 0x007f) {
094: hi = true;
095: break;
096: }
097: }
098: // no hibyte, don't encode
099: if (!hi) {
100: return in;
101: } else {
102: return "=?UTF-8?B?" + Base64.encode(in) + "?=";
103: }
104: }
105:
106: public static String parsePACFile(String autoConfigURL) {
107: String result = "FAILED";
108: String pacFileBody = "No";
109: boolean redirect = false;
110: InputStream autoConfigURLInputStream = null;
111: String contentType = new String(); //set some thing so that it wont throw null pointer Exception
112: try {
113: do {
114: redirect = false;
115: if (autoConfigURL.trim().startsWith("http")) {
116: URL configURL = new URL(autoConfigURL);
117: URLConnection configURLConn = configURL
118: .openConnection();
119: configURLConn.setUseCaches(false);
120: configURLConn.setDoInput(true);
121: configURLConn.setAllowUserInteraction(false);
122: contentType = configURLConn.getContentType();
123: autoConfigURLInputStream = configURLConn
124: .getInputStream();
125: } else {
126: if (autoConfigURL.charAt(7) != '/') {
127: autoConfigURL = "file:///"
128: + autoConfigURL.substring(7);
129: }
130: URL url = new URL(autoConfigURL);
131: URLConnection conn = url.openConnection();
132: autoConfigURLInputStream = conn.getInputStream();
133: contentType = "file";
134: }
135: if (contentType
136: .equalsIgnoreCase("application/x-internet-signup")
137: || autoConfigURL.trim().toUpperCase().endsWith(
138: ".INS")) {
139: PropertyResourceBundle insRes = null;
140: String autoConfigJS = null;
141: try {
142: DataInputStream in = new DataInputStream(
143: autoConfigURLInputStream);
144: insRes = new PropertyResourceBundle(in);
145: autoConfigJS = insRes
146: .getString("AutoConfigJSURL");
147: } catch (Exception ex) {
148: ex.printStackTrace();
149: }
150:
151: if (autoConfigJS != null
152: && (!autoConfigJS.trim().equals(""))) {
153: autoConfigURL = autoConfigJS;
154: redirect = true;
155: } else {
156: String redirectAutoConfigURL = null;
157: try {
158: redirectAutoConfigURL = insRes
159: .getString("AutoConfigURL");
160: } catch (Exception ex) {
161: ex.printStackTrace();
162: }
163: if (redirectAutoConfigURL != null
164: && (!redirectAutoConfigURL.trim()
165: .equals(""))) {
166: if (!redirectAutoConfigURL.trim()
167: .equalsIgnoreCase(
168: autoConfigURL.trim())) {
169: //it is a real case of ins redirection
170: autoConfigURL = redirectAutoConfigURL;
171: redirect = true;
172: } else {
173: System.out
174: .println("AutoConfigJSURL is not defined in the Ins file");
175: return "DIRECT";
176: }
177: } else {
178: System.out
179: .println("AutoConfigURL is not defined in the Ins file");
180: return "DIRECT";
181: }
182: }
183: }
184: } while (redirect);
185:
186: DataInputStream in = new DataInputStream(
187: autoConfigURLInputStream);
188:
189: byte data[] = null;
190: byte buf[] = new byte[128];
191: ByteArrayOutputStream baos = new ByteArrayOutputStream();
192: DataOutputStream dos = new DataOutputStream(baos);
193: int numberOfBytes = 1;
194: while (numberOfBytes > 0) {
195: try {
196: numberOfBytes = in.read(buf);
197: if (numberOfBytes > 0) {
198: dos.write(buf, 0, numberOfBytes);
199: }
200: } catch (IOException ioe) {
201: numberOfBytes = 0;
202: }
203: }
204: try {
205: dos.flush();
206: data = baos.toByteArray();
207: in.close();
208: dos.close();
209: System.out.println("PAC file content length -> "
210: + data.length);
211: } catch (Exception ex) {
212: ex.printStackTrace();
213: }
214: pacFileBody = new String(data);
215: pacFileBody = URLEncoder.encode(pacFileBody);
216: } catch (Exception ex) {
217: ex.printStackTrace();
218: }
219:
220: //get the Pac file Body
221: String qString = "parsePacFile" + "&PacFileUrl="
222: + autoConfigURL;
223: qString += ("&ServerURL=" + ClientConfig.getTargetURL());
224: String myIPAddr = "255.255.255.255";
225: try {
226: myIPAddr = InetAddress.getLocalHost().getHostAddress();
227: } catch (Exception ex) {
228: ex.printStackTrace();
229: }
230: qString += ("&ClientIPAddr=" + myIPAddr);
231:
232: String qSt = "&PacFileBody=" + pacFileBody;
233:
234: //pacFileBody is no more required
235: pacFileBody = null;
236:
237: //send the pac file body to netletConfig servlet
238: BufferedReader in = null;
239: try {
240: in = new BufferedReader(new InputStreamReader(sendByPost(
241: qString, qSt)));
242: //this will return null if it fails,like if js.jar is not there
243: if (result == null || result.equals("null"))
244: result = "FAILED";
245: else
246: result = in.readLine();
247:
248: System.out.println(result);
249: } catch (Exception ex) {
250: ex.printStackTrace();
251: }
252: return result;
253: }
254:
255: /*
256: * Checks whether the proxy settings stored in member variables is valid or not.
257: */
258: public static boolean isProxySettingsValid(String proxySSL,
259: int proxySSLPort) {
260: ProxySConn sconn = null;
261: try {
262: // try regular connection
263: sconn = new ProxySConn(proxySSLPort, proxySSL, ClientConfig
264: .getDestPort(), ClientConfig.getDestHost(), null);
265: } catch (ProxyAuthNeededException e) {
266: // try again with proxy auth
267: try {
268: sconn = new ProxySConn(proxySSLPort, proxySSL,
269: ClientConfig.getDestPort(), ClientConfig
270: .getDestHost(), true, null);
271: } catch (ProxyAuthNeededException ee) {
272: System.out.println("Invalid proxy information");
273: } catch (ProxyAuthFailedException ee) {
274: System.out.println("Invalid proxy information");
275: }
276: } catch (ProxyAuthFailedException e) {
277: System.out.println("Invalid proxy information");
278: }
279: Socket out_s = sconn.getconn();
280: if (out_s != null) {
281: return true;
282: } else {
283: return false;
284: }
285: }
286:
287: public static boolean isClientBindIPValid(String clientBindIP) {
288: ServerSocket echoSock = null;
289: boolean valid = false;
290: boolean go = true;
291: int retries = 5; // Max 5 retries for port hunting.
292:
293: while (go) {
294: try {
295: InetAddress bindAddr = null;
296: bindAddr = InetAddress.getByName(clientBindIP);
297: BrowserType browser = BrowserType.getInstance();
298: if (browser.getIEBrowser()
299: && browser.getJavaVMVersion() == 1) {
300: try {
301: if (Class
302: .forName("com.ms.security.PolicyEngine") != null) {
303: com.ms.security.PolicyEngine
304: .assertPermission(com.ms.security.PermissionID.NETIO);
305: com.ms.security.PolicyEngine
306: .assertPermission(com.ms.security.PermissionID.FILEIO);
307: }
308: } catch (Exception cnfe) {
309: //ignore
310: }
311: }
312: echoSock = new ServerSocket(0, 10, bindAddr);
313: valid = true;
314: if (echoSock != null) {
315: echoSock.close();
316: echoSock = null;
317: }
318: go = false;
319: } catch (UnknownHostException uhe) {
320: System.out
321: .println("Unable to make server socket to host "
322: + clientBindIP + " " + uhe);
323: echoSock = null;
324: go = false;
325: } catch (IOException ioe) {
326: System.out
327: .println("Unable to make server socket to host "
328: + clientBindIP + " " + ioe);
329: echoSock = null;
330: // Try binding to a freely availble port.
331: go = retries > 0 ? true : false;
332: --retries;
333: }
334: }
335: return valid;
336: }
337:
338: public static boolean wildcardMatch(String str1, String str2) {
339: int beginIndex1 = 0;
340: int endIndex1 = 0;
341: int beginIndex2 = 0;
342: int endIndex2 = 0;
343: int strlen1 = str1.length();
344: int strlen2 = str2.length();
345: String substr = null;
346:
347: // if one of the string is null, consider it no match.
348: if ((str1 == null) || (str2 == null))
349: return (false);
350:
351: if ((endIndex2 = str2.indexOf('*', beginIndex2)) != -1) {
352: // get the substring prior to the first '*'
353: substr = str2.substring(beginIndex2, endIndex2);
354:
355: // check if the first char in str2 is '*', i.e. the substring is null
356: if (endIndex2 > beginIndex2) {
357: // str1 contains the substring too? if not, no match
358: if ((beginIndex1 = str1.indexOf(substr, beginIndex1)) == -1)
359: return (false);
360: // if it is not a SUFFIX match, then the prefixes should be equal
361: if ((beginIndex1 != beginIndex2))
362: return (false);
363: }
364: // move the pointer to next char after the substring already matched
365: beginIndex1 = beginIndex1 + (endIndex2 - beginIndex2);
366: if (endIndex2 >= strlen2 - 1)
367: return (true);
368: beginIndex2 = endIndex2 + 1;
369: } else // str2 doesn't contain wildcard '*'
370: {
371: if ((beginIndex1 = str1.indexOf(str2)) == -1)
372: return (false);
373: if ((strlen1 == strlen2))
374: return (true);
375: return (false);
376: }
377:
378: // There are more than '*'s in str2, repeat what we have done
379: while ((endIndex2 = str2.indexOf('*', beginIndex2)) != -1) {
380: substr = str2.substring(beginIndex2, endIndex2);
381: if (endIndex2 > beginIndex2)
382: if ((beginIndex1 = str1.indexOf(substr, beginIndex1)) == -1)
383: return (false);
384: beginIndex1 = beginIndex1 + (endIndex2 - beginIndex2);
385: if (endIndex2 >= strlen2 - 1)
386: return (true);
387: beginIndex2 = endIndex2 + 1;
388: }
389: // The substring after the last '*'
390: substr = str2.substring(beginIndex2, strlen2);
391:
392: if ((endIndex1 = str1.lastIndexOf(substr, strlen1 - 1)) == -1)
393: return (false);
394:
395: if (beginIndex1 > endIndex1)
396: return (false);
397:
398: beginIndex1 = endIndex1;
399: if (((strlen1 - beginIndex1) == (strlen2 - beginIndex2)))
400: return (true);
401: return (false);
402: }
403: }
|