001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/contrib/org/apache/commons/httpclient/contrib/proxy/PluginProxyTestApplet.java $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient.contrib.proxy;
031:
032: import java.awt.BorderLayout;
033: import java.awt.Container;
034: import java.awt.FlowLayout;
035: import java.awt.GridLayout;
036: import java.awt.LayoutManager;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039: import java.net.URL;
040:
041: import javax.swing.JApplet;
042: import javax.swing.JButton;
043: import javax.swing.JLabel;
044: import javax.swing.JOptionPane;
045: import javax.swing.JPanel;
046: import javax.swing.JTextField;
047: import javax.swing.SwingUtilities;
048:
049: import org.apache.commons.httpclient.ProxyHost;
050:
051: /**
052: * <p>
053: * DISCLAIMER: HttpClient developers DO NOT actively support this component.
054: * The component is provided as a reference material, which may be inappropriate
055: * for use without additional customization.
056: * </p>
057: */
058:
059: public class PluginProxyTestApplet extends JApplet {
060:
061: private JTextField urlTextField = new JTextField();
062: private JPanel grid = null;
063: private JLabel hostLabel = null;
064: private JLabel portLabel = null;
065:
066: public PluginProxyTestApplet() {
067:
068: }
069:
070: public void init() {
071: Container content = getContentPane();
072: content.setLayout(new BorderLayout());
073:
074: // Proxy info table
075: grid = getPanel(new GridLayout(2, 3, 2, 2));
076: grid.add(getHeaderLabel("URL"));
077: grid.add(getHeaderLabel("Proxy Host"));
078: grid.add(getHeaderLabel("Proxy Port"));
079: grid.add(urlTextField);
080: hostLabel = getLabel("");
081: portLabel = getLabel("");
082: grid.add(hostLabel);
083: grid.add(portLabel);
084: grid.validate();
085: content.add(grid, BorderLayout.CENTER);
086:
087: // Button panel - SOUTH
088: JPanel buttonPanel = getPanel(new FlowLayout());
089: JButton button = new JButton("Detect Proxy");
090: button.addActionListener(new ActionListener() {
091: public void actionPerformed(ActionEvent e) {
092: SwingUtilities.invokeLater(new Runnable() {
093: public void run() {
094: detectProxy();
095: }
096: });
097: }
098: });
099: buttonPanel.add(button);
100: content.add(buttonPanel, BorderLayout.SOUTH);
101:
102: // version panel - NORTH
103: JPanel versionPanel = getPanel(new FlowLayout());
104: String javaVersion = System.getProperty("java.runtime.version");
105: JLabel versionLabel = getLabel("Java Version: " + javaVersion);
106: versionPanel.add(versionLabel);
107: content.add(versionPanel, BorderLayout.NORTH);
108: validate();
109:
110: super .setSize(400, 100);
111: }
112:
113: private JPanel getPanel(LayoutManager layout) {
114: JPanel result = new JPanel(layout);
115: return result;
116: }
117:
118: private JLabel getHeaderLabel(String text) {
119: JLabel result = new JLabel("<html><u><b>" + text
120: + "</b></u></html>");
121: result.setHorizontalAlignment(JLabel.CENTER);
122: return result;
123: }
124:
125: private JLabel getLabel(String text) {
126: JLabel result = new JLabel(text);
127: result.setHorizontalAlignment(JLabel.CENTER);
128: return result;
129: }
130:
131: private void detectProxy() {
132: String urlString = urlTextField.getText();
133: if (urlString == null || "".equals(urlString)) {
134: JOptionPane.showMessageDialog(super .getRootPane(),
135: "URL can't be empty", "Missing URL",
136: JOptionPane.ERROR_MESSAGE);
137: return;
138: }
139: if (!urlString.startsWith("http://")) {
140: urlString = "http://" + urlString;
141: }
142: try {
143: URL url = new URL(urlString);
144: ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
145: if (hostInfo != null) {
146: hostLabel.setText(hostInfo.getHostName());
147: portLabel.setText("" + hostInfo.getPort());
148: } else {
149: hostLabel.setText("none");
150: portLabel.setText("none");
151: }
152: grid.validate();
153: } catch (ProxyDetectionException e) {
154: JOptionPane.showMessageDialog(getRootPane(),
155: e.getMessage(), "Proxy Detection Failed",
156: JOptionPane.ERROR_MESSAGE);
157: e.printStackTrace();
158: } catch (Exception e) {
159: JOptionPane.showMessageDialog(getRootPane(),
160: e.getMessage(), "Unexpected Exception",
161: JOptionPane.ERROR_MESSAGE);
162: e.printStackTrace();
163: }
164: }
165:
166: public String getProxyHost(String urlString) {
167: String result = urlString;
168: try {
169: URL url = new URL(urlString);
170: ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
171: if (hostInfo != null) {
172: result = hostInfo.getHostName();
173: }
174: } catch (Exception e) {
175: e.printStackTrace();
176: }
177: return result;
178: }
179:
180: public int getProxyPort(String urlString) {
181: int result = 80;
182: try {
183: URL url = new URL(urlString);
184: ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
185: if (hostInfo != null) {
186: result = hostInfo.getPort();
187: }
188: } catch (Exception e) {
189: e.printStackTrace();
190: }
191: return result;
192: }
193:
194: }
|