PageRenderTime 63ms CodeModel.GetById 53ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/jars/Firewall/FirewallPlugin.java

#
Java | 108 lines | 68 code | 11 blank | 29 comment | 13 complexity | ea29057bf0bf22db004cf96583a27c96 MD5 | raw file
  1/*
  2 * FirewallPlugin.java - a firewall authenticator plugin for jEdit
  3 * Copyright (C) 1999 Dirk Moebius
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20import java.net.*;
 21import java.util.Vector;
 22import org.gjt.sp.jedit.*;
 23import org.gjt.sp.jedit.gui.OptionsDialog;
 24import org.gjt.sp.jedit.msg.PropertiesChanged;
 25import org.gjt.sp.util.Log;
 26
 27
 28/**
 29 * FirewallPlugin - a firewall authenticator plugin for jEdit
 30 * @author Dirk Moebius <dmoebius@gmx.net>
 31 * @version 0.2.1
 32 */
 33public class FirewallPlugin extends EBPlugin {
 34    
 35    // begin EBPlugin implementation
 36    public void start() { 
 37        propertiesChanged();
 38    }
 39    
 40    public void createOptionPanes(OptionsDialog optionsDialog) {
 41        optionsDialog.addOptionPane(new FirewallOptionPane());
 42    }
 43
 44    public void handleMessage(EBMessage msg) {
 45        if (msg instanceof PropertiesChanged) {
 46            propertiesChanged();
 47        }
 48    }
 49    // end EBPlugin implementation
 50
 51    
 52    private void propertiesChanged() {
 53        boolean enabled = jEdit.getBooleanProperty("firewall.enabled");
 54        if (!enabled) {
 55            Log.log(Log.DEBUG, this, "Firewall disabled");
 56            System.getProperties().remove("proxySet");
 57            System.getProperties().remove("proxyHost");
 58            System.getProperties().remove("proxyPort");        
 59            System.getProperties().remove("http.proxyHost");
 60            System.getProperties().remove("http.proxyPort");
 61            System.getProperties().remove("http.nonProxyHosts");
 62            Authenticator.setDefault(null);
 63        } else {
 64            // set proxy host
 65            String host = jEdit.getProperty("firewall.host");
 66            if (host == null) {
 67                return;
 68            }
 69            System.setProperty("http.proxyHost", host);
 70            Log.log(Log.DEBUG, this, "Firewall enabled: " + host);
 71            // set proxy port
 72            String port = jEdit.getProperty("firewall.port");
 73            if (port != null) {
 74                System.setProperty("http.proxyPort", port);
 75            }
 76            // set non proxy hosts list
 77            String nonProxyHosts = jEdit.getProperty("firewall.nonProxyHosts");
 78            if (nonProxyHosts != null) {
 79                System.setProperty("http.nonProxyHosts", nonProxyHosts);
 80            }
 81            // set proxy authentication
 82            String username = jEdit.getProperty("firewall.user");
 83            if (username == null || username.length()==0) {
 84                Log.log(Log.DEBUG, this, "Firewall without user");
 85                Authenticator.setDefault(new FirewallAuthenticator(null));
 86            } else {
 87                Log.log(Log.DEBUG, this, "Firewall user: " + username);
 88                PasswordAuthentication pw = new PasswordAuthentication(
 89                    username, 
 90                    jEdit.getProperty("firewall.password").toCharArray()
 91                );
 92                Authenticator.setDefault(new FirewallAuthenticator(pw));
 93            }
 94        }
 95    }
 96}
 97
 98
 99class FirewallAuthenticator extends Authenticator {
100    PasswordAuthentication pw = null;
101    public FirewallAuthenticator(PasswordAuthentication pw) {
102        this.pw = pw;
103    }
104    protected PasswordAuthentication getPasswordAuthentication() {
105        return pw;
106    }
107}
108