Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/ui/ProxyTester.java

raw symbols SCIP
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 * Small test application that allows you to select a proxy search strategy and
31 * then validate URLs against it.
32 *
33 * @author Markus Bernhardt, Copyright 2016
34 * @author Bernd Rosstauscher, Copyright 2009
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 * Constructor
49 ************************************************************************/
50
51 public ProxyTester() {
52 super();
53 init();
54 }
55
56 /*************************************************************************
57 * Initializes the GUI.
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 * Install the framework logger.
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 * Test the given URL with the given Proxy Search.
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 * Main entry point for the application.
153 *
154 * @param args
155 * command line arguments.
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 * Change the L&F to the system default.
172 ************************************************************************/
173
174 private static void setLookAndFeel() {
175 try {
176 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
177 } catch (Exception e) {
178 // Use default
179 }
180 }
181 }