PageRenderTime 47ms CodeModel.GetById 21ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. import java.net.*;
  20. import java.util.Vector;
  21. import org.gjt.sp.jedit.*;
  22. import org.gjt.sp.jedit.gui.OptionsDialog;
  23. import org.gjt.sp.jedit.msg.PropertiesChanged;
  24. import org.gjt.sp.util.Log;
  25. /**
  26. * FirewallPlugin - a firewall authenticator plugin for jEdit
  27. * @author Dirk Moebius <dmoebius@gmx.net>
  28. * @version 0.2.1
  29. */
  30. public class FirewallPlugin extends EBPlugin {
  31. // begin EBPlugin implementation
  32. public void start() {
  33. propertiesChanged();
  34. }
  35. public void createOptionPanes(OptionsDialog optionsDialog) {
  36. optionsDialog.addOptionPane(new FirewallOptionPane());
  37. }
  38. public void handleMessage(EBMessage msg) {
  39. if (msg instanceof PropertiesChanged) {
  40. propertiesChanged();
  41. }
  42. }
  43. // end EBPlugin implementation
  44. private void propertiesChanged() {
  45. boolean enabled = jEdit.getBooleanProperty("firewall.enabled");
  46. if (!enabled) {
  47. Log.log(Log.DEBUG, this, "Firewall disabled");
  48. System.getProperties().remove("proxySet");
  49. System.getProperties().remove("proxyHost");
  50. System.getProperties().remove("proxyPort");
  51. System.getProperties().remove("http.proxyHost");
  52. System.getProperties().remove("http.proxyPort");
  53. System.getProperties().remove("http.nonProxyHosts");
  54. Authenticator.setDefault(null);
  55. } else {
  56. // set proxy host
  57. String host = jEdit.getProperty("firewall.host");
  58. if (host == null) {
  59. return;
  60. }
  61. System.setProperty("http.proxyHost", host);
  62. Log.log(Log.DEBUG, this, "Firewall enabled: " + host);
  63. // set proxy port
  64. String port = jEdit.getProperty("firewall.port");
  65. if (port != null) {
  66. System.setProperty("http.proxyPort", port);
  67. }
  68. // set non proxy hosts list
  69. String nonProxyHosts = jEdit.getProperty("firewall.nonProxyHosts");
  70. if (nonProxyHosts != null) {
  71. System.setProperty("http.nonProxyHosts", nonProxyHosts);
  72. }
  73. // set proxy authentication
  74. String username = jEdit.getProperty("firewall.user");
  75. if (username == null || username.length()==0) {
  76. Log.log(Log.DEBUG, this, "Firewall without user");
  77. Authenticator.setDefault(new FirewallAuthenticator(null));
  78. } else {
  79. Log.log(Log.DEBUG, this, "Firewall user: " + username);
  80. PasswordAuthentication pw = new PasswordAuthentication(
  81. username,
  82. jEdit.getProperty("firewall.password").toCharArray()
  83. );
  84. Authenticator.setDefault(new FirewallAuthenticator(pw));
  85. }
  86. }
  87. }
  88. }
  89. class FirewallAuthenticator extends Authenticator {
  90. PasswordAuthentication pw = null;
  91. public FirewallAuthenticator(PasswordAuthentication pw) {
  92. this.pw = pw;
  93. }
  94. protected PasswordAuthentication getPasswordAuthentication() {
  95. return pw;
  96. }
  97. }