|                                                                                                              1
 30  package org.apache.commons.httpclient.contrib.proxy;
 31
 32  import java.awt.BorderLayout
  ; 33  import java.awt.Container
  ; 34  import java.awt.FlowLayout
  ; 35  import java.awt.GridLayout
  ; 36  import java.awt.LayoutManager
  ; 37  import java.awt.event.ActionEvent
  ; 38  import java.awt.event.ActionListener
  ; 39  import java.net.URL
  ; 40
 41  import javax.swing.JApplet
  ; 42  import javax.swing.JButton
  ; 43  import javax.swing.JLabel
  ; 44  import javax.swing.JOptionPane
  ; 45  import javax.swing.JPanel
  ; 46  import javax.swing.JTextField
  ; 47  import javax.swing.SwingUtilities
  ; 48
 49  import org.apache.commons.httpclient.ProxyHost;
 50
 51
 58
 59  public class PluginProxyTestApplet extends JApplet
  { 60
 61
 62      private JTextField
  urlTextField = new JTextField  (); 63      private JPanel
  grid = null; 64      private JLabel
  hostLabel = null; 65      private JLabel
  portLabel = null; 66
 67
 68      public PluginProxyTestApplet() {
 69
 70      }
 71
 72      public void init() {
 73          Container
  content = getContentPane(); 74          content.setLayout(new BorderLayout
  ()); 75
 76                  grid = getPanel(new GridLayout
  (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                  JPanel
  buttonPanel = getPanel(new FlowLayout  ()); 91          JButton
  button = new JButton  ("Detect Proxy"); 92          button.addActionListener(new ActionListener
  () { 93              public void actionPerformed(ActionEvent
  e) { 94                  SwingUtilities.invokeLater(new Runnable
  () { 95                      public void run() {
 96                          detectProxy();
 97                      }
 98                  });
 99              }
 100         });
 101         buttonPanel.add(button);
 102         content.add(buttonPanel, BorderLayout.SOUTH);
 103
 104                 JPanel
  versionPanel = getPanel(new FlowLayout  ()); 106         String
  javaVersion = System.getProperty("java.runtime.version"); 107         JLabel
  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
  getPanel(LayoutManager  layout) { 116         JPanel
  result = new JPanel  (layout); 117         return result;
 118     }
 119
 120     private JLabel
  getHeaderLabel(String  text) { 121         JLabel
  result = new JLabel  ("<html><u><b>" + text + "</b></u></html>"); 122         result.setHorizontalAlignment(JLabel.CENTER);
 123         return result;
 124     }
 125
 126     private JLabel
  getLabel(String  text) { 127         JLabel
  result = new JLabel  (text); 128         result.setHorizontalAlignment(JLabel.CENTER);
 129         return result;
 130     }
 131
 132     private void detectProxy() {
 133         String
  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
  url = new URL  (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
  e) { 162             JOptionPane.showMessageDialog(getRootPane(),
 163                     e.getMessage() ,
 164                     "Unexpected Exception",
 165                     JOptionPane.ERROR_MESSAGE);
 166             e.printStackTrace();
 167         }
 168     }
 169
 170     public String
  getProxyHost(String  urlString) { 171         String
  result = urlString; 172         try {
 173             URL
  url = new URL  (urlString); 174             ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
 175             if (hostInfo != null) {
 176                 result = hostInfo.getHostName();
 177             }
 178         } catch (Exception
  e) { 179             e.printStackTrace();
 180         }
 181         return result;
 182     }
 183
 184     public int getProxyPort(String
  urlString) { 185         int result = 80;
 186         try {
 187             URL
  url = new URL  (urlString); 188             ProxyHost hostInfo = PluginProxyUtil.detectProxy(url);
 189             if (hostInfo != null) {
 190                 result = hostInfo.getPort();
 191             }
 192         } catch (Exception
  e) { 193             e.printStackTrace();
 194         }
 195         return result;
 196     }
 197
 198 }
 199
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |