<commit_before>/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.kaaproject.kaa.sandbox.web.controller;

import org.apache.commons.io.IOUtils;
import org.kaaproject.kaa.examples.common.projects.Project;
import org.kaaproject.kaa.sandbox.web.shared.dto.BuildOutputData;
import org.kaaproject.kaa.sandbox.web.shared.dto.ProjectDataType;
import org.kaaproject.kaa.sandbox.web.shared.services.SandboxService;
import org.kaaproject.kaa.sandbox.web.shared.services.SandboxServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * The Class SandboxController.
 */
@Controller
@RequestMapping("api")
public class SandboxController {

    private static final String DATA_TYPE = "dataType";

    private static final String PROJECT_ID = "projectId";

    /**
     * The Constant logger.
     */
    private static final Logger LOG = LoggerFactory.getLogger(SandboxController.class);

    /**
     * The sandbox service.
     */
    @Autowired
    private SandboxService sandboxService;

    @ExceptionHandler(SandboxServiceException.class)
    public ResponseEntity<String> handleSandboxServiceException(SandboxServiceException ex) {
        ResponseEntity<String> entity = new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        return entity;
    }

    /**
     * Gets all demo projects.
     */
    @RequestMapping(value = "demoProjects", method = RequestMethod.GET)
    @ResponseBody
    public List<Project> getDemoProjects() throws SandboxServiceException {
        return sandboxService.getDemoProjects();
    }

    /**
     * Downloads a project demo sources.
     *
     * @param lang SDK language
     * @param name Demo project name
     * @param response standard response
     * @throws SandboxServiceException if couldn't download a file
     */
    @RequestMapping(value = "demoProjects/download/{name}/{lang}", method = RequestMethod.GET)
    public void downloadProjectSources(@PathVariable String lang, @PathVariable String name, HttpServletResponse response) throws SandboxServiceException {
        String fileName = name + ".tar.gz";
        String fullPathName = "/usr/lib/kaa-sandbox/demo_projects/" + lang + "/" + fileName;
        LOG.info("Downloading file [{}]", fullPathName);
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        try {
            IOUtils.copy(Files.newInputStream(Paths.get(fullPathName)), response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            LOG.error("Couldn't download file [{}]", fullPathName);
            throw new SandboxServiceException(e);
        }
    }

    /**
     * Check if project data is cached.
     */
    @RequestMapping(value = "isProjectDataExists", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkProjectDataExists(
            @RequestParam(value = PROJECT_ID) String projectId,
            @RequestParam(value = DATA_TYPE) String dataType) throws SandboxServiceException {
        return sandboxService.checkProjectDataExists(projectId, ProjectDataType.valueOf(dataType.toUpperCase()));
    }

    /**
     * Check if project data is cached.
     */
    @RequestMapping(value = "buildProjectData", method = RequestMethod.POST)
    @ResponseBody
    public byte[] buildProjectData(
            @RequestParam(value = PROJECT_ID) String projectId,
            @RequestParam(value = DATA_TYPE) String dataType) throws SandboxServiceException {
        BuildOutputData buildOutputData = new BuildOutputData();
        try {<commit_msg>Added endpoint for downloading SDK<commit_after>/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.kaaproject.kaa.sandbox.web.controller;

import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.DirectoryScanner;
import org.kaaproject.kaa.examples.common.projects.Project;
import org.kaaproject.kaa.sandbox.web.shared.dto.BuildOutputData;
import org.kaaproject.kaa.sandbox.web.shared.dto.ProjectDataType;
import org.kaaproject.kaa.sandbox.web.shared.services.SandboxService;
import org.kaaproject.kaa.sandbox.web.shared.services.SandboxServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * The Class SandboxController.
 */
@Controller
@RequestMapping("api")
public class SandboxController {

    private static final String DATA_TYPE = "dataType";

    private static final String PROJECT_ID = "projectId";

    /**
     * The Constant logger.
     */
    private static final Logger LOG = LoggerFactory.getLogger(SandboxController.class);

    /**
     * The sandbox service.
     */
    @Autowired
    private SandboxService sandboxService;

    @ExceptionHandler(SandboxServiceException.class)
    public ResponseEntity<String> handleSandboxServiceException(SandboxServiceException ex) {
        ResponseEntity<String> entity = new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        return entity;
    }

    /**
     * Gets all demo projects.
     */
    @RequestMapping(value = "demoProjects", method = RequestMethod.GET)
    @ResponseBody
    public List<Project> getDemoProjects() throws SandboxServiceException {
        return sandboxService.getDemoProjects();
    }

    /**
     * Downloads a project demo sources.
     *
     * @param lang     SDK language
     * @param name     Demo project name
     * @param response standard response
     * @throws SandboxServiceException if couldn't download a file
     */
    @RequestMapping(value = "demoProjects/download/{name}/{lang}", method = RequestMethod.GET)
    public void downloadProjectSources(@PathVariable String lang, @PathVariable String name, HttpServletResponse response) throws SandboxServiceException {
        String fileName = name + ".tar.gz";
        String fullPathName = "/usr/lib/kaa-sandbox/demo_projects/" + lang + "/" + fileName;
        LOG.info("Downloading file [{}]", fullPathName);
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        try {
            IOUtils.copy(Files.newInputStream(Paths.get(fullPathName)), response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            LOG.error("Couldn't download file [{}]", fullPathName);
            throw new SandboxServiceException(e);
        }
    }

    /**
     * Downloads an SDK for needed language.
     *
     * @param lang     SDK language
     * @param response standard response
     * @throws SandboxServiceException if couldn't download
     */
    @RequestMapping(value = "demoProjects/download/sdk/{lang}", method = RequestMethod.GET)
    public void downloadProjectSDK(@PathVariable String lang, HttpServletResponse response) throws SandboxServiceException {
        String sdkPath = "/usr/lib/kaa-node/sdk/" + lang + "/";
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(new String[]{"kaa-*.jar"});
        scanner.setBasedir(sdkPath);
        scanner.setCaseSensitive(false);
        try {
            scanner.scan();
            String[] files = scanner.getIncludedFiles();
            if (files.length == 0) {
                LOG.warn("No SDK found for language [{}]", lang);
                return;
            }
            String sdkFullName = sdkPath + files[0];
            LOG.info("Downloading SDK [{}]", sdkFullName);
            response.setHeader("Content-disposition", "attachment; filename=" + files[0]);
            IOUtils.copy(Files.newInputStream(Paths.get(sdkFullName)), response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            LOG.error("Couldn't download SDK for language [{}]", lang);
            throw new SandboxServiceException(e);
        }
    }

    /**
     * Check if project data is cached.
     */
    @RequestMapping(value = "isProjectDataExists", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkProjectDataExists(
            @RequestParam(value = PROJECT_ID) String projectId,
            @RequestParam(value = DATA_TYPE) String dataType) throws SandboxServiceException {
        return sandboxService.checkProjectDataExists(projectId, ProjectDataType.valueOf(dataType.toUpperCase()));
    }

    /**
     * Check if project data is cached.
     */
    @RequestMapping(value = "buildProjectData", method = RequestMethod.POST)
    @ResponseBody
    public byte[] buildProjectData(
            @RequestParam(value = PROJECT_ID) String projectId,
            @RequestParam(value = DATA_TYPE) String dataType) throws SandboxServiceException {
        BuildOutputData buildOutputData = new BuildOutputData();
        try {
{'fraction_non_alphanumeric': 0.10090361445783133, 'fraction_numerical': 0.002917921686746988, 'mean_word_length': 3.399585921325052, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1223163', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2855, 'n_words': 715}