KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > system > JProxy


1 /*
2     =========================================================================
3     Package system - System routines.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JProxy.java,v 1.6 2007/02/04 01:12:35 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.6 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.system;
40
41 import java.util.*;
42 import java.net.*;
43
44
45
46 public class JProxy {
47
48   /**
49    * =========================================================================
50    * Authenticator class implementation.
51    * Implements the object authenticator to use internally by JProxy
52    * class to authenticate socks proxy on virtual machine.
53    *=========================================================================
54    */

55   private final class ProxyAuth extends Authenticator {
56     
57     private PasswordAuthentication auth;
58
59     /**
60      * Constructor. Initializes all class data.
61      * @param strUserName The username to be
62      * retrieved by Authenticator by virtual machine;
63      * @param strPasswd The username to be
64      * retrieved by Authenticator by virtual machine;
65      */

66     private ProxyAuth( String JavaDoc strUserName, String JavaDoc strPasswd ) {
67
68       auth = new PasswordAuthentication( strUserName, strPasswd.toCharArray() );
69     }
70
71     /**
72      * Return the PasswordAuthentication to virtaul machine.
73      */

74     protected PasswordAuthentication getPasswordAuthentication() {
75       
76       return auth;
77     }
78   }
79   /*
80    * =========================================================================
81    * ProxyAuth inner class
82    * =========================================================================
83    */

84   
85   ProxyAuth authenticator = null;
86   String JavaDoc strUserName = null;
87   String JavaDoc strPasswd = null;
88
89
90   /**
91    * Contruct and initialize all
92    * class data.
93    */

94   public JProxy() {
95
96   }
97
98   /**
99    * Set the user name for authentication on proxy server.
100    * @param strUserName the username to set (to clear user name,
101    * set this parameter to null or empty string);
102    */

103   public void setUserName( String JavaDoc strUserName ) {
104   
105     this.strUserName = strUserName;
106   }
107   
108   /**
109    * Return the user name for authentication on proxy server.
110    */

111   public String JavaDoc getUserName() {
112     
113     return strUserName;
114   }
115   
116   /**
117    * Set the user password for authentication on proxy server.
118    * @param strPasswd the password to set (to clear password,
119    * set this parameter to null or empty string);
120    */

121   public void setPassword( String JavaDoc strPasswd ) {
122   
123     this.strPasswd = strPasswd;
124   }
125   
126   /**
127    * Return the user password for authentication on proxy server.
128    */

129   public String JavaDoc getPassword() {
130     
131     return strPasswd;
132   }
133   
134   /**
135    * set a http proxy
136    * @param host The ip of the proxy to use
137    * @param port The port of the proxy to use
138    */

139   public void setHttpProxy( String JavaDoc 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     // Use HTTP authentication
148
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   /**
164    * clear all http proxy settings
165    */

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   /**
179    * set a socks proxy
180    * @param host The ip of the proxy to use
181    * @param port The port of the proxy to use
182    */

183   public void setSocksProxy( String JavaDoc 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     // Use socks 5 authentication
192
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   /**
208    * clear all socks proxy settings
209    */

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   /**
222    * Clear all object data, Authenticator, user name
223    * and password stored on it.
224    */

225   public void clearAll() {
226     
227     this.clearHttpProxy();
228     this.clearSocksProxy();
229     
230     if( authenticator != null )
231       Authenticator.setDefault( null );
232   }
233 }
234
235 // JProxySettings class
Popular Tags