1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/*******************************************************************************
* Copyright (c) 2014 Tasktop Technologies and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tasktop Technologies - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.tasks.core.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.repositories.core.ILocationService;
import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore;
import org.eclipse.mylyn.internal.commons.repositories.core.LocationService;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
/**
* Reads properties from the deprecated Eclipse keyring and writes them to the {@link ICredentialsStore} provided by the
* {@link ILocationService}.
*
* @author Sam Davis
*/
public abstract class KeyringMigrator<T> {
protected final ILocationService service = LocationService.getDefault();
protected final String authRealm;
protected final String authScheme;
public KeyringMigrator(String authRealm, String authScheme) {
this.authRealm = authRealm;
this.authScheme = authScheme;
}
/**
* Migrate credentials for the given locations
*/
public void migrateCredentials(Collection<T> locations) {
for (T location : locations) {
migrateCredentials(location);
}
}
protected void migrateCredentials(T location) {
try {
Map<String, String> properties = getAuthorizationInfo(getUrl(location));
putProperties(properties, location);
} catch (MalformedURLException e) {
StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
"Error migrating keyring credentials for " + getUrl(location), e)); //$NON-NLS-1$
}
}
protected Map<String, String> getAuthorizationInfo(String url) throws MalformedURLException {
return getAuthorizationInfo(new URL(url), authRealm, authScheme);
}
@SuppressWarnings("deprecation")
protected Map<String, String> getAuthorizationInfo(URL url, String realm, String scheme)
throws MalformedURLException {
return Platform.getAuthorizationInfo(url, realm, scheme);
}
protected void putProperties(Map<String, String> properties, T location) {
if (properties != null) {
ICredentialsStore store = service.getCredentialsStore(getUrl(location));
for (Entry<String, String> entry : properties.entrySet()) {
putKeyValue(location, entry.getKey(), entry.getValue(), store);
}
}
}
protected void putKeyValue(T location, String key, String value, ICredentialsStore store) {
store.put(key, value, key.endsWith(".password")); //$NON-NLS-1$
}
protected abstract String getUrl(T location);
}
|