<commit_before>package org.safehaus.subutai.rest.template.manager;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.safehaus.subutai.api.agentmanager.AgentManager;
import org.safehaus.subutai.api.aptrepositorymanager.AptRepoException;
import org.safehaus.subutai.api.aptrepositorymanager.AptRepositoryManager;
import org.safehaus.subutai.api.template.manager.TemplateManager;
import org.safehaus.subutai.api.templateregistry.TemplateRegistryManager;
import org.safehaus.subutai.shared.protocol.Agent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestServiceImpl implements RestService {

    private static final Logger logger = LoggerFactory.getLogger(RestServiceImpl.class);
    private String managementHostName = "management";
    TemplateManager templateManager;
    TemplateRegistryManager templateRegistry;
    AptRepositoryManager aptRepoManager;
    AgentManager agentManager;

    public void setTemplateManager(TemplateManager templateManager) {
        this.templateManager = templateManager;
    }

    public void setTemplateRegistry(TemplateRegistryManager templateRegistry) {
        this.templateRegistry = templateRegistry;
    }

    public void setAptRepoManager(AptRepositoryManager aptRepoManager) {
        this.aptRepoManager = aptRepoManager;
    }

    public void setAgentManager(AgentManager agentManager) {
        this.agentManager = agentManager;
    }

    @Override
    public String getManagementHostName() {
        return managementHostName;
    }

    @Override
    public void setManagementHostName(String managementHostName) {
        this.managementHostName = managementHostName;
    }

    @Override
    public Response importTemplate(InputStream in, String configDir) {
        Path path;
        try {
            path = Files.createTempFile("subutai-template-", ".deb");
            try(OutputStream os = new FileOutputStream(path.toFile())) {
                int len;
                byte[] buf = new byte[1024];
                while((len = in.read(buf)) > 0)
                    os.write(buf, 0, len);
                os.flush();
            }
            logger.info("Payload saved to " + path.toString());
        } catch(IOException ex) {
            String m = "Failed to write payload data to file";
            logger.error(m, ex);
            return Response.serverError().build();
        }

        Agent mgmt = agentManager.getAgentByHostname(managementHostName);
        try {
            //aptRepoManager.addPackageByPath(mgmt, path.toString(), false);

            Path p = Paths.get(configDir, "config");
            List<String> conf = aptRepoManager.readFileContents(mgmt,
                    path.toString(), Arrays.asList(p.toString()));

            p = Paths.get(configDir, "packages");
            List<String> pack = aptRepoManager.readFileContents(mgmt,
                    path.toString(), Arrays.asList(p.toString()));

            templateRegistry.registerTemplate(mergeLines(conf), mergeLines(pack));

        } catch(AptRepoException ex) {
            String m = "Failed to process deb package";
            logger.error(m, ex);
            return Response.serverError().build();
        } catch(Exception ex) {
            String m = "Import of package failed";
            logger.error(m, ex);
            return Response.serverError().build();
        } finally {
            // clean up
            path.toFile().delete();
        }
        logger.info("Template package successfully imported.");
        return Response.ok().build();
    }

    @Override
    public Response exportTemplate(String templateName) {
        // TODO: we need to be able to export in specified physical sever
        // currently this method calls export script on management server
        String path = templateManager.exportTemplate(managementHostName, templateName);
        if(path == null)
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

        String cd = "attachment; filename=\"" + Paths.get(path).getFileName() + "\"";
        return Response.ok(new File(path)).
                header("Content-Disposition", cd).
                type(MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
    }

    private String mergeLines(List<String> lines) {
        StringBuilder sb = new StringBuilder();
        for(String s : lines) sb.append(s).append(System.lineSeparator());
        return sb.toString();
    }

}
<commit_msg>SUBUTAI-993: uncomment package upload to apt repo<commit_after>package org.safehaus.subutai.rest.template.manager;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.safehaus.subutai.api.agentmanager.AgentManager;
import org.safehaus.subutai.api.aptrepositorymanager.AptRepoException;
import org.safehaus.subutai.api.aptrepositorymanager.AptRepositoryManager;
import org.safehaus.subutai.api.template.manager.TemplateManager;
import org.safehaus.subutai.api.templateregistry.TemplateRegistryManager;
import org.safehaus.subutai.shared.protocol.Agent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestServiceImpl implements RestService {

    private static final Logger logger = LoggerFactory.getLogger(RestServiceImpl.class);
    private String managementHostName = "management";
    TemplateManager templateManager;
    TemplateRegistryManager templateRegistry;
    AptRepositoryManager aptRepoManager;
    AgentManager agentManager;

    public void setTemplateManager(TemplateManager templateManager) {
        this.templateManager = templateManager;
    }

    public void setTemplateRegistry(TemplateRegistryManager templateRegistry) {
        this.templateRegistry = templateRegistry;
    }

    public void setAptRepoManager(AptRepositoryManager aptRepoManager) {
        this.aptRepoManager = aptRepoManager;
    }

    public void setAgentManager(AgentManager agentManager) {
        this.agentManager = agentManager;
    }

    @Override
    public String getManagementHostName() {
        return managementHostName;
    }

    @Override
    public void setManagementHostName(String managementHostName) {
        this.managementHostName = managementHostName;
    }

    @Override
    public Response importTemplate(InputStream in, String configDir) {
        Path path;
        try {
            path = Files.createTempFile("subutai-template-", ".deb");
            try(OutputStream os = new FileOutputStream(path.toFile())) {
                int len;
                byte[] buf = new byte[1024];
                while((len = in.read(buf)) > 0)
                    os.write(buf, 0, len);
                os.flush();
            }
            logger.info("Payload saved to " + path.toString());
        } catch(IOException ex) {
            String m = "Failed to write payload data to file";
            logger.error(m, ex);
            return Response.serverError().build();
        }

        Agent mgmt = agentManager.getAgentByHostname(managementHostName);
        try {
            aptRepoManager.addPackageByPath(mgmt, path.toString(), false);

            Path p = Paths.get(configDir, "config");
            List<String> conf = aptRepoManager.readFileContents(mgmt,
                    path.toString(), Arrays.asList(p.toString()));

            p = Paths.get(configDir, "packages");
            List<String> pack = aptRepoManager.readFileContents(mgmt,
                    path.toString(), Arrays.asList(p.toString()));

            templateRegistry.registerTemplate(mergeLines(conf), mergeLines(pack));

        } catch(AptRepoException ex) {
            String m = "Failed to process deb package";
            logger.error(m, ex);
            return Response.serverError().build();
        } catch(Exception ex) {
            String m = "Import of package failed";
            logger.error(m, ex);
            return Response.serverError().build();
        } finally {
            // clean up
            path.toFile().delete();
        }
        logger.info("Template package successfully imported.");
        return Response.ok().build();
    }

    @Override
    public Response exportTemplate(String templateName) {
        // TODO: we need to be able to export in specified physical sever
        // currently this method calls export script on management server
        String path = templateManager.exportTemplate(managementHostName, templateName);
        if(path == null)
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

        String cd = "attachment; filename=\"" + Paths.get(path).getFileName() + "\"";
        return Response.ok(new File(path)).
                header("Content-Disposition", cd).
                type(MediaType.APPLICATION_OCTET_STREAM_TYPE).build();
    }

    private String mergeLines(List<String> lines) {
        StringBuilder sb = new StringBuilder();
        for(String s : lines) sb.append(s).append(System.lineSeparator());
        return sb.toString();
    }

}

{'fraction_non_alphanumeric': 0.10114052076608565, 'fraction_numerical': 0.002044329675059178, 'mean_word_length': 2.760113268608414, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6993820', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2401, 'n_words': 532}