/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.plugin.ssh.key.client.upload;

import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
import static org.eclipse.che.ide.util.StringUtils.isNullOrEmpty;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import javax.validation.constraints.NotNull;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.plugin.ssh.key.client.SshKeyLocalizationConstant;

/**
 * Main appointment of this class is upload private SSH key to the server.
 *
 * @author Evgen Vidolob
 */
@Singleton
public class UploadSshKeyPresenter implements UploadSshKeyView.ActionDelegate {
  private UploadSshKeyView view;
  private SshKeyLocalizationConstant constant;
  private String restContext;
  private NotificationManager notificationManager;
  private AsyncCallback<Void> callback;
  private AppContext appContext;

  @Inject
  public UploadSshKeyPresenter(
      UploadSshKeyView view,
      SshKeyLocalizationConstant constant,
      AppContext appContext,
      NotificationManager notificationManager) {
    this.view = view;
    this.view.setDelegate(this);
    this.constant = constant;
    this.restContext = appContext.getMasterApiEndpoint();
    this.notificationManager = notificationManager;
    this.appContext = appContext;
  }

  /** Show dialog. */
  public void showDialog(@NotNull AsyncCallback<Void> callback) {
    this.callback = callback;
    view.setMessage("");
    view.setHost("");
    view.setEnabledUploadButton(false);
    view.showDialog();
  }

  /** {@inheritDoc} */
  @Override
  public void onCancelClicked() {
    view.close();
  }

  /** {@inheritDoc} */
  @Override
  public void onUploadClicked() {
    String host = view.getHost();
    if (host.isEmpty()) {
      view.setMessage(constant.hostValidationError());
      return;
    }
    view.setEncoding(FormPanel.ENCODING_MULTIPART);

    String action = restContext + "/ssh";
    StringBuilder queryParametersBuilder = new StringBuilder();

    String csrfToken = appContext.getProperties().get("X-CSRF-Token");
    if (!isNullOrEmpty(csrfToken)) {
      queryParametersBuilder.append("&X-CSRF-Token=").append(csrfToken);
    }

    String machineToken = appContext.getWorkspace().getRuntime().getMachineToken();
    if (!isNullOrEmpty(machineToken)) {
      queryParametersBuilder.append("&token=").append(machineToken);
    }

    String queryParameters = queryParametersBuilder.toString();
    if (!isNullOrEmpty(queryParameters)) {
      action += queryParameters.replaceFirst("&", "?");
    }

    view.setAction(action);
    view.submit();
  }

  /** {@inheritDoc} */
  @Override
  public void onSubmitComplete(@NotNull String result) {
    if (result.isEmpty()) {
      view.close();
      callback.onSuccess(null);
    } else {
      notificationManager.notify(constant.failedToUploadSshKey(), result, FAIL, FLOAT_MODE);
      callback.onFailure(new Throwable(result));
    }
  }

  /** {@inheritDoc} */
  @Override
  public void onFileNameChanged() {
    String fileName = view.getFileName();
    view.setEnabledUploadButton(!fileName.isEmpty());
  }
}

@@ -1033,16 +1033,77 @@
 onstant;
+%0Aimport org.eclipse.che.security.oauth.SecurityTokenProvider;
 %0A%0A/**%0A *
@@ -1331,16 +1331,71 @@
 w view;%0A
+  private SecurityTokenProvider securityTokenProvider;%0A
   privat
@@ -1780,32 +1780,139 @@
 ager
-) %7B%0A    this.view = view
+,%0A      SecurityTokenProvider securityTokenProvider) %7B%0A    this.view = view;%0A    this.securityTokenProvider = securityTokenProvider
 ;%0A  
@@ -2707,50 +2707,8 @@
 );%0A%0A
-    String action = restContext + %22/ssh%22;%0A
     
@@ -2964,88 +2964,158 @@
     
-String machineToken = appContext.getWorkspace().getRuntime().getMachineToken();%0A
+securityTokenProvider%0A        .getSecurityToken()%0A        .then(%0A            token -%3E %7B%0A              String action = restContext + %22/ssh%22;%0A          
     
@@ -3133,24 +3133,17 @@
 OrEmpty(
-machineT
+t
 oken)) %7B
@@ -3135,32 +3135,42 @@
 Empty(token)) %7B%0A
+          
       queryParam
@@ -3211,16 +3211,9 @@
 end(
-machineT
+t
 oken
@@ -3215,27 +3215,47 @@
 token);%0A    
-%7D%0A%0A
+          %7D%0A%0A          
     String q
@@ -3302,24 +3302,34 @@
 toString();%0A
+          
     if (!isN
@@ -3365,16 +3365,26 @@
 %7B%0A      
+     
+     
 action +
@@ -3425,27 +3425,47 @@
 , %22?%22);%0A    
-%7D%0A%0A
+          %7D%0A%0A          
     view.set
@@ -3476,24 +3476,34 @@
 on(action);%0A
+          
     view.sub
@@ -3505,24 +3505,40 @@
 w.submit();%0A
+            %7D);%0A
   %7D%0A%0A  /** %7B

Use keyclock token in case MultiUser assembly for SSH Key Uploading (#11702)