/*******************************************************************************
 * Copyright (c) 2012-2015 Codenvy, S.A.
 * 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:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.git.impl.nativegit.ssh;

import com.google.inject.Inject;

import org.eclipse.che.api.core.UnauthorizedException;
import org.eclipse.che.api.git.GitException;
import org.eclipse.che.git.impl.nativegit.GitUrl;
import org.eclipse.che.ide.ext.ssh.server.SshKey;
import org.eclipse.che.ide.ext.ssh.server.SshKeyPair;
import org.eclipse.che.ide.ext.ssh.server.SshKeyStore;
import org.eclipse.che.ide.ext.ssh.server.SshKeyStoreException;
import org.eclipse.che.ide.ext.ssh.server.SshKeyUploader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

/**
 * Implementation SshKeyProvider that provides private key and upload public
 *
 * @author Anton Korneta
 */
public class SshKeyProviderImpl implements SshKeyProvider {

    private static final Logger LOG = LoggerFactory.getLogger(SshKeyProviderImpl.class);
    private final SshKeyStore         sshKeyStore;
    private final Set<SshKeyUploader> sshKeyUploaders;

    @Inject
    public SshKeyProviderImpl(SshKeyStore sshKeyStore, Set<SshKeyUploader> sshKeyUploaders) {
        this.sshKeyStore = sshKeyStore;
        this.sshKeyUploaders = sshKeyUploaders;
    }

    /**
     * Get private ssh key and upload public ssh key to repository hosting service.
     *
     * @param url
     *         url to git repository
     * @return private ssh key
     * @throws GitException
     *         if an error occurs while generating or uploading keys
     */
    @Override
    public byte[] getPrivateKey(String url) throws GitException {
        String host = GitUrl.getHost(url);
        SshKey publicKey;
        SshKey privateKey;

        // check keys existence and generate if need
        try {
            if ((privateKey = sshKeyStore.getPrivateKey(host)) != null) {
                publicKey = sshKeyStore.getPublicKey(host);
                if (publicKey == null) {
                    sshKeyStore.removeKeys(host);
                    SshKeyPair sshKeyPair = sshKeyStore.genKeyPair(host, null, null);
                    publicKey = sshKeyPair.getPublicKey();
                    privateKey = sshKeyPair.getPrivateKey();
                }
            } else {
                SshKeyPair sshKeyPair = sshKeyStore.genKeyPair(host, null, null);
                publicKey = sshKeyPair.getPublicKey();
                privateKey = sshKeyPair.getPrivateKey();
            }
        } catch (SshKeyStoreException e) {
            throw new GitException(e.getMessage(), e);
        }

        SshKeyUploader uploader = null;

        for (Iterator<SshKeyUploader> itr = sshKeyUploaders.iterator(); uploader == null && itr.hasNext(); ) {
            SshKeyUploader next = itr.next();
            if (next.match(url)) {
                uploader = next;
            }
        }

        if (uploader != null) {
            // upload public key
            try {
                uploader.uploadKey(publicKey);
            } catch (IOException e) {
                throw new GitException(e.getMessage(), e);
            } catch (UnauthorizedException e) {
                // Git action might fail without uploaded public SSH key.
                LOG.warn(String.format("Unable upload public SSH key with %s", uploader.getClass().getSimpleName()), e);
            }
        } else {
            // Git action might fail without SSH key.
            LOG.warn(String.format("Not found ssh key uploader for %s", host));
        }
        return privateKey.getBytes();
    }
}
@@ -802,62 +802,8 @@
 ey;%0A
-import org.eclipse.che.ide.ext.ssh.server.SshKeyPair;%0A
 impo
@@ -1078,24 +1078,24 @@
 va.util.
-Iterator
+Optional
 ;%0Aimport
@@ -1284,17 +1284,16 @@
 vider %7B%0A
-%0A
     priv
@@ -1373,16 +1373,17 @@
 class);%0A
+%0A
     priv
@@ -2184,29 +2184,8 @@
 ence
- and generate if need
 %0A   
@@ -2345,84 +2345,84 @@
     
-    if (publicKey == null) %7B%0A                    sshKeyStore.removeKeys(host
+%7D else %7B%0A                throw new GitException(%22Unable get private ssh key%22
 );%0A 
@@ -2428,32 +2428,34 @@
             
+%7D%0A
         
 SshKeyPair s
@@ -2446,130 +2446,95 @@
     
-SshKeyPair sshKeyPair = sshKeyStore.genKeyPair(host, null, null);%0A                    publicKey = sshKeyPair.getPublicKey(
+%7D catch (SshKeyStoreException e) %7B%0A            throw new GitException(e.getMessage(), e
 );%0A 
@@ -2536,32 +2536,35 @@
 e);%0A        
+%7D%0A%0A
         
     privateK
@@ -2555,53 +2555,90 @@
     
-    privateKey = sshKeyPair.getPrivateKey();%0A
+final Optional%3CSshKeyUploader%3E optionalKeyUploader = sshKeyUploaders.stream()%0A    
     
@@ -2641,34 +2641,32 @@
                 
-%7D%0A
             %7D el
@@ -2657,33 +2657,31 @@
             
-%7D else %7B%0A
+       
             
@@ -2688,186 +2688,94 @@
     
-SshKeyPair sshKeyPair = sshKeyStore.genKeyPair(host, null, null);%0A                publicKey = sshKeyPair.getPublicKey();%0A                privateKey = sshKeyPair.getPrivateKey();%0A
+                     .filter(keyUploader -%3E keyUploader.match(url))%0A                  
     
@@ -2778,34 +2778,32 @@
             
-%7D%0A
         
 %7D catch (Ssh
@@ -2794,106 +2794,35 @@
     
-%7D catch (SshKeyStoreException e) %7B%0A            throw new GitException(e.getMessage(), e);%0A
+                   
         
 %7D%0A%0A 
@@ -2817,27 +2817,24 @@
         
-%7D%0A%0A
         
 SshKeyUp
@@ -2829,88 +2829,45 @@
     
-SshKeyUploader uploader = null;%0A%0A        for (Iterator%3CSshKeyUploader%3E itr = ssh
+   .findFirst();%0A        if (optional
 KeyU
@@ -2877,57 +2877,20 @@
 ader
-s
 .i
-terator(); uploader == null && itr.hasNex
+sPresen
 t()
-; 
 ) %7B%0A
@@ -2897,24 +2897,30 @@
             
+final 
 SshKeyUpload
@@ -2926,151 +2926,45 @@
 der 
-next = itr.next();%0A            if (next.match(url)) %7B%0A                uploader = next;%0A            %7D%0A        %7D%0A%0A        if (uploader != null) %7B
+uploader = optionalKeyUploader.get();
 %0A   

Revert "Revert "IDEX-2234 Removed autogenerating of ssh keys""