Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / SecureStoreUtils.java
blob89ba06f83d63c8648fd65cc94b8a3380ca32000c
1 /*******************************************************************************
2 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
3 * Copyright (C) 2010, Philipp Thun <philipp.thun@sap.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org.eclipse.egit.ui.internal;
14 import java.io.IOException;
15 import java.text.MessageFormat;
17 import org.eclipse.egit.core.credentials.UserPasswordCredentials;
18 import org.eclipse.egit.ui.Activator;
19 import org.eclipse.equinox.security.storage.StorageException;
20 import org.eclipse.jgit.annotations.Nullable;
21 import org.eclipse.jgit.transport.URIish;
23 /**
24 * Utilities for EGit secure store
26 public class SecureStoreUtils {
27 /**
28 * Store credentials for the given uri
30 * @param credentials
31 * @param uri
32 * @return true if successful
34 public static boolean storeCredentials(UserPasswordCredentials credentials,
35 URIish uri) {
36 if (credentials != null && uri != null) {
37 try {
38 org.eclipse.egit.core.Activator.getDefault()
39 .getCredentialsStore().putCredentials(uri, credentials);
40 } catch (StorageException | IOException e) {
41 Activator.handleError(MessageFormat.format(
42 UIText.SecureStoreUtils_writingCredentialsFailed, uri),
43 e, true);
44 return false;
47 return true;
50 /**
51 * Gets credentials stored for the given uri. Logs {@code StorageException}
52 * if thrown by the secure store implementation and removes credentials
53 * which can't be read from secure store
55 * @param uri
56 * @return credentials stored in secure store for given uri
58 public static @Nullable UserPasswordCredentials getCredentials(
59 final URIish uri) {
60 if (uri == null) {
61 return null;
63 try {
64 return org.eclipse.egit.core.Activator.getDefault()
65 .getCredentialsStore().getCredentials(uri);
66 } catch (StorageException e) {
67 Activator.logError(MessageFormat.format(
68 UIText.SecureStoreUtils_errorReadingCredentials,
69 uri), e);
70 clearCredentials(uri);
71 return null;
75 /**
76 * Clear credentials stored for the given uri if any exist
78 * @param uri
80 public static void clearCredentials(final URIish uri) {
81 if (uri == null) {
82 return;
84 try {
85 org.eclipse.egit.core.Activator.getDefault().getCredentialsStore()
86 .clearCredentials(uri);
87 } catch (IOException e) {
88 Activator.logError(MessageFormat.format(
89 UIText.SecureStoreUtils_errorClearingCredentials, uri), e);