001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.internal.ui;
018:
019: import java.io.UnsupportedEncodingException;
020: import java.net.Authenticator;
021: import java.net.PasswordAuthentication;
022: import java.net.URLEncoder;
023:
024: import net.refractions.udig.ui.PlatformGIS;
025:
026: import org.eclipse.jface.window.Window;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.ui.PlatformUI;
029: import org.osgi.service.prefs.Preferences;
030:
031: public class UDIGAuthenticator extends Authenticator {
032: private static final String NAME = "NAME"; //$NON-NLS-1$
033: private static final String PASSWORD = "PASSWORD"; //$NON-NLS-1$
034: private static final String URL_AUTHENTICATION = "URL_AUTHENTICATION"; //$NON-NLS-1$
035: private String username;
036: private String password;
037: private boolean storePassword;
038:
039: protected PasswordAuthentication getPasswordAuthentication() {
040: final String[] name = new String[1];
041: final String[] pass = new String[1];
042: name[0] = loadName();
043: pass[0] = loadPassword();
044:
045: if (name[0] == null && pass[0] == null) {
046: //TODO check if credentials have been previously entered and remembered
047: PlatformGIS.syncInDisplayThread(new Runnable() {
048: public void run() {
049: promptForPassword();
050: name[0] = username;
051: pass[0] = password;
052: }
053: });
054: }
055: if (name[0] == null && pass[0] == null) {
056: return null;
057: }
058: if (storePassword)
059: store(name[0], pass[0]);
060: return new PasswordAuthentication(name[0], pass[0]
061: .toCharArray());
062: }
063:
064: private void store(String name, String pass) {
065: try {
066: Preferences node = UiPlugin.getUserPreferences().node(
067: getNodeKey());
068: node.put(NAME, name);
069: node.put(PASSWORD, pass);
070: } catch (Exception e) {
071: UiPlugin.log("", e); //$NON-NLS-1$
072: }
073: }
074:
075: private String loadPassword() {
076: try {
077: Preferences node = UiPlugin.getUserPreferences().node(
078: getNodeKey());
079: String pass = node.get(PASSWORD, null);
080: if (pass == null)
081: return null;
082:
083: return pass;
084: } catch (Exception e) {
085: UiPlugin.log("", e); //$NON-NLS-1$
086: return null;
087: }
088: }
089:
090: private String getNodeKey() throws UnsupportedEncodingException {
091: return URL_AUTHENTICATION
092: + URLEncoder.encode(getRequestingURL().toString(),
093: "UTF-8"); //$NON-NLS-1$
094: }
095:
096: private String loadName() {
097: try {
098: Preferences node = UiPlugin.getUserPreferences().node(
099: getNodeKey());
100: return node.get(NAME, null);
101:
102: } catch (Exception e) {
103: UiPlugin.log("", e); //$NON-NLS-1$
104: return null;
105: }
106: }
107:
108: protected void promptForPassword() {
109:
110: Shell shell = PlatformUI.getWorkbench()
111: .getActiveWorkbenchWindow().getShell();
112: AuthenticationDialog dialog = new AuthenticationDialog(shell);
113: dialog.setBlockOnOpen(true);
114: int result = dialog.open();
115: if (result == Window.CANCEL) {
116: username = null;
117: password = null;
118: return;
119: }
120: username = dialog.getUsername();
121: if (username == null) {
122: username = ""; //$NON-NLS-1$
123: }
124: password = dialog.getPassword();
125: if (password == null) {
126: password = ""; //$NON-NLS-1$
127: }
128: storePassword = dialog.shouldRemember();
129: }
130: }
|