public UsernamePassword getUsernameAndPasswordForService()

in taverna-credential-manager-impl/src/main/java/org/apache/taverna/security/credentialmanager/impl/CredentialManagerImpl.java [754:835]


	public UsernamePassword getUsernameAndPasswordForService(URI serviceURI,
			boolean usePathRecursion, String requestingMessage)
			throws CMException {
		// Need to make sure we are initialized before we do anything else
		// as Credential Manager can be created but not initialized
		initialize();

		synchronized (keystore) {
			SecretKeySpec passwordKey = null;
			LinkedHashSet<URI> possibleServiceURIsToLookup = getPossibleServiceURIsToLookup(
					serviceURI, usePathRecursion);
			Map<URI, URI> allServiceURIs = getFragmentMappedURIsForAllUsernameAndPasswordPairs();

			try {
				for (URI lookupURI : possibleServiceURIsToLookup) {
					URI mappedURI = allServiceURIs.get(lookupURI);
					if (mappedURI == null)
						continue;

					// We found it - get the username and password in the
					// Keystore associated with this service URI
					String alias = null;
					alias = "password#" + mappedURI.toASCIIString();
					passwordKey = (((SecretKeySpec) keystore.getKey(alias,
							masterPassword.toCharArray())));
					if (passwordKey == null) {
						// Unexpected, it was just there in the map!
						logger.warn("Could not find alias " + alias
								+ " for known uri " + lookupURI
								+ ", just deleted?");
						// Remember we went outside synchronized(keystore) while
						// looping
						continue;
					}
					String unpasspair = new String(passwordKey.getEncoded(),
							UTF_8);
					/*
					 * decoded key contains string
					 * <USERNAME><SEPARATOR_CHARACTER><PASSWORD>
					 */

					int separatorAt = unpasspair
							.indexOf(USERNAME_AND_PASSWORD_SEPARATOR_CHARACTER);
					if (separatorAt < 0)
						throw new CMException("Invalid credentials stored for "
								+ lookupURI);

					String username = unpasspair.substring(0, separatorAt);
					String password = unpasspair.substring(separatorAt + 1);

					UsernamePassword usernamePassword = new UsernamePassword();
					usernamePassword.setUsername(username);
					usernamePassword.setPassword(password.toCharArray());
					return usernamePassword;
				}

				// Nothing found in the Keystore, let's lookup using the service
				// username and password providers
				for (ServiceUsernameAndPasswordProvider provider : serviceUsernameAndPasswordProviders) {
					UsernamePassword usernamePassword = provider
							.getServiceUsernameAndPassword(serviceURI,
									requestingMessage);
					if (usernamePassword == null)
						continue;
					if (usernamePassword.isShouldSave()) {
						URI uri = serviceURI;
						if (usePathRecursion)
							uri = normalizeServiceURI(serviceURI);
						addUsernameAndPasswordForService(usernamePassword, uri);
					}
					return usernamePassword;
				}
				// Giving up
				return null;
			} catch (Exception ex) {
				String exMessage = "Failed to get the username and password pair for service "
						+ serviceURI + " from the Keystore";
				logger.error(exMessage, ex);
				throw new CMException(exMessage, ex);
			}
		}
	}