/Users/lyon/j4p/src/net/proxy/Proxy.java

1    /* 
2     * @author Douglas A. Lyon 
3     * @version  Nov 17, 2002.1:37:08 PM 
4     */ 
5    package net.proxy; 
6     
7    import java.net.Authenticator; 
8    import java.net.InetAddress; 
9    import java.net.PasswordAuthentication; 
10   import java.net.UnknownHostException; 
11   import java.util.Properties; 
12    
13   public class Proxy { 
14       public static void main(String[] args) { 
15           printProxy(); 
16       } 
17    
18       public static void printProxy() { 
19           setSoeProxy(); 
20           String s = System.getProperty( 
21                   "network.proxy.type"); 
22           System.out.println(s); 
23       } 
24    
25       /** 
26        * use a proxy server when connecting to the 
27        * web. SOE = School of Engineering at 
28        * Fairfield. For example: Proxy.setSoeProxy(); 
29        */ 
30       public static void setSoeProxy() { 
31           setHttpProxy("172.16.10.3", "8080"); 
32       } 
33    
34    
35       public static void setHttpProxy(String host, 
36                                       String port) { 
37           Properties p = System.getProperties(); 
38           p.put("proxySet", "true"); 
39           p.put("proxyHost", host); 
40           p.put("proxyPort", port); 
41       } 
42    
43       public static void setFtpProxy(String host, 
44                                      String port) { 
45           Properties p = System.getProperties(); 
46           p.put("ftpProxySet", "true"); 
47           p.put("ftpProxyHost", host); 
48           p.put("ftpProxyPort", port); 
49       } 
50    
51       public static PasswordAuthentication 
52               getHttpPasswordAuthentictor() { 
53           String scheme = "basic"; 
54           String prompt = "Enter UID and passwd"; 
55           String protocol = "http/1.1"; 
56           int port = 80; 
57           InetAddress inetAddr = 
58                   getLocalHost(); 
59           return Authenticator.requestPasswordAuthentication( 
60                   inetAddr, 
61                   80, 
62                   protocol, 
63                   prompt, 
64                   scheme); 
65       } 
66    
67       private static InetAddress getLocalHost() { 
68           try { 
69               return InetAddress.getLocalHost(); 
70           } catch (UnknownHostException e) { 
71               System.out.println( 
72                       "unable to get localhost IP address!"); 
73               return null; 
74           } 
75       } 
76   } 
77