/*******************************************************************************
 * Copyright (c) 2012-2016 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.ide.actions;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.action.Action;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.project.node.HasStorablePath;
import org.eclipse.che.ide.api.selection.Selection;
import org.eclipse.che.ide.download.DownloadContainer;
import org.eclipse.che.ide.part.explorer.project.ProjectExplorerPresenter;
import org.eclipse.che.ide.project.node.FileReferenceNode;

/**
 * Download selected item action.
 * If selected item is a project or a folder then item will be downloaded as a zip archive.
 *
 * @author Roman Nikitenko
 */
@Singleton
public class DownloadItemAction extends Action {

    private final String BASE_URL;

    private final AppContext appContext;
    private final DownloadContainer downloadContainer;
    private final ProjectExplorerPresenter projectExplorer;

    @Inject
    public DownloadItemAction(AppContext appContext,
                              CoreLocalizationConstant locale,
                              DownloadContainer downloadContainer,
                              ProjectExplorerPresenter projectExplorer) {
        super(locale.downloadItemName(), locale.downloadItemDescription());
        this.appContext = appContext;
        this.downloadContainer = downloadContainer;
        this.projectExplorer = projectExplorer;

        BASE_URL = "/project/export/";
    }

    /** {@inheritDoc} */
    @Override
    public void actionPerformed(ActionEvent e) {
        Selection<?> selection = projectExplorer.getSelection();
        if (selection.isEmpty() || selection.getAllElements().size() > 1) {
            return;
        }

        Object selectedNode = selection.getHeadElement();

        if (!(selectedNode instanceof HasStorablePath)) {
            return;
        }

        String url = getUrl((HasStorablePath)selectedNode);
        downloadContainer.setUrl(url);
    }

    /** {@inheritDoc} */
    @Override
    public void update(ActionEvent event) {
        Selection<?> selection = projectExplorer.getSelection();

        if (selection == null || selection.isEmpty() || selection.getAllElements().size() > 1) {
            event.getPresentation().setEnabledAndVisible(false);
            return;
        }

        Object selectedNode = selection.getHeadElement();

        event.getPresentation().setEnabledAndVisible(selectedNode instanceof HasStorablePath);
    }

    private String getUrl(HasStorablePath node) {
        String path = normalizePath(node.getStorablePath());

        if (node instanceof FileReferenceNode) {
            return appContext.getDevMachine().getWsAgentBaseUrl() + BASE_URL + "file/" + path;
        }
        return appContext.getDevMachine().getWsAgentBaseUrl() + BASE_URL + path;
    }

    private String normalizePath(String path) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        return path;
    }
}

@@ -820,16 +820,75 @@
 ontext;%0A
+import org.eclipse.che.ide.api.machine.WsAgentURLModifier;%0A
 import o
@@ -1464,32 +1464,46 @@
 inal AppContext 
+              
 appContext;%0A    
@@ -1526,32 +1526,39 @@
 wnloadContainer 
+       
 downloadContaine
@@ -1615,24 +1615,85 @@
 ectExplorer;
+%0A    private final WsAgentURLModifier       agentURLModifier;
 %0A%0A    @Injec
@@ -1947,16 +1947,83 @@
 Explorer
+,%0A                              WsAgentURLModifier agentURLModifier
 ) %7B%0A    
@@ -2231,16 +2231,66 @@
 xplorer;
+%0A        this.agentURLModifier = agentURLModifier;
 %0A%0A      
@@ -2736,25 +2736,24 @@
       %7D%0A
-%0A
         
 String u
@@ -2748,96 +2748,95 @@
     
-String url = getUrl((HasStorablePath)selectedNode);%0A        downloadContainer.setUrl(url
+downloadContainer.setUrl(agentURLModifier.modify(getUrl((HasStorablePath)selectedNode))
 );%0A 

Fix download file feature for hosted assembly