1 29 38 39 package org.planetamessenger.system; 40 41 import java.util.*; 42 import java.net.*; 43 44 45 46 public class JProxy { 47 48 55 private final class ProxyAuth extends Authenticator { 56 57 private PasswordAuthentication auth; 58 59 66 private ProxyAuth( String strUserName, String strPasswd ) { 67 68 auth = new PasswordAuthentication( strUserName, strPasswd.toCharArray() ); 69 } 70 71 74 protected PasswordAuthentication getPasswordAuthentication() { 75 76 return auth; 77 } 78 } 79 84 85 ProxyAuth authenticator = null; 86 String strUserName = null; 87 String strPasswd = null; 88 89 90 94 public JProxy() { 95 96 } 97 98 103 public void setUserName( String strUserName ) { 104 105 this.strUserName = strUserName; 106 } 107 108 111 public String getUserName() { 112 113 return strUserName; 114 } 115 116 121 public void setPassword( String strPasswd ) { 122 123 this.strPasswd = strPasswd; 124 } 125 126 129 public String getPassword() { 130 131 return strPasswd; 132 } 133 134 139 public void setHttpProxy( String host, int nPort ) { 140 141 Properties prop = System.getProperties(); 142 143 prop.put( "http.proxySet", "true" ); 144 prop.put( "http.proxyHost", host ); 145 prop.put( "http.proxyPort", String.valueOf( nPort ) ); 146 147 if( ( strUserName != null ) && ( strPasswd != null ) ) { 149 if( !strUserName.trim().equals( "" ) && !strPasswd.trim().equals( "" ) ) { 150 prop.put( "http.proxyUser", strUserName ); 151 prop.put( "http.proxyPassword", strPasswd ); 152 153 if( authenticator == null ) { 154 authenticator = new ProxyAuth( strUserName, strPasswd ); 155 Authenticator.setDefault( authenticator ); 156 } 157 } 158 } 159 160 System.setProperties( prop ); 161 } 162 163 166 public void clearHttpProxy() { 167 168 Properties prop = System.getProperties(); 169 170 prop.remove( "http.proxyHost" ); 171 prop.remove( "http.proxyPort" ); 172 prop.remove( "http.proxySet" ); 173 prop.remove( "http.proxyUser" ); 174 prop.remove( "http.proxyPassword" ); 175 System.setProperties( prop ); 176 } 177 178 183 public void setSocksProxy( String host, int nPort ) { 184 185 Properties prop = System.getProperties(); 186 187 prop.put( "socksProxySet", "true" ); 188 prop.put( "socksProxyHost", host ); 189 prop.put( "socksProxyPort", String.valueOf( nPort ) ); 190 191 if( ( strUserName != null ) && ( strPasswd != null ) ) { 193 if( !strUserName.trim().equals( "" ) && !strPasswd.trim().equals( "" ) ) { 194 prop.put( "java.net.socks.username", strUserName ); 195 prop.put( "java.net.socks.password", strPasswd ); 196 197 if( authenticator == null ) { 198 authenticator = new ProxyAuth( strUserName, strPasswd ); 199 Authenticator.setDefault( authenticator ); 200 } 201 } 202 } 203 204 System.setProperties( prop ); 205 } 206 207 210 public void clearSocksProxy() { 211 212 Properties prop = System.getProperties(); 213 214 prop.remove( "socksProxyHost" ); 215 prop.remove( "socksProxyPort" ); 216 prop.remove( "java.net.socks.username" ); 217 prop.remove( "java.net.socks.password" ); 218 System.setProperties( prop ); 219 } 220 221 225 public void clearAll() { 226 227 this.clearHttpProxy(); 228 this.clearSocksProxy(); 229 230 if( authenticator != null ) 231 Authenticator.setDefault( null ); 232 } 233 } 234 235 | Popular Tags |