diff options
Diffstat (limited to 'org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn')
12 files changed, 1018 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/ILocationService.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/ILocationService.java new file mode 100644 index 00000000..1988c03d --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/ILocationService.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import java.net.Proxy; + +import javax.net.ssl.X509TrustManager; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationCredentials; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationType; +import org.eclipse.mylyn.commons.repositories.auth.ICredentialsStore; + +/** + * @author Steffen Pingel + * @noextend This interface is not intended to be extended by clients. + * @noimplement This interface is not intended to be implemented by clients. + */ +public interface ILocationService { + + // FIXME replace with 3.5 proxy API + public abstract Proxy getProxyForHost(String host, String proxyType); + + public abstract X509TrustManager getTrustManager(); + + public abstract <T extends AuthenticationCredentials> T requestCredentials(AuthenticationType type, + Class<T> credentialsKind, String message, IProgressMonitor monitor); + + public ICredentialsStore getCredentialsStore(String id); + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryCategory.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryCategory.java new file mode 100644 index 00000000..0aa6bb09 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryCategory.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import org.eclipse.core.runtime.PlatformObject; + +/** + * Categories to group repositories of the same kind, e.g. Tasks, Builds or Reviews. + * + * @author Robert Elves + */ +public class RepositoryCategory extends PlatformObject { + + private final String id; + + private final String label; + + private final int rank; + + public static final String ID_CATEGORY_BUGS = "org.eclipse.mylyn.category.bugs"; //$NON-NLS-1$ + + public static final String ID_CATEGORY_BUILDS = "org.eclipse.mylyn.category.build"; //$NON-NLS-1$ + + public static final String ID_CATEGORY_OTHER = "org.eclipse.mylyn.category.other"; //$NON-NLS-1$ + + public static final String ID_CATEGORY_REVIEWS = "org.eclipse.mylyn.category.review"; //$NON-NLS-1$ + + public static final String ID_CATEGORY_TASKS = "org.eclipse.mylyn.category.tasks"; //$NON-NLS-1$ + + public static final String ID_CATEGORY_ALL = "org.eclipse.mylyn.category.all"; //$NON-NLS-1$ + + public static final String ID_CATEGORY_ROOT = "org.eclipse.mylyn.category.root"; //$NON-NLS-1$ + + public RepositoryCategory(String id, String label, int rank) { + this.id = id; + this.label = label; + this.rank = rank; + } + + public String getId() { + return id; + } + + public int compareTo(Object arg0) { + if (arg0 instanceof RepositoryCategory) { + return this.getRank() - ((RepositoryCategory) arg0).getRank(); + } + return 0; + } + + public int getRank() { + return rank; + } + + public String getLabel() { + return label; + } + + @Override + public String toString() { + return getLabel(); + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryLocation.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryLocation.java new file mode 100644 index 00000000..c8945556 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryLocation.java @@ -0,0 +1,358 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.PlatformObject; +import org.eclipse.equinox.security.storage.StorageException; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationCredentials; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationType; +import org.eclipse.mylyn.commons.repositories.auth.ICredentialsStore; +import org.eclipse.mylyn.internal.commons.repositories.CredentialsFactory; +import org.eclipse.mylyn.internal.commons.repositories.InMemoryCredentialsStore; +import org.eclipse.mylyn.internal.commons.repositories.LocationService; + +/** + * @author Steffen Pingel + */ +public class RepositoryLocation extends PlatformObject { + + private static final String AUTH_HTTP = "org.eclipse.mylyn.tasklist.repositories.httpauth"; //$NON-NLS-1$ + + private static final String AUTH_PROXY = "org.eclipse.mylyn.tasklist.repositories.proxy"; //$NON-NLS-1$ + + private static final String AUTH_REPOSITORY = "org.eclipse.mylyn.tasklist.repositories"; //$NON-NLS-1$ + + private static final String ENABLED = ".enabled"; //$NON-NLS-1$ + + private static final String ID_PLUGIN = "org.eclipse.mylyn.commons.repository"; //$NON-NLS-1$ + + public static final String PROPERTY_CATEGORY = "category"; //$NON-NLS-1$ + + public static final String PROPERTY_ENCODING = "encoding"; //$NON-NLS-1$ + + public static final String PROPERTY_ID = "id"; //$NON-NLS-1$ + + public static final String PROPERTY_LABEL = "label"; //$NON-NLS-1$ + + public static final String PROPERTY_OFFLINE = "org.eclipse.mylyn.tasklist.repositories.offline"; //$NON-NLS-1$ + + public static final String PROPERTY_TIMEZONE = "timezone"; //$NON-NLS-1$ + + public static final String PROPERTY_URL = "url"; //$NON-NLS-1$ + + public static final String PROPERTY_USERNAME = "org.eclipse.mylyn.repositories.username"; //$NON-NLS-1$ + + public static final String PROPERTY_PROXY_HOST = "org.eclipse.mylyn.repositories.proxy.host"; //$NON-NLS-1$ + + public static final String PROPERTY_PROXY_PORT = "org.eclipse.mylyn.repositories.proxy.port"; //$NON-NLS-1$ + + public static final String PROPERTY_PROXY_USEDEFAULT = "org.eclipse.mylyn.repositories.proxy.usedefault"; //$NON-NLS-1$ + + private static final String SAVE_PASSWORD = ".savePassword"; //$NON-NLS-1$ + + private static String getKeyPrefix(AuthenticationType type) { + switch (type) { + case HTTP: + return AUTH_HTTP; + case PROXY: + return AUTH_PROXY; + case REPOSITORY: + return AUTH_REPOSITORY; + } + throw new IllegalArgumentException("Unknown authentication type: " + type); //$NON-NLS-1$ + } + + private ICredentialsStore credentialsStore; + + // transient + private IStatus errorStatus = null; + + private final Map<String, String> properties = new LinkedHashMap<String, String>(); + + private final Set<PropertyChangeListener> propertyChangeListeners = new HashSet<PropertyChangeListener>(); + + private ILocationService service; + + private boolean workingCopy; + + public RepositoryLocation() { + this.service = LocationService.getDefault(); + } + + public RepositoryLocation(Map<String, String> properties) { + this.properties.putAll(properties); + this.workingCopy = true; + this.service = LocationService.getDefault(); + } + + public RepositoryLocation(RepositoryLocation source) { + this.properties.putAll(source.properties); + this.workingCopy = true; + this.service = source.getService(); + } + + public void addChangeListener(PropertyChangeListener listener) { + propertyChangeListeners.add(listener); + } + + public void clearCredentials() { + getCredentialsStore().clear(); + } + + public <T extends AuthenticationCredentials> T getCredentials(AuthenticationType authType, Class<T> credentialsKind) { + String prefix = getKeyPrefix(authType); + if (getBooleanPropery(prefix + ENABLED)) { + if (getId() == null) { + // can't determine location of credentials + return null; + } + try { + return CredentialsFactory.create(credentialsKind, getCredentialsStore(), prefix); + } catch (StorageException e) { + // FIXME + } + } + return null; + } + + public ICredentialsStore getCredentialsStore() { + if (credentialsStore == null) { + return getService().getCredentialsStore(getId()); + } + return credentialsStore; + } + + public String getId() { + String id = getProperty(PROPERTY_ID); + if (id == null) { + throw new IllegalStateException("Repository ID is not set"); //$NON-NLS-1$ + } + return id; + } + + public Map<String, String> getProperties() { + return new LinkedHashMap<String, String>(this.properties); + } + + public String getProperty(String name) { + return this.properties.get(name); + } + + /** + * @return the URL if the label property is not set + */ + public String getLabel() { + String label = properties.get(PROPERTY_LABEL); + if (label != null && label.length() > 0) { + return label; + } else { + return getUrl(); + } + } + + public boolean getSavePassword(AuthenticationType authType) { + return getBooleanPropery(getKeyPrefix(authType) + SAVE_PASSWORD); + } + + public boolean getBooleanPropery(String key) { + String value = getProperty(key); + return value != null && Boolean.parseBoolean(value); + } + + public ILocationService getService() { + return service; + } + + public IStatus getStatus() { + return errorStatus; + } + + public String getUrl() { + return getProperty(PROPERTY_URL); + } + + /** + * The username is cached since it needs to be retrieved frequently (e.g. for Task List decoration). + */ + public String getUserName() { + return getProperty(PROPERTY_USERNAME); + } + + public void setUserName(String userName) { + setProperty(PROPERTY_USERNAME, userName); + } + + private void handlePropertyChange(String key, Object old, Object value) { + if (PROPERTY_ID.equals(key)) { + credentialsStore = null; + } + + PropertyChangeEvent event = new PropertyChangeEvent(this, key, old, value); + for (PropertyChangeListener listener : propertyChangeListeners) { + listener.propertyChange(event); + } + } + + private boolean hasChanged(Object oldValue, Object newValue) { + return oldValue != null && !oldValue.equals(newValue) || oldValue == null && newValue != null; + } + + public boolean hasProperty(String name) { + String value = getProperty(name); + return value != null && value.trim().length() > 0; + } + + public boolean isOffline() { + return Boolean.parseBoolean(getProperty(PROPERTY_OFFLINE)); + } + + public boolean isWorkingCopy() { + return workingCopy; + } + + public void removeChangeListener(PropertyChangeListener listener) { + propertyChangeListeners.remove(listener); + } + + public void removeProperty(String key) { + setProperty(key, null); + } + + public <T extends AuthenticationCredentials> void setCredentials(AuthenticationType authType, T credentials) { + String prefix = getKeyPrefix(authType); + + if (credentials == null) { + setProperty(prefix + ENABLED, String.valueOf(false)); + } else { + setProperty(prefix + ENABLED, String.valueOf(true)); + try { + credentials.save(getCredentialsStore(), prefix); + } catch (StorageException e) { + // FIXME + } + } + } + + public void setCredentialsStore(ICredentialsStore credentialsStore) { + this.credentialsStore = credentialsStore; + } + + public void setLabel(String label) { + setProperty(PROPERTY_LABEL, label); + } + + public void setOffline(boolean offline) { + properties.put(PROPERTY_OFFLINE, String.valueOf(offline)); + } + + public void setProperty(String key, String newValue) { + Assert.isNotNull(key); + String oldValue = this.properties.get(key); + if (hasChanged(oldValue, newValue)) { + this.properties.put(key.intern(), (newValue != null) ? newValue.intern() : null); + handlePropertyChange(key, oldValue, newValue); + } + } + + public void setService(ILocationService service) { + this.service = service; + } + + public void setStatus(IStatus errorStatus) { + this.errorStatus = errorStatus; + } + + @Override + public String toString() { + return getLabel(); + } + + public void apply(RepositoryLocation location) { + String oldId = getProperty(PROPERTY_ID); + ICredentialsStore oldCredentialsStore = null; + if (oldId != null) { + oldCredentialsStore = getCredentialsStore(); + } + + // merge properties + HashSet<String> removed = new HashSet<String>(properties.keySet()); + removed.removeAll(location.properties.keySet()); + for (Map.Entry<String, String> entry : location.properties.entrySet()) { + setProperty(entry.getKey(), entry.getValue()); + } + for (String key : removed) { + setProperty(key, null); + } + + String newId = getProperty(PROPERTY_ID); + if (newId != null) { + // migrate credentials if url has changed + ICredentialsStore newCredentialsStore = getCredentialsStore(); + if (!newId.equals(oldId)) { + if (oldCredentialsStore != null) { + try { + oldCredentialsStore.copyTo(newCredentialsStore); + oldCredentialsStore.clear(); + } catch (StorageException e) { + // FIXME + } + } + } + + // merge credentials + if (location.getCredentialsStore() instanceof InMemoryCredentialsStore) { + try { + ((InMemoryCredentialsStore) location.getCredentialsStore()).copyTo(newCredentialsStore); + } catch (StorageException e) { + // FIXME + } + } + } + + } + + public void setIdPreservingCredentialsStore(String id) { + ICredentialsStore store = getCredentialsStore(); + setProperty(RepositoryLocation.PROPERTY_ID, id); + if (this.credentialsStore == null) { + setCredentialsStore(store); + } + } + + /** + * Returns true if a normalized form of <code>url</code> matches the URL of this location. + */ + public boolean hasUrl(String url) { + Assert.isNotNull(url); + String myUrl = getUrl(); + if (myUrl == null) { + return false; + } + try { + return new URI(url + "/").normalize().equals(new URI(myUrl + "/").normalize()); //$NON-NLS-1$//$NON-NLS-2$ + } catch (URISyntaxException e) { + return false; + } + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryValidator.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryValidator.java new file mode 100644 index 00000000..905caaa4 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/RepositoryValidator.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; + +/** + * @author Steffen Pingel + */ +public abstract class RepositoryValidator { + + private final RepositoryLocation location; + + private IStatus result; + + public RepositoryValidator(RepositoryLocation location) { + this.location = location; + } + + public RepositoryLocation getLocation() { + return location; + } + + public IStatus getResult() { + return result; + } + + public abstract IStatus run(IProgressMonitor monitor); + + public void setResult(IStatus result) { + this.result = result; + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/AuthenticationCredentials.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/AuthenticationCredentials.java new file mode 100644 index 00000000..d2ab0a4d --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/AuthenticationCredentials.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories.auth; + +import org.eclipse.equinox.security.storage.StorageException; + +/** + * @author Steffen Pingel + */ +public abstract class AuthenticationCredentials { + + public abstract void save(ICredentialsStore store, String prefix) throws StorageException; + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/AuthenticationType.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/AuthenticationType.java new file mode 100644 index 00000000..05d33309 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/AuthenticationType.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2004, 2008 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.commons.repositories.auth; + +/** + * An enum of supported authentication types. + * + * @author Steffen Pingel + */ +public enum AuthenticationType { + /** + * HTTP authentication, this is typically basic authentication but other methods such as digest or NTLM are used as + * well. + */ + HTTP, + /** Proxy authentication. */ + PROXY, + /** Task repository authentication. */ + REPOSITORY +}
\ No newline at end of file diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/ICredentialsStore.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/ICredentialsStore.java new file mode 100644 index 00000000..05962ffd --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/ICredentialsStore.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories.auth; + +import java.io.IOException; + +import org.eclipse.equinox.security.storage.StorageException; + +/** + * @author Steffen Pingel + * @noextend This interface is not intended to be extended by clients. + * @noimplement This interface is not intended to be implemented by clients. + */ +public interface ICredentialsStore { + + public void clear(); + + public void flush() throws IOException; + + public String get(String key, String def) throws StorageException; + + public byte[] getByteArray(String key, byte[] def) throws StorageException; + + public String[] keys(); + + public void put(String key, String value, boolean encrypt) throws StorageException; + + public void putByteArray(String key, byte[] value, boolean encrypt) throws StorageException; + + public void remove(String key); + + public void copyTo(ICredentialsStore target) throws StorageException; + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/UsernamePasswordCredentials.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/UsernamePasswordCredentials.java new file mode 100644 index 00000000..df86dd54 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/commons/repositories/auth/UsernamePasswordCredentials.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2004, 2008 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.commons.repositories.auth; + +import org.eclipse.equinox.security.storage.StorageException; + +/** + * Provides a user name and password. + * + * @author Steffen Pingel + * @noextend This class is not intended to be subclassed by clients. + */ +public class UsernamePasswordCredentials extends AuthenticationCredentials { + + private final String userName; + + private final String password; + + /** + * @param userName + * the user name, must not be null + * @param password + * the password, must not be null + */ + public UsernamePasswordCredentials(String userName, String password) { + if (userName == null) { + throw new IllegalArgumentException(); + } + if (password == null) { + throw new IllegalArgumentException(); + } + + this.userName = userName; + this.password = password; + } + + public String getUserName() { + return userName; + } + + public String getPassword() { + return password; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((password == null) ? 0 : password.hashCode()); + result = prime * result + ((userName == null) ? 0 : userName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final UsernamePasswordCredentials other = (UsernamePasswordCredentials) obj; + if (password == null) { + if (other.password != null) { + return false; + } + } else if (!password.equals(other.password)) { + return false; + } + if (userName == null) { + if (other.userName != null) { + return false; + } + } else if (!userName.equals(other.userName)) { + return false; + } + return true; + } + + public static UsernamePasswordCredentials create(ICredentialsStore store, String prefix) throws StorageException { + String userName = store.get(prefix + ".user", ""); //$NON-NLS-1$ //$NON-NLS-2$ + String password = store.get(prefix + ".password", ""); //$NON-NLS-1$ //$NON-NLS-2$ + return new UsernamePasswordCredentials(userName, password); + } + + @Override + public void save(ICredentialsStore store, String prefix) throws StorageException { + store.put(prefix + ".user", userName, false); //$NON-NLS-1$ + store.put(prefix + ".password", password, true); //$NON-NLS-1$ + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/CredentialsFactory.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/CredentialsFactory.java new file mode 100644 index 00000000..7412b63c --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/CredentialsFactory.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import org.eclipse.equinox.security.storage.StorageException; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationCredentials; +import org.eclipse.mylyn.commons.repositories.auth.ICredentialsStore; +import org.eclipse.mylyn.commons.repositories.auth.UsernamePasswordCredentials; + +/** + * Simple factory that creates {@link AuthenticationCredentials} objects. + * + * @author Steffen Pingel + */ +public class CredentialsFactory { + + public static <T extends AuthenticationCredentials> T create(Class<T> credentialsKind, + ICredentialsStore credentialsStore, String key) throws StorageException { + if (credentialsKind == UsernamePasswordCredentials.class) { + return (T) UsernamePasswordCredentials.create(credentialsStore, key); + } + throw new IllegalArgumentException("Unknown credentials type: " + credentialsKind); //$NON-NLS-1$ + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/InMemoryCredentialsStore.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/InMemoryCredentialsStore.java new file mode 100644 index 00000000..69c692e0 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/InMemoryCredentialsStore.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.eclipse.equinox.security.storage.StorageException; +import org.eclipse.mylyn.commons.repositories.auth.ICredentialsStore; + +/** + * @author Steffen Pingel + */ +public class InMemoryCredentialsStore implements ICredentialsStore { + + private final ConcurrentHashMap<String, Object> store; + + private final ICredentialsStore parent; + + public InMemoryCredentialsStore(ICredentialsStore parent) { + this.parent = parent; + store = new ConcurrentHashMap<String, Object>(); + } + + public void clear() { + store.clear(); + } + + public void flush() throws IOException { + // does nothing + } + + public String get(String key, String def) throws StorageException { + String value = (String) store.get(key); + if (value == null && parent != null) { + return parent.get(key, def); + } + return (value != null) ? value : def; + } + + public byte[] getByteArray(String key, byte[] def) throws StorageException { + byte[] value = (byte[]) store.get(key); + if (value == null && parent != null) { + return parent.getByteArray(key, def); + } + return (value != null) ? value : def; + } + + public String[] keys() { + return store.keySet().toArray(new String[0]); + } + + public void put(String key, String value, boolean encrypt) throws StorageException { + store.put(key, value); + } + + public void putByteArray(String key, byte[] value, boolean encrypt) throws StorageException { + store.put(key, value); + } + + public void remove(String key) { + store.remove(key); + } + + public void copyTo(ICredentialsStore target) throws StorageException { + for (Map.Entry<String, Object> entry : store.entrySet()) { + if (entry.getValue() instanceof String) { + target.put(entry.getKey(), (String) entry.getValue(), true); + } else if (entry.getValue() instanceof byte[]) { + target.putByteArray(entry.getKey(), (byte[]) entry.getValue(), true); + } + } + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/LocationService.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/LocationService.java new file mode 100644 index 00000000..b6bc8f90 --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/LocationService.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import java.net.Proxy; +import java.util.HashMap; +import java.util.Map; + +import javax.net.ssl.X509TrustManager; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.mylyn.commons.net.IProxyProvider; +import org.eclipse.mylyn.commons.net.WebUtil; +import org.eclipse.mylyn.commons.repositories.ILocationService; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationCredentials; +import org.eclipse.mylyn.commons.repositories.auth.AuthenticationType; +import org.eclipse.mylyn.commons.repositories.auth.ICredentialsStore; +import org.eclipse.mylyn.commons.repositories.auth.UsernamePasswordCredentials; + +/** + * @author Steffen Pingel + */ +public class LocationService implements ILocationService { + + public static final String ID_PLUGIN = "org.eclipse.mylyn.commons.repository"; //$NON-NLS-1$ + + private static LocationService instance = new LocationService(null, null, new PlatformProxyProvider()); + + public static LocationService getDefault() { + return instance; + } + + private static class PlatformProxyProvider implements IProxyProvider { + + public Proxy getProxyForHost(String host, String proxyType) { + return WebUtil.getProxy(host, proxyType); + } + + } + + private final Map<AuthenticationType, UsernamePasswordCredentials> credentialsByType; + + private final IProxyProvider proxyProvider; + + public LocationService(String username, String password, IProxyProvider proxyProvider) { + this.credentialsByType = new HashMap<AuthenticationType, UsernamePasswordCredentials>(); + this.proxyProvider = proxyProvider; + + if (username != null && password != null) { + setCredentials(AuthenticationType.REPOSITORY, username, password); + } + } + +// public LocationService(String url, String username, String password) { +// this(url, username, password, new PlatformProxyProvider()); +// } +// +// public LocationService(String url) { +// this(url, null, null, new PlatformProxyProvider()); +// } + + public UsernamePasswordCredentials getCredentials(AuthenticationType authType) { + return credentialsByType.get(authType); + } + + public Proxy getProxyForHost(String host, String proxyType) { + if (proxyProvider != null) { + return proxyProvider.getProxyForHost(host, proxyType); + } + return null; + } + + public void setCredentials(AuthenticationType authType, String username, String password) { + credentialsByType.put(authType, new UsernamePasswordCredentials(username, password)); + } + + public X509TrustManager getTrustManager() { + // ignore + return null; + } + + public <T extends AuthenticationCredentials> T requestCredentials(AuthenticationType type, + Class<T> credentialsKind, String message, IProgressMonitor monitor) { + throw new UnsupportedOperationException(); + } + + public ICredentialsStore getCredentialsStore(String id) { + return new SecureCredentialsStore(id); + } + +} diff --git a/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/SecureCredentialsStore.java b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/SecureCredentialsStore.java new file mode 100644 index 00000000..8b8d4aee --- /dev/null +++ b/org.eclipse.mylyn.commons.repositories.core/src/org/eclipse/mylyn/internal/commons/repositories/SecureCredentialsStore.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2010 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.commons.repositories; + +import java.io.IOException; + +import org.eclipse.equinox.security.storage.EncodingUtils; +import org.eclipse.equinox.security.storage.ISecurePreferences; +import org.eclipse.equinox.security.storage.SecurePreferencesFactory; +import org.eclipse.equinox.security.storage.StorageException; +import org.eclipse.mylyn.commons.repositories.auth.ICredentialsStore; + +/** + * @author Steffen Pingel + */ +public class SecureCredentialsStore implements ICredentialsStore { + + private static final String ID_PLUGIN = "org.eclipse.mylyn.commons.repository"; //$NON-NLS-1$ + + private final String url; + + public SecureCredentialsStore(String url) { + this.url = url; + } + + public void clear() { + //getSecurePreferences().clear(); + getSecurePreferences().removeNode(); + } + + public void flush() throws IOException { + getSecurePreferences().flush(); + } + + public String get(String key, String def) throws StorageException { + return getSecurePreferences().get(key, def); + } + + public byte[] getByteArray(String key, byte[] def) throws StorageException { + return getSecurePreferences().getByteArray(key, def); + } + + private ISecurePreferences getSecurePreferences() { + ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault().node(ID_PLUGIN); + securePreferences = securePreferences.node(EncodingUtils.encodeSlashes(getUrl())); + return securePreferences; + } + + public String getUrl() { + return url; + } + + public String[] keys() { + return getSecurePreferences().keys(); + } + + public void put(String key, String value, boolean encrypt) throws StorageException { + getSecurePreferences().put(key, value, encrypt); + } + + public void putByteArray(String key, byte[] value, boolean encrypt) throws StorageException { + getSecurePreferences().putByteArray(key, value, encrypt); + } + + public void remove(String key) { + getSecurePreferences().remove(key); + } + + public void copyTo(ICredentialsStore target) throws StorageException { + ISecurePreferences preferences = getSecurePreferences(); + for (String key : preferences.keys()) { + target.put(key, preferences.get(key, null), preferences.isEncrypted(key)); + } + } + +} |