summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/test/ProxyTester.java
blob: c01c7bb23d885bd197a8f7b80bad115a41f1264e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.btr.proxy.test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URL;
import java.text.MessageFormat;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import com.btr.proxy.search.ProxySearch;
import com.btr.proxy.search.ProxySearch.Strategy;
import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;

/*****************************************************************************
 * Small test application that allows you to select a proxy search strategy
 * and then validate URLs against it. 
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class ProxyTester extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private JComboBox modes;
	private JButton testButton;
	private JTextField urlField;

	private JTextArea logArea;

	/*************************************************************************
	 * Constructor
	 ************************************************************************/
	
	public ProxyTester() {
		super();
		init();
	}
	
	/*************************************************************************
	 * Initializes the GUI.
	 ************************************************************************/
	
	private void init() {
		setTitle("Proxy Vole Tester");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel p = new JPanel();

		p.add(new JLabel("Mode:"));

		this.modes = new JComboBox(ProxySearch.Strategy.values());
		p.add(this.modes);

		p.add(new JLabel("URL:"));
		this.urlField = new JTextField(30); 
		this.urlField.setText("http://code.google.com/p/proxy-vole/");
		p.add(this.urlField);
		
		this.testButton = new JButton("Test");
		this.testButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				testUrl();
			}
		});
		p.add(this.testButton);

		this.logArea = new JTextArea(5, 50);
		JPanel contenPane = new JPanel(new BorderLayout());
		contenPane.add(p, BorderLayout.NORTH);
		contenPane.add(new JScrollPane(this.logArea), BorderLayout.CENTER);
		setContentPane(contenPane);

		pack();
		setLocationRelativeTo(null);
		installLogger();
	}

	/*************************************************************************
	 * Install the framework logger.
	 ************************************************************************/
	
	private void installLogger() {
		Logger.setBackend(new Logger.LogBackEnd() {
			public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) {
				ProxyTester.this.logArea.append(loglevel+"\t"+MessageFormat.format(msg, params)+"\n");
			}
			public boolean isLogginEnabled(LogLevel logLevel) {
				return true;
			}
		});
	}

	/*************************************************************************
	 * Test the given URL with the given Proxy Search.
	 ************************************************************************/
	
	protected void testUrl() {
		try {
			if (this.urlField.getText().trim().length() == 0) {
				JOptionPane.showMessageDialog(this, "Please enter an URL first.");
				return;
			}
			
			this.logArea.setText("");

			Strategy pss = (Strategy) this.modes.getSelectedItem();
			ProxySearch ps = new ProxySearch();
			ps.addStrategy(pss);
			ProxySelector psel = ps.getProxySelector();
			if (psel == null) {
				JOptionPane.showMessageDialog(this, "No proxy settings available for this mode.");
				return;
			}
			ProxySelector.setDefault(psel);
			
			URL url = new URL(this.urlField.getText().trim()); 
			List<Proxy> result = psel.select(url.toURI());
			if (result == null || result.size() == 0) {
				JOptionPane.showMessageDialog(this, "No proxy found for this url.");
				return;
			}
			
			JOptionPane.showMessageDialog(this, 
					"Proxy Settings found using "+pss+" strategy.\n" +
					"Proxy used for URL is: "+result.get(0));
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Error:"+e.getMessage(), "Error checking URL.", JOptionPane.ERROR_MESSAGE);
		}
		
	}

	/*************************************************************************
	 * Main entry point for the application.
	 * @param args command line arguments.
	 ************************************************************************/
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				setLookAndFeel();
				
				ProxyTester mainFrame = new ProxyTester();
				mainFrame.setVisible(true);
			}

		});
	}

	/*************************************************************************
	 * Change the L&F to the system default.
	 ************************************************************************/
	
	private static void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// Use default
		}
	}
}