KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > contrib > proxy > PluginProxyTestApplet


1 /*
2  * $HeadURL: https://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/proxy/PluginProxyTestApplet.java $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  *
6  * ====================================================================
7  *
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements. See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation. For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */

30 package org.apache.commons.httpclient.contrib.proxy;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Container JavaDoc;
34 import java.awt.FlowLayout JavaDoc;
35 import java.awt.GridLayout JavaDoc;
36 import java.awt.LayoutManager JavaDoc;
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.ActionListener JavaDoc;
39 import java.net.URL JavaDoc;
40
41 import javax.swing.JApplet JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JOptionPane JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47 import javax.swing.SwingUtilities JavaDoc;
48
49 import org.apache.commons.httpclient.ProxyHost;
50
51 /**
52  * <p>
53  * DISCLAIMER: HttpClient developers DO NOT actively support this component.
54  * The component is provided as a reference material, which may be inappropriate
55  * for use without additional customization.
56  * </p>
57  */

58
59 public class PluginProxyTestApplet extends JApplet JavaDoc {
60
61     
62     private JTextField JavaDoc urlTextField = new JTextField JavaDoc();
63     private JPanel JavaDoc grid = null;
64     private JLabel JavaDoc hostLabel = null;
65     private JLabel JavaDoc portLabel = null;
66     
67     
68     public PluginProxyTestApplet() {
69         
70     }
71     
72     public void init() {
73         Container JavaDoc content = getContentPane();
74         content.setLayout(new BorderLayout JavaDoc());
75         
76         // Proxy info table
77
grid = getPanel(new GridLayout JavaDoc(2,3,2,2));
78         grid.add(getHeaderLabel("URL"));
79         grid.add(getHeaderLabel("Proxy Host"));
80         grid.add(getHeaderLabel("Proxy Port"));
81         grid.add(urlTextField);
82         hostLabel = getLabel("");
83         portLabel = getLabel("");
84         grid.add(hostLabel);
85         grid.add(portLabel);
86         grid.validate();
87         content.add(grid, BorderLayout.CENTER);
88         
89         // Button panel - SOUTH
90
JPanel JavaDoc buttonPanel = getPanel(new FlowLayout JavaDoc());
91         JButton JavaDoc button = new JButton JavaDoc("Detect Proxy");
92         button.addActionListener(new ActionListener JavaDoc() {
93             public void actionPerformed(ActionEvent JavaDoc e) {
94                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
95                     public void run() {
96                         detectProxy();
97                     }
98                 });
99             }
100         });
101         buttonPanel.add(button);
102         content.add(buttonPanel, BorderLayout.SOUTH);
103         
104         // version panel - NORTH
105
JPanel JavaDoc versionPanel = getPanel(new FlowLayout JavaDoc());
106         String JavaDoc javaVersion = System.getProperty("java.runtime.version");
107         JLabel JavaDoc versionLabel = getLabel("Java Version: "+javaVersion);
108         versionPanel.add(versionLabel);
109         content.add(versionPanel, BorderLayout.NORTH);
110         validate();
111         
112         super.setSize(400,100);
113     }
114     
115     private JPanel JavaDoc getPanel(LayoutManager JavaDoc layout) {
116         JPanel JavaDoc result = new JPanel JavaDoc(layout);
117         return result;
118     }
119     
120     private JLabel JavaDoc getHeaderLabel(String JavaDoc text) {
121         JLabel JavaDoc result = new JLabel JavaDoc("<html><u><b>" + text + "</b></u></html>");
122         result.setHorizontalAlignment(JLabel.CENTER);
123         return result;
124     }
125     
126     private JLabel JavaDoc getLabel(String JavaDoc text) {
127         JLabel JavaDoc result = new JLabel JavaDoc(text);
128         result.setHorizontalAlignment(JLabel.CENTER);
129         return result;
130     }
131     
132     private void detectProxy() {
133         String JavaDoc urlString = urlTextField.getText();
134         if (urlString == null || "".equals(urlString)) {
135             JOptionPane.showMessageDialog(super.getRootPane(),
136                                           "URL can't be empty",
137                                           "Missing URL",
138                                           JOptionPane.ERROR_MESSAGE);
139             return;
140         }
141         if (!urlString.startsWith("http://")) {
142             urlString = "http://" + urlString;
143         }
144         try {
145             URL JavaDoc url = new URL JavaDoc(urlString);
146             ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
147             if (hostInfo != null) {
148                 hostLabel.setText(hostInfo.getHostName());
149                 portLabel.setText(""+hostInfo.getPort());
150             } else {
151                 hostLabel.setText("none");
152                 portLabel.setText("none");
153             }
154             grid.validate();
155         } catch (ProxyDetectionException e) {
156             JOptionPane.showMessageDialog(getRootPane(),
157                                           e.getMessage() ,
158                                           "Proxy Detection Failed",
159                                           JOptionPane.ERROR_MESSAGE);
160             e.printStackTrace();
161         } catch (Exception JavaDoc e) {
162             JOptionPane.showMessageDialog(getRootPane(),
163                     e.getMessage() ,
164                     "Unexpected Exception",
165                     JOptionPane.ERROR_MESSAGE);
166             e.printStackTrace();
167         }
168     }
169     
170     public String JavaDoc getProxyHost(String JavaDoc urlString) {
171         String JavaDoc result = urlString;
172         try {
173             URL JavaDoc url = new URL JavaDoc(urlString);
174             ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
175             if (hostInfo != null) {
176                 result = hostInfo.getHostName();
177             }
178         } catch (Exception JavaDoc e) {
179             e.printStackTrace();
180         }
181         return result;
182     }
183
184     public int getProxyPort(String JavaDoc urlString) {
185         int result = 80;
186         try {
187             URL JavaDoc url = new URL JavaDoc(urlString);
188             ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
189             if (hostInfo != null) {
190                 result = hostInfo.getPort();
191             }
192         } catch (Exception JavaDoc e) {
193             e.printStackTrace();
194         }
195         return result;
196     }
197
198 }
199
Popular Tags