1 21 22 package com.izforge.izpack.installer; 23 24 import java.awt.BorderLayout ; 25 import java.awt.Component ; 26 import java.awt.Cursor ; 27 import java.awt.GridLayout ; 28 import java.awt.Toolkit ; 29 import java.io.InputStream ; 30 import java.net.Authenticator ; 31 import java.net.ConnectException ; 32 import java.net.InetAddress ; 33 import java.net.PasswordAuthentication ; 34 import java.net.URL ; 35 import java.net.URLConnection ; 36 import java.util.Locale ; 37 38 import javax.swing.JDialog ; 39 import javax.swing.JLabel ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 import javax.swing.JPasswordField ; 43 import javax.swing.JTextField ; 44 import javax.swing.UIManager ; 45 import javax.swing.JProgressBar ; 46 47 54 public class WebAccessor 55 { 56 57 private Thread openerThread = null; 58 59 private InputStream iStream = null; 60 61 private Exception exception = null; 62 63 private Object soloCancelOption = null; 64 65 private Component parent = null; 66 67 private JDialog dialog = null; 68 69 private boolean tryProxy = false; 70 71 private JPanel passwordPanel = null; 72 73 private JLabel promptLabel; 74 75 private JTextField nameField; 76 77 private JPasswordField passField; 78 79 private JPanel proxyPanel = null; 80 81 private JLabel errorLabel; 82 83 private JTextField hostField; 84 85 private JTextField portField; 86 87 92 public WebAccessor() 93 { 94 throw new UnsupportedOperationException (); 96 } 97 98 104 public WebAccessor(Component parent) 105 { 106 this.parent = parent; 107 Locale l = null; 108 if (parent != null) parent.getLocale(); 109 soloCancelOption = UIManager.get("OptionPane.cancelButtonText", l); Authenticator.setDefault(new MyDialogAuthenticator()); 112 } 113 114 120 public InputStream openInputStream(URL url) 121 { 122 Object [] options = { soloCancelOption}; 124 125 JProgressBar progressBar = new JProgressBar (1, 100); 126 progressBar.setIndeterminate(true); 127 Object [] contents = { "Connecting to the Internet", progressBar }; 128 JOptionPane pane = new JOptionPane (contents, 129 JOptionPane.INFORMATION_MESSAGE, 130 JOptionPane.DEFAULT_OPTION, null, options, 131 options[0]); 132 dialog = pane.createDialog(parent, "Accessing Install Files"); 133 pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 134 135 Object value = null; 136 OPEN_URL: while (true) 137 { 138 startOpening(url); dialog.setVisible(true); 141 value = pane.getValue(); 142 143 if (value == null || value == soloCancelOption) 145 { 146 try 147 { 148 openerThread.interrupt(); } 150 catch (Exception e) 151 {} 152 iStream = null; break; 154 } 155 156 else if (value == JOptionPane.UNINITIALIZED_VALUE) 158 { 159 if (iStream != null) break; 161 162 164 if (!tryProxy) break; 166 167 JPanel panel = getProxyPanel(); 170 errorLabel.setText("Unable to connect: " + exception.getMessage()); 171 while (true) 172 { 173 int result = JOptionPane.showConfirmDialog(parent, panel, 174 "Proxy Configuration", JOptionPane.OK_CANCEL_OPTION, 175 JOptionPane.QUESTION_MESSAGE); 176 if (result != JOptionPane.OK_OPTION) break OPEN_URL; 178 179 String host = null; 180 String port = null; 181 182 try 183 { 184 InetAddress addr = InetAddress.getByName(hostField.getText()); 185 host = addr.getHostName(); 186 } 187 catch (Exception x) 188 { 189 errorLabel.setText("Unable to resolve Host"); 190 Toolkit.getDefaultToolkit().beep(); 191 } 192 193 try 194 { 195 if (host != null) port = Integer.valueOf(portField.getText()).toString(); 196 } 197 catch (NumberFormatException x) 198 { 199 errorLabel.setText("Invalid Port"); 200 Toolkit.getDefaultToolkit().beep(); 201 } 202 203 if (host != null && port != null) 204 { 205 System.getProperties().put("proxySet", "true"); 208 System.getProperties().put("proxyHost", host); 209 System.getProperties().put("proxyPort", port); 210 break; 211 } 212 } 213 } 214 } 215 return iStream; 216 } 217 218 private void startOpening(final URL url) 219 { 220 openerThread = new Thread () { 221 222 public void run() 223 { 224 iStream = null; 225 try 226 { 227 tryProxy = false; 228 URLConnection connection = url.openConnection(); 229 iStream = connection.getInputStream(); 232 } 233 catch (ConnectException x) 234 { tryProxy = true; 236 exception = x; 237 238 } 239 catch (Exception x) 240 { 241 exception = x; 245 246 } 247 finally 248 { 249 if (dialog != null) 254 { 255 Thread.yield(); 256 dialog.setVisible(false); 257 } 258 } 259 } 260 }; 261 openerThread.start(); 262 } 263 264 267 private JPanel getProxyPanel() 268 { 269 if (proxyPanel == null) 270 { 271 proxyPanel = new JPanel (new BorderLayout (5, 5)); 272 273 errorLabel = new JLabel (); 274 275 JPanel fields = new JPanel (new GridLayout (2, 2)); 276 String h = (String ) System.getProperties().get("proxyHost"); 277 String p = (String ) System.getProperties().get("proxyPort"); 278 hostField = new JTextField (h != null ? h : ""); 279 portField = new JTextField (p != null ? p : ""); 280 JLabel host = new JLabel ("Host: "); JLabel port = new JLabel ("Port: "); fields.add(host); 283 fields.add(hostField); 284 fields.add(port); 285 fields.add(portField); 286 287 JLabel exampleLabel = new JLabel ("e.g. host=\"gatekeeper.example.com\" port=\"80\""); 288 289 proxyPanel.add(errorLabel, BorderLayout.NORTH); 290 proxyPanel.add(fields, BorderLayout.CENTER); 291 proxyPanel.add(exampleLabel, BorderLayout.SOUTH); 292 } 293 proxyPanel.validate(); 294 295 return proxyPanel; 296 } 297 298 private JPanel getPasswordPanel() 299 { 300 if (passwordPanel == null) 301 { 302 passwordPanel = new JPanel (new BorderLayout (5, 5)); 303 304 promptLabel = new JLabel (); 305 306 JPanel fields = new JPanel (new GridLayout (2, 2)); 307 nameField = new JTextField (); 308 passField = new JPasswordField (); 309 JLabel name = new JLabel ("Name: "); JLabel pass = new JLabel ("Password: "); fields.add(name); 312 fields.add(nameField); 313 fields.add(pass); 314 fields.add(passField); 315 316 passwordPanel.add(promptLabel, BorderLayout.NORTH); 317 passwordPanel.add(fields, BorderLayout.CENTER); 318 } 319 passField.setText(""); 320 321 return passwordPanel; 322 } 323 324 327 private class MyDialogAuthenticator extends Authenticator 328 { 329 330 public PasswordAuthentication getPasswordAuthentication() 331 { 332 JPanel p = getPasswordPanel(); 334 String prompt = getRequestingPrompt(); 335 InetAddress addr = getRequestingSite(); 336 if (addr != null) prompt += " (" + addr.getHostName() + ")"; 337 promptLabel.setText(prompt); 338 int result = JOptionPane.showConfirmDialog(parent, p, "Enter Password", 339 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 340 if (result != JOptionPane.OK_OPTION) return null; 341 342 return new PasswordAuthentication (nameField.getText(), passField.getPassword()); 343 } 344 } 345 } 346 | Popular Tags |