1 29 38 39 package org.planetamessenger.mos.engine; 40 41 import java.sql.Connection ; 42 import java.sql.PreparedStatement ; 43 import java.sql.ResultSet ; 44 import java.sql.SQLException ; 45 import org.planetamessenger.system.*; 46 47 48 public class JProxySettings { 49 50 boolean bUseProxy; 51 boolean bRequestAuth; 52 53 JProxy proxySet; 54 String strSocksProxyHost; 55 String strHTTPProxyHost; 56 String strUserName; 57 String strUserPassword; 58 int nSocksProxyPort; 59 int nHTTPProxyPort; 60 61 62 69 public JProxySettings() { 70 71 proxySet = new JProxy(); 72 } 73 74 88 public void update( boolean bUseProxy, 89 String strSocksProxyHost, 90 int nSocksProxyPort, 91 String strHTTPProxyHost, 92 int nHTTPProxyPort, 93 boolean bRequestAuth, 94 String strUserName, 95 String strUserPassword ) { 96 97 int nUseProxy = ( bUseProxy ? 1 : 0 ); 98 int nRequestAuth = ( bRequestAuth ? 1 : 0 ); 99 Connection conn = null; 100 PreparedStatement st = null; 101 102 this.bUseProxy = bUseProxy; 103 this.strSocksProxyHost = strSocksProxyHost; 104 this.nSocksProxyPort = nSocksProxyPort; 105 this.strHTTPProxyHost = strHTTPProxyHost; 106 this.nHTTPProxyPort = nHTTPProxyPort; 107 this.bRequestAuth = bRequestAuth; 108 this.strUserName = strUserName; 109 this.strUserPassword = strUserPassword; 110 111 try { 112 conn = JSharedObjects.getDatabase().getConnection(); 113 st = conn.prepareStatement( "UPDATE profile SET use_proxy= ?, socks_proxy_host = ?, socks_proxy_port = ?, " + 114 "http_proxy_host = ?, http_proxy_port = ?, requires_auth = ?, " + 115 "proxy_user_name = ?, proxy_user_passwd = ? WHERE profile_id = ?" ); 116 117 st.setInt( 1, nUseProxy ); 118 st.setString( 2, strSocksProxyHost ); 119 st.setInt( 3, nSocksProxyPort ); 120 st.setString( 4, strHTTPProxyHost ); 121 st.setInt( 5, nHTTPProxyPort ); 122 st.setInt( 6, nRequestAuth ); 123 st.setString( 7, strUserName ); 124 st.setString( 8, strUserPassword ); 125 st.setLong( 9, JSharedObjects.getProfileManager().getProfileId() ); 126 st.executeUpdate(); 127 128 } catch( SQLException e ) { 129 System.err.println( "JProxySettings.updateSettings() - " + e ); 130 } finally { 131 if( st != null ) 132 try { 133 st.close(); 134 } catch( SQLException e ) { 135 System.err.println( "JProxySettings.updateSettings() - " + e ); 136 } 137 138 apply(); 139 } 140 } 141 142 147 public void load( long nProfileId ) { 148 149 Connection conn = null; 150 PreparedStatement st = null; 151 ResultSet resultSet = null; 152 153 try{ 154 conn = JSharedObjects.getDatabase().getConnection(); 155 st = conn.prepareStatement( "SELECT use_proxy, socks_proxy_host, socks_proxy_port, http_proxy_host, http_proxy_port, requires_auth, proxy_user_name, proxy_user_passwd FROM profile WHERE profile_id = ?" ); 156 st.setLong( 1, nProfileId ); 157 resultSet = st.executeQuery(); 158 159 if( resultSet == null ) 160 return; 161 162 if( resultSet.next() ) { 163 this.bUseProxy = ( resultSet.getInt( "use_proxy" ) == 1 ? true : false ); 164 this.strSocksProxyHost = resultSet.getString( "socks_proxy_host" ); 165 this.nSocksProxyPort = resultSet.getInt( "socks_proxy_port" ); 166 this.strHTTPProxyHost = resultSet.getString( "http_proxy_host" ); 167 this.nHTTPProxyPort = resultSet.getInt( "http_proxy_port" ); 168 this.bRequestAuth = ( resultSet.getInt( "requires_auth" ) == 1 ? true : false ); 169 this.strUserName = resultSet.getString( "proxy_user_name" ); 170 this.strUserPassword = resultSet.getString( "proxy_user_passwd" ); 171 } 172 else { 173 this.bUseProxy = false; 174 this.bRequestAuth = false; 175 this.strSocksProxyHost = ""; 176 this.nSocksProxyPort = 0; 177 this.strHTTPProxyHost = ""; 178 this.nHTTPProxyPort = 0; 179 this.strUserName = ""; 180 this.strUserPassword = ""; 181 } 182 183 resultSet.close(); 184 185 } catch( java.sql.SQLException e ) { 186 System.err.println( "JProxySettings.loadSettings() - SQLException " + e ); 187 } finally { 188 if( resultSet != null ) 189 try { 190 resultSet.close(); 191 } catch ( SQLException e ) {} 192 193 if( st != null ) { 194 try { 195 st.close(); 196 } catch ( SQLException e ) {} 197 } 198 199 apply(); 200 } 201 } 202 203 207 void apply() { 208 209 if( bUseProxy ) { 210 proxySet.setUserName( strUserName ); 211 proxySet.setPassword( strUserPassword ); 212 213 if( !strHTTPProxyHost.trim().equals( "" ) ) 215 proxySet.setHttpProxy( strHTTPProxyHost, nHTTPProxyPort ); 216 else 217 proxySet.clearHttpProxy(); 218 219 if( !strSocksProxyHost.trim().equals( "" ) ) 221 proxySet.setSocksProxy( strSocksProxyHost, nSocksProxyPort ); 222 else 223 proxySet.clearSocksProxy(); 224 } 225 else 226 proxySet.clearAll(); 227 } 228 229 232 public boolean getUseProxy() { 233 234 return bUseProxy; 235 } 236 237 240 public String getSocksProxyHost() { 241 242 return strSocksProxyHost; 243 } 244 245 248 public int getSocksProxyPort() { 249 250 return nSocksProxyPort; 251 } 252 253 256 public String getHTTPProxyHost() { 257 258 return strHTTPProxyHost; 259 } 260 261 264 public int getHTTPProxyPort() { 265 266 return nHTTPProxyPort; 267 } 268 269 272 public boolean getRequestAuth() { 273 274 return bRequestAuth; 275 } 276 277 280 public String getUserName() { 281 282 return strUserName; 283 } 284 285 288 public String getUserPassword() { 289 290 return strUserPassword; 291 } 292 } 293 294 | Popular Tags |