|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 Tasktop Technologies and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Tasktop Technologies - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylyn.tasks.ui; |
| 13 |
|
| 14 |
import java.net.MalformedURLException; |
| 15 |
import java.net.URL; |
| 16 |
import java.util.Map; |
| 17 |
import java.util.Set; |
| 18 |
|
| 19 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 20 |
import org.eclipse.core.runtime.IStatus; |
| 21 |
import org.eclipse.core.runtime.Platform; |
| 22 |
import org.eclipse.core.runtime.Status; |
| 23 |
import org.eclipse.core.runtime.jobs.Job; |
| 24 |
import org.eclipse.equinox.security.storage.EncodingUtils; |
| 25 |
import org.eclipse.equinox.security.storage.ISecurePreferences; |
| 26 |
import org.eclipse.equinox.security.storage.SecurePreferencesFactory; |
| 27 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
| 28 |
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; |
| 29 |
import org.eclipse.mylyn.internal.tasks.ui.Messages; |
| 30 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
| 31 |
import org.eclipse.mylyn.tasks.core.TaskRepository; |
| 32 |
import org.eclipse.osgi.util.NLS; |
| 33 |
|
| 34 |
public class MigrateToSecureStorageJob extends Job { |
| 35 |
|
| 36 |
private final String kind; |
| 37 |
|
| 38 |
public MigrateToSecureStorageJob(String kind) { |
| 39 |
super(Messages.MigrateToSecureStorageJob_title); |
| 40 |
this.kind = kind; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected IStatus run(IProgressMonitor monitor) { |
| 45 |
Set<TaskRepository> repos = TasksUiPlugin.getRepositoryManager().getRepositories(kind); |
| 46 |
if (repos != null) { |
| 47 |
for (TaskRepository repo : repos) { |
| 48 |
migrateToSecureStorage(repo); |
| 49 |
} |
| 50 |
} |
| 51 |
return Status.OK_STATUS; |
| 52 |
} |
| 53 |
|
| 54 |
@SuppressWarnings({ "deprecation", "unchecked" }) |
| 55 |
public static boolean migrateToSecureStorage(TaskRepository repository) { |
| 56 |
if (repository.getProperty(ITasksCoreConstants.PROPERTY_USE_SECURE_STORAGE) == null |
| 57 |
&& !repository.getUrl().equals("local")) { //$NON-NLS-1$ |
| 58 |
try { |
| 59 |
Map<String, String> map = Platform.getAuthorizationInfo(new URL(repository.getUrl()), "", "Basic");//$NON-NLS-1$ //$NON-NLS-2$ |
| 60 |
if (map != null) { |
| 61 |
ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault().node( |
| 62 |
ITasksCoreConstants.ID_PLUGIN); |
| 63 |
securePreferences = securePreferences.node(EncodingUtils.encodeSlashes(repository.getUrl())); |
| 64 |
for (String key : map.keySet()) { |
| 65 |
String value = map.get(key); |
| 66 |
if (value != null) { |
| 67 |
securePreferences.put(key, value, key.endsWith(".password")); //$NON-NLS-1$ |
| 68 |
} |
| 69 |
} |
| 70 |
try { |
| 71 |
Platform.flushAuthorizationInfo(new URL(repository.getUrl()), "", "Basic"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 72 |
} catch (MalformedURLException ex) { |
| 73 |
Platform.flushAuthorizationInfo( |
| 74 |
new URL("http://eclipse.org/mylyn"), repository.getUrl(), "Basic"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 75 |
} |
| 76 |
} |
| 77 |
repository.setProperty(ITasksCoreConstants.PROPERTY_USE_SECURE_STORAGE, "true"); //$NON-NLS-1$ |
| 78 |
return true; |
| 79 |
} catch (Exception e) { |
| 80 |
StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, NLS.bind( |
| 81 |
Messages.MigrateToSecureStorageJob_failed, repository.getUrl()), e)); |
| 82 |
} |
| 83 |
} |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |