| 1 |
package com.github.markusbernhardt.proxy.ui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.event.ActionEvent; |
| 5 |
import java.awt.event.ActionListener; |
| 6 |
import java.net.Proxy; |
| 7 |
import java.net.ProxySelector; |
| 8 |
import java.net.URL; |
| 9 |
import java.text.MessageFormat; |
| 10 |
import java.util.List; |
| 11 |
|
| 12 |
import javax.swing.JButton; |
| 13 |
import javax.swing.JComboBox; |
| 14 |
import javax.swing.JFrame; |
| 15 |
import javax.swing.JLabel; |
| 16 |
import javax.swing.JOptionPane; |
| 17 |
import javax.swing.JPanel; |
| 18 |
import javax.swing.JScrollPane; |
| 19 |
import javax.swing.JTextArea; |
| 20 |
import javax.swing.JTextField; |
| 21 |
import javax.swing.SwingUtilities; |
| 22 |
import javax.swing.UIManager; |
| 23 |
|
| 24 |
import com.github.markusbernhardt.proxy.ProxySearch; |
| 25 |
import com.github.markusbernhardt.proxy.ProxySearch.Strategy; |
| 26 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 27 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ProxyTester extends JFrame { |
| 38 |
|
| 39 |
private static final long serialVersionUID = 1L; |
| 40 |
|
| 41 |
private JComboBox<ProxySearch.Strategy> modes; |
| 42 |
private JButton testButton; |
| 43 |
private JTextField urlField; |
| 44 |
|
| 45 |
private JTextArea logArea; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public ProxyTester() { |
| 52 |
super(); |
| 53 |
init(); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private void init() { |
| 61 |
setTitle("Proxy Vole Tester"); |
| 62 |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 63 |
|
| 64 |
JPanel p = new JPanel(); |
| 65 |
|
| 66 |
p.add(new JLabel("Mode:")); |
| 67 |
|
| 68 |
this.modes = new JComboBox<ProxySearch.Strategy>(ProxySearch.Strategy.values()); |
| 69 |
p.add(this.modes); |
| 70 |
|
| 71 |
p.add(new JLabel("URL:")); |
| 72 |
this.urlField = new JTextField(30); |
| 73 |
this.urlField.setText("http://www.google.com/"); |
| 74 |
p.add(this.urlField); |
| 75 |
|
| 76 |
this.testButton = new JButton("Test"); |
| 77 |
this.testButton.addActionListener(new ActionListener() { |
| 78 |
public void actionPerformed(ActionEvent arg0) { |
| 79 |
testUrl(); |
| 80 |
} |
| 81 |
}); |
| 82 |
p.add(this.testButton); |
| 83 |
|
| 84 |
this.logArea = new JTextArea(5, 50); |
| 85 |
JPanel contenPane = new JPanel(new BorderLayout()); |
| 86 |
contenPane.add(p, BorderLayout.NORTH); |
| 87 |
contenPane.add(new JScrollPane(this.logArea), BorderLayout.CENTER); |
| 88 |
setContentPane(contenPane); |
| 89 |
|
| 90 |
pack(); |
| 91 |
setLocationRelativeTo(null); |
| 92 |
installLogger(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private void installLogger() { |
| 100 |
Logger.setBackend(new Logger.LogBackEnd() { |
| 101 |
public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) { |
| 102 |
ProxyTester.this.logArea.append(loglevel + "\t" + MessageFormat.format(msg, params) + "\n"); |
| 103 |
} |
| 104 |
|
| 105 |
public boolean isLogginEnabled(LogLevel logLevel) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
}); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected void testUrl() { |
| 116 |
try { |
| 117 |
if (this.urlField.getText().trim().length() == 0) { |
| 118 |
ProxyTester.this.logArea.append("ERROR\tPlease enter an URL first.\n"); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
this.logArea.setText(""); |
| 123 |
|
| 124 |
Strategy pss = (Strategy) this.modes.getSelectedItem(); |
| 125 |
ProxySearch ps = new ProxySearch(); |
| 126 |
ps.addStrategy(pss); |
| 127 |
ProxySelector psel = ps.getProxySelector(); |
| 128 |
if (psel == null) { |
| 129 |
ProxyTester.this.logArea.append("ERROR\tNo proxy settings available for this mode.\n"); |
| 130 |
return; |
| 131 |
} |
| 132 |
ProxySelector.setDefault(psel); |
| 133 |
|
| 134 |
URL url = new URL(this.urlField.getText().trim()); |
| 135 |
List<Proxy> result = psel.select(url.toURI()); |
| 136 |
if (result == null || result.size() == 0) { |
| 137 |
ProxyTester.this.logArea.append("INFO\tNo proxy found for this url.\n"); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
ProxyTester.this.logArea.append("INFO\tProxy Settings found using " + pss + " strategy.\n"); |
| 142 |
ProxyTester.this.logArea.append("INFO\tProxy used for URL is: " + result.get(0) + "\n"); |
| 143 |
|
| 144 |
} catch (Exception e) { |
| 145 |
JOptionPane.showMessageDialog(this, "Error:" + e.getMessage(), "Error checking URL.", |
| 146 |
JOptionPane.ERROR_MESSAGE); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static void main(String[] args) { |
| 159 |
SwingUtilities.invokeLater(new Runnable() { |
| 160 |
public void run() { |
| 161 |
setLookAndFeel(); |
| 162 |
|
| 163 |
ProxyTester mainFrame = new ProxyTester(); |
| 164 |
mainFrame.setVisible(true); |
| 165 |
} |
| 166 |
|
| 167 |
}); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
private static void setLookAndFeel() { |
| 175 |
try { |
| 176 |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 177 |
} catch (Exception e) { |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
} |