/*******************************************************************************
 * 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.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.Optional;
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
        try {
            if ((privateKey = sshKeyStore.getPrivateKey(host)) != null) {
                publicKey = sshKeyStore.getPublicKey(host);
            } else {
                throw new GitException("Unable get private ssh key");
            }
        } catch (SshKeyStoreException e) {
            throw new GitException(e.getMessage(), e);
        }

        final Optional<SshKeyUploader> optionalKeyUploader = sshKeyUploaders.stream()
                                                                            .filter(keyUploader -> keyUploader.match(url))
                                                                            .findFirst();
        if (optionalKeyUploader.isPresent()) {
            final SshKeyUploader uploader = optionalKeyUploader.get();
            // 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();
    }
}
@@ -798,16 +798,70 @@
 SshKey;%0A
+import org.eclipse.che.ide.ext.ssh.server.SshKeyPair;%0A
 import o
@@ -1132,24 +1132,24 @@
 va.util.
-Optional
+Iterator
 ;%0Aimport
@@ -1338,16 +1338,17 @@
 vider %7B%0A
+%0A
     priv
@@ -1428,17 +1428,16 @@
 class);%0A
-%0A
     priv
@@ -1800,17 +1800,16 @@
  key to 
-r
 epositor
@@ -2233,16 +2233,37 @@
 xistence
+ and generate if need
 %0A       
@@ -2419,84 +2419,84 @@
     
-%7D else %7B%0A                throw new GitException(%22Unable get private ssh key%22
+    if (publicKey == null) %7B%0A                    sshKeyStore.removeKeys(host
 );%0A 
@@ -2506,26 +2506,24 @@
         
-%7D%0A
         
 %7D catch 
@@ -2518,95 +2518,130 @@
     
-%7D catch (SshKeyStoreException e) %7B%0A            throw new GitException(e.getMessage(), e
+SshKeyPair sshKeyPair = sshKeyStore.genKeyPair(host, null, null);%0A                    publicKey = sshKeyPair.getPublicKey(
 );%0A 
@@ -2651,19 +2651,16 @@
     
-%7D%0A%0A
         
 fina
@@ -2659,85 +2659,52 @@
     
-final Optional%3CSshKeyUploader%3E optionalKeyUploader = sshKeyUploaders.stream
+    privateKey = sshKeyPair.getPrivateKey
 ()
+;
 %0A   
@@ -2708,32 +2708,34 @@
                 
+%7D%0A
                 
@@ -2722,33 +2722,41 @@
   %7D%0A            
- 
+%7D else %7B%0A
                 
@@ -2759,104 +2759,186 @@
     
-                               .filter(keyUploader -%3E keyUploader.match(url))%0A                  
+SshKeyPair sshKeyPair = sshKeyStore.genKeyPair(host, null, null);%0A                publicKey = sshKeyPair.getPublicKey();%0A                privateKey = sshKeyPair.getPrivateKey();%0A
     
@@ -2941,32 +2941,34 @@
             
+%7D%0A
         
             
@@ -2959,35 +2959,106 @@
     
-                   
+%7D catch (SshKeyStoreException e) %7B%0A            throw new GitException(e.getMessage(), e);%0A
         
     
@@ -3053,24 +3053,27 @@
         
+%7D%0A%0A
         
    .find
@@ -3068,45 +3068,88 @@
     
-   .findFirst();%0A        if (optional
+SshKeyUploader uploader = null;%0A%0A        for (Iterator%3CSshKeyUploader%3E itr = ssh
 KeyU
@@ -3159,20 +3159,57 @@
 ader
+s
 .i
-sPresen
+terator(); uploader == null && itr.hasNex
 t()
+; 
 ) %7B%0A
@@ -3220,22 +3220,16 @@
         
-final 
 SshKeyUp
@@ -3239,45 +3239,151 @@
 der 
-uploader = optionalKeyUploader.get();
+next = itr.next();%0A            if (next.match(url)) %7B%0A                uploader = next;%0A            %7D%0A        %7D%0A%0A        if (uploader != null) %7B
 %0A   

Revert "IDEX-2234 Removed autogenerating of ssh keys"