/**
 * Copyright Notice
 *
 * This is a work of the U.S. Government and is not subject to copyright 
 * protection in the United States. Foreign copyrights may apply.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gov.va.isaac.sync.git;

import java.util.Arrays;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

/**
 * {@link SSHFriendlyUsernamePasswordCredsProvider}
 * 
 * This implementation 99% copied from {@link UsernamePasswordCredentialsProvider} - but added support 
 * for some YES/No type questions
 *
 * @author <a href="mailto:daniel.armbrust.list@gmail.com">Dan Armbrust</a> 
 */
public class SSHFriendlyUsernamePasswordCredsProvider extends CredentialsProvider {
	private String username;

	private char[] password;

	/**
	 * Initialize the provider with a single username and password.
	 *
	 * @param username
	 * @param password
	 */
	public SSHFriendlyUsernamePasswordCredsProvider(String username, String password) {
		this(username, password.toCharArray());
	}

	/**
	 * Initialize the provider with a single username and password.
	 *
	 * @param username
	 * @param password
	 */
	public SSHFriendlyUsernamePasswordCredsProvider(String username, char[] password) {
		this.username = username;
		this.password = password;
	}

	@Override
	public boolean isInteractive() {
		return false;
	}

	@Override
	public boolean supports(CredentialItem... items) {
		for (CredentialItem i : items) {
			if (i instanceof CredentialItem.Username)
				continue;

			else if (i instanceof CredentialItem.Password)
				continue;
			
			else if (i instanceof CredentialItem.YesNoType)
				continue;

			else
				return false;
		}
		return true;
	}

	@Override
	public boolean get(URIish uri, CredentialItem... items)
			throws UnsupportedCredentialItem {
		for (CredentialItem i : items) {
			if (i instanceof CredentialItem.Username) {
				((CredentialItem.Username) i).setValue(username);
				continue;
			}
			if (i instanceof CredentialItem.Password) {
				((CredentialItem.Password) i).setValue(password);
				continue;
			}
			if (i instanceof CredentialItem.StringType) {
				if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
					((CredentialItem.StringType) i).setValue(new String(
							password));
					continue;
				}
			}
			if (i instanceof CredentialItem.YesNoType) {
				//TODO - if we have a GUI - prompt the user
				if (i.getPromptText().startsWith("The authenticity of host '")) { //$NON-NLS-1$
					((CredentialItem.YesNoType) i).setValue(true);
					continue;
				}
			}
			throw new UnsupportedCredentialItem(uri, i.getClass().getName()
					+ ":" + i.getPromptText()); //$NON-NLS-1$
		}
		return true;
	}

	/** Destroy the saved username and password.. */
	public void clear() {
		username = null;

		if (password != null) {
			Arrays.fill(password, (char) 0);
			password = null;
		}
	}
}

@@ -1697,16 +1697,43 @@
 sername,
+ (password == null ? null :
  passwor
@@ -1748,16 +1748,17 @@
 Array())
+)
 ;%0A%09%7D%0A%0A%09/
@@ -2292,16 +2292,94 @@
 ssword)%0A
+%09%09%09%7B%0A%09%09%09%09if (password == null)%0A%09%09%09%09%7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09%09%7B%0A%09
 %09%09%09%09cont
@@ -2384,16 +2384,27 @@
 ntinue;%0A
+%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%09%0A%09%09%09e
@@ -2818,24 +2818,178 @@
 Password) %7B%0A
+%09%09%09%09if (password == null)%0A%09%09%09%09%7B%0A%09%09%09%09%09throw new UnsupportedCredentialItem(uri, %22No password was set into the credentials provider%22);%0A%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09%09%7B%0A%09
 %09%09%09%09((Creden
@@ -3030,24 +3030,25 @@
 sword);%0A%09%09%09%09
+%09
 continue;%0A%09%09
@@ -3037,32 +3037,38 @@
 %0A%09%09%09%09%09continue;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09if (i in

handle null password