|
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.Set; |
| 17 |
|
| 18 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 19 |
import org.eclipse.core.runtime.IStatus; |
| 20 |
import org.eclipse.core.runtime.Platform; |
| 21 |
import org.eclipse.core.runtime.Status; |
| 22 |
import org.eclipse.core.runtime.jobs.Job; |
| 23 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
| 24 |
import org.eclipse.mylyn.commons.net.AuthenticationCredentials; |
| 25 |
import org.eclipse.mylyn.commons.net.AuthenticationType; |
| 26 |
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; |
| 27 |
import org.eclipse.mylyn.internal.tasks.ui.Messages; |
| 28 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
| 29 |
import org.eclipse.mylyn.tasks.core.TaskRepository; |
| 30 |
import org.eclipse.osgi.util.NLS; |
| 31 |
|
| 32 |
public class MigrateToSecureStorageJob extends Job { |
| 33 |
|
| 34 |
private final String kind; |
| 35 |
|
| 36 |
public MigrateToSecureStorageJob(String kind) { |
| 37 |
super(Messages.MigrateToSecureStorageJob_title); |
| 38 |
this.kind = kind; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected IStatus run(IProgressMonitor monitor) { |
| 43 |
Set<TaskRepository> repos = TasksUiPlugin.getRepositoryManager().getRepositories(kind); |
| 44 |
if (repos != null) { |
| 45 |
for (TaskRepository repo : repos) { |
| 46 |
migrateToSecureStorage(repo); |
| 47 |
} |
| 48 |
} |
| 49 |
return Status.OK_STATUS; |
| 50 |
} |
| 51 |
|
| 52 |
@SuppressWarnings({ "deprecation" }) |
| 53 |
public static boolean migrateToSecureStorage(TaskRepository repository) { |
| 54 |
if (repository.getProperty(ITasksCoreConstants.PROPERTY_USE_SECURE_STORAGE) == null |
| 55 |
&& !repository.getUrl().equals("local")) { //$NON-NLS-1$ |
| 56 |
try { |
| 57 |
AuthenticationCredentials creds = repository.getCredentials(AuthenticationType.REPOSITORY); |
| 58 |
boolean savePassword = repository.getSavePassword(AuthenticationType.REPOSITORY); |
| 59 |
|
| 60 |
repository.setProperty(ITasksCoreConstants.PROPERTY_USE_SECURE_STORAGE, "true"); //$NON-NLS-1$ |
| 61 |
|
| 62 |
if (creds != null) { |
| 63 |
try { |
| 64 |
Platform.flushAuthorizationInfo(new URL(repository.getUrl()), "", "Basic"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 65 |
} catch (MalformedURLException ex) { |
| 66 |
Platform.flushAuthorizationInfo( |
| 67 |
new URL("http://eclipse.org/mylyn"), repository.getUrl(), "Basic"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 68 |
} |
| 69 |
|
| 70 |
repository.setCredentials(AuthenticationType.REPOSITORY, creds, savePassword); |
| 71 |
} |
| 72 |
|
| 73 |
return true; |
| 74 |
} catch (Exception e) { |
| 75 |
StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, NLS.bind( |
| 76 |
"Could not migrate credentials to secure storage for {0}", repository.getUrl()), e)); |
| 77 |
} |
| 78 |
} |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |