PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/build/distribution/IzPack/src/lib/com/izforge/izpack/installer/WebAccessor.java

https://bitbucket.org/jorgenio/gvsig
Java | 320 lines | 212 code | 38 blank | 70 comment | 34 complexity | 7bd4e96693cf23b1fa5f550e722dd0e6 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * $Id: WebAccessor.java 5819 2006-06-14 07:29:09Z cesar $
  3. * IzPack
  4. * Copyright (C) 2002 Johannes Lehtinen
  5. *
  6. * File : WebAccessor.java
  7. * Description : Prompt user for proxies and passwords
  8. * Author's email : mchenryc@acm.org
  9. * Author's Website : http://www.izforge.com
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. package com.izforge.izpack.installer;
  26. import java.awt.BorderLayout;
  27. import java.awt.Component;
  28. import java.awt.Cursor;
  29. import java.awt.GridLayout;
  30. import java.awt.Toolkit;
  31. import java.io.InputStream;
  32. import java.net.Authenticator;
  33. import java.net.ConnectException;
  34. import java.net.InetAddress;
  35. import java.net.PasswordAuthentication;
  36. import java.net.URL;
  37. import java.net.URLConnection;
  38. import java.util.Locale;
  39. import javax.swing.JDialog;
  40. import javax.swing.JLabel;
  41. import javax.swing.JOptionPane;
  42. import javax.swing.JPanel;
  43. import javax.swing.JPasswordField;
  44. import javax.swing.JTextField;
  45. import javax.swing.UIManager;
  46. /**
  47. * Dialogs for password authentication and firewall specification, when needed,
  48. * during web installation.
  49. *
  50. * @author Chadwick McHenry
  51. * @version 1.0
  52. */
  53. public class WebAccessor
  54. {
  55. private Thread openerThread = null;
  56. private InputStream iStream = null;
  57. private Exception exception = null;
  58. private Object soloCancelOption = null;
  59. private Component parent = null;
  60. private JDialog dialog = null;
  61. private boolean tryProxy = false;
  62. private JPanel passwordPanel = null;
  63. private JLabel promptLabel;
  64. private JTextField nameField;
  65. private JPasswordField passField;
  66. private JPanel proxyPanel = null;
  67. private JLabel errorLabel;
  68. private JTextField hostField;
  69. private JTextField portField;
  70. /**
  71. * Not yet Implemented: placeholder for headless installs.
  72. *
  73. * @throws UnsupportedOperationException
  74. */
  75. public WebAccessor()
  76. {
  77. // the class should probably be rearranged to do this.
  78. throw new UnsupportedOperationException();
  79. }
  80. /**
  81. * Create a WebAccessor that prompts for proxies and passwords using a
  82. * JDialog.
  83. *
  84. * @param parent determines the frame in which the dialog is displayed; if
  85. * the parentComponent has no Frame, a default Frame is used
  86. */
  87. public WebAccessor(Component parent)
  88. {
  89. this.parent = parent;
  90. Locale l = null;
  91. if (parent != null)
  92. parent.getLocale();
  93. soloCancelOption = UIManager.get("OptionPane.cancelButtonText",l);// TODO: i18n?
  94. Authenticator.setDefault(new MyDialogAuthenticator());
  95. }
  96. /**
  97. * Opens a URL connection and returns it's InputStream for the specified URL.
  98. *
  99. * @param url the url to open the stream to.
  100. * @return an input stream ready to read, or null on failure
  101. */
  102. public InputStream openInputStream(URL url)
  103. {
  104. // TODO: i18n everything
  105. Object[] options = {soloCancelOption};
  106. JOptionPane pane = new JOptionPane("Connecting to the Internet",
  107. JOptionPane.INFORMATION_MESSAGE,
  108. JOptionPane.DEFAULT_OPTION,
  109. null, options, options[0]);
  110. dialog = pane.createDialog(parent, "Accessing Install Files");
  111. pane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  112. Object value = null;
  113. OPEN_URL:
  114. while (true)
  115. {
  116. startOpening(url); // this starts a thread that may dismiss the dialog before user
  117. dialog.setVisible(true);
  118. value = pane.getValue();
  119. // dialog closed or canceled (by widget)
  120. if (value == null || value == soloCancelOption)
  121. {
  122. try {
  123. openerThread.interrupt();// stop the connection
  124. } catch (Exception e) {
  125. }
  126. iStream = null; // even if connection was made just after cancel
  127. break;
  128. }
  129. // dialog closed by thread so either a connection error or success!
  130. else if (value == JOptionPane.UNINITIALIZED_VALUE)
  131. {
  132. // success!
  133. if (iStream != null)
  134. break;
  135. //System.err.println(exception);
  136. // an exception we don't expect setting a proxy to fix
  137. if (! tryProxy)
  138. break;
  139. // else (exception != null)
  140. // show proxy dialog until valid values or cancel
  141. JPanel panel = getProxyPanel();
  142. errorLabel.setText("Unable to connect: " +exception.getMessage());
  143. while (true)
  144. {
  145. int result = JOptionPane.showConfirmDialog(parent, panel,
  146. "Proxy Configuration",
  147. JOptionPane.OK_CANCEL_OPTION,
  148. JOptionPane.QUESTION_MESSAGE);
  149. if (result != JOptionPane.OK_OPTION) // canceled
  150. break OPEN_URL;
  151. String host = null;
  152. String port = null;
  153. try {
  154. InetAddress addr = InetAddress.getByName(hostField.getText());
  155. host = addr.getHostName();
  156. } catch (Exception x) {
  157. errorLabel.setText("Unable to resolve Host");
  158. Toolkit.getDefaultToolkit().beep();
  159. }
  160. try {
  161. if (host != null)
  162. port = Integer.valueOf(portField.getText()).toString();
  163. } catch (NumberFormatException x) {
  164. errorLabel.setText("Invalid Port");
  165. Toolkit.getDefaultToolkit().beep();
  166. }
  167. if (host != null && port != null) {
  168. //System.err.println ("Setting http proxy: "+ host +":"+ port);
  169. System.getProperties().put ("proxySet", "true");
  170. System.getProperties().put ("proxyHost", host);
  171. System.getProperties().put ("proxyPort", port);
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. return iStream;
  178. }
  179. private void startOpening(final URL url)
  180. {
  181. openerThread = new Thread()
  182. {
  183. public void run() {
  184. iStream = null;
  185. try {
  186. tryProxy = false;
  187. URLConnection connection = url.openConnection();
  188. iStream = connection.getInputStream(); // just to make connection
  189. } catch (ConnectException x) { // could be an incorrect proxy
  190. tryProxy = true;
  191. exception = x;
  192. } catch (Exception x) {
  193. // Exceptions that get here are considered cancels or missing
  194. // pages, eg 401 if user finally cancels auth
  195. exception = x;
  196. } finally {
  197. // if dialog is in use, allow it to become visible /before/ closing
  198. // it, else on /fast/ connectinos, it may open later and hang!
  199. if (dialog != null)
  200. {
  201. Thread.yield();
  202. dialog.setVisible(false);
  203. }
  204. }
  205. }
  206. };
  207. openerThread.start();
  208. }
  209. /**
  210. * Only to be called after an initial error has indicated a connection problem
  211. */
  212. private JPanel getProxyPanel()
  213. {
  214. if (proxyPanel == null)
  215. {
  216. proxyPanel = new JPanel(new BorderLayout(5,5));
  217. errorLabel = new JLabel();
  218. JPanel fields = new JPanel(new GridLayout(2,2));
  219. String h = (String)System.getProperties().get ("proxyHost");
  220. String p = (String)System.getProperties().get ("proxyPort");
  221. hostField = new JTextField(h != null ? h : "");
  222. portField = new JTextField(p != null ? p : "");
  223. JLabel host = new JLabel("Host: "); // TODO: i18n
  224. JLabel port = new JLabel("Port: "); // TODO: i18n
  225. fields.add(host);
  226. fields.add(hostField);
  227. fields.add(port);
  228. fields.add(portField);
  229. JLabel exampleLabel =
  230. new JLabel("e.g. host=\"gatekeeper.example.com\" port=\"80\"");
  231. proxyPanel.add(errorLabel, BorderLayout.NORTH);
  232. proxyPanel.add(fields, BorderLayout.CENTER);
  233. proxyPanel.add(exampleLabel, BorderLayout.SOUTH);
  234. }
  235. proxyPanel.validate();
  236. return proxyPanel;
  237. }
  238. private JPanel getPasswordPanel()
  239. {
  240. if (passwordPanel == null)
  241. {
  242. passwordPanel = new JPanel(new BorderLayout(5,5));
  243. promptLabel = new JLabel();
  244. JPanel fields = new JPanel(new GridLayout(2,2));
  245. nameField = new JTextField();
  246. passField = new JPasswordField();
  247. JLabel name = new JLabel("Name: "); // TODO: i18n
  248. JLabel pass = new JLabel("Password: "); // TODO: i18n
  249. fields.add(name);
  250. fields.add(nameField);
  251. fields.add(pass);
  252. fields.add(passField);
  253. passwordPanel.add(promptLabel, BorderLayout.NORTH);
  254. passwordPanel.add(fields, BorderLayout.CENTER);
  255. }
  256. passField.setText("");
  257. return passwordPanel;
  258. }
  259. /**
  260. * Authenticates via dialog when needed.
  261. */
  262. private class MyDialogAuthenticator extends Authenticator
  263. {
  264. public PasswordAuthentication getPasswordAuthentication()
  265. {
  266. // TODO: i18n
  267. JPanel p = getPasswordPanel();
  268. String prompt = getRequestingPrompt();
  269. InetAddress addr = getRequestingSite();
  270. if (addr != null)
  271. prompt += " (" + addr.getHostName() + ")";
  272. promptLabel.setText(prompt);
  273. int result = JOptionPane.showConfirmDialog(parent, p, "Enter Password",
  274. JOptionPane.OK_CANCEL_OPTION,
  275. JOptionPane.QUESTION_MESSAGE);
  276. if (result != JOptionPane.OK_OPTION)
  277. return null;
  278. return new PasswordAuthentication(nameField.getText(),
  279. passField.getPassword());
  280. }
  281. };
  282. }