package de.novatecgmbh.restsecspring.api.crawler;

import de.novatecgmbh.restsecspring.logic.crawler.SwaggerCrawler;
import de.novatecgmbh.restsecspring.logic.reporting.attackset.Attackset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping(value = "/crawler/swagger")
public class SwaggerCrawlerController {

    private static Logger logger = LoggerFactory.getLogger(SwaggerCrawlerController.class);

    @CrossOrigin
    @RequestMapping(value = "/upload", method = RequestMethod.POST)//, headers = "content-type=multipart/form-data")
    public String handleFileUpload(@RequestParam ("uploadFile") MultipartFile multipartFile){
        logger.info("File received! Name: " + multipartFile.getOriginalFilename() + " Size: " + multipartFile.getSize());
        String fileName = multipartFile.getOriginalFilename();

        Pattern jsonPattern = Pattern.compile(".*json$");
        Pattern yamlPattern = Pattern.compile(".ya?ml$");
        Matcher jsonMatcher = jsonPattern.matcher(fileName);
        Matcher yamlMatcher = yamlPattern.matcher(fileName);

        if (jsonMatcher.find()) {
            logger.info("Swagger JSON ...");
        } else if (yamlMatcher.find()) {
            logger.info("Swagger YAML ...");
        } else {
            logger.warn("Unknown Filetype.");
        }

        if (!multipartFile.isEmpty()) {
            try {
                byte[] bytes = multipartFile.getBytes();
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("src/main/resources/uploads/"+fileName)));
                bos.write(bytes);
                bos.close();
                SwaggerCrawler swaggerCrawler = new SwaggerCrawler();
                swaggerCrawler.crawlJSON(new File("src/main/resources/uploads/"+fileName));
                logger.info("Sending: " + String.valueOf(swaggerCrawler.getNumberOfEndpoints()));
//                Attackset attackset = Attackset.getInstance();
//                return attackset.getAttackSet().toString();
                return String.valueOf(swaggerCrawler.getNumberOfEndpoints());
            } catch (Exception e) {
                return "Upload failed. " + fileName + " : " + e.getMessage();
            }
        } else {
            return "Upload failed. File is empty.";
        }
    }

}

@@ -114,81 +114,8 @@
 er;%0A
-import de.novatecgmbh.restsecspring.logic.reporting.attackset.Attackset;%0A
 impo
@@ -712,57 +712,8 @@
 OST)
-//, headers = %22content-type=multipart/form-data%22)
 %0A   
@@ -815,244 +815,161 @@
     
-logger.info(%22File received! Name: %22 + multipartFile.getOriginalFilename() + %22 Size: %22 + multipartFile.getSize());%0A        String fileName = multipartFile.getOriginalFilename();%0A%0A        Pattern jsonPattern = Pattern.compile(%22.*json$
+String fileName = multipartFile.getOriginalFilename();%0A        logger.info(%22File received : %22 + fileName + %22 (size: %22 + multipartFile.getSize() + %22)
 %22);%0A
+%0A
     
@@ -980,27 +980,28 @@
 Pattern 
-yamlPattern
+allowedFiles
  = Patte
@@ -1016,77 +1016,40 @@
 le(%22
-.ya?ml$%22);%0A        Matcher jsonMatcher = jsonPattern.matcher(fileName
+(%5B%5E%5C%5Cs%5D+(%5C%5C.(?i)(json%7Cya?ml))$)%22
 );%0A 
@@ -1063,21 +1063,17 @@
 Matcher 
-yamlM
+m
 atcher =
@@ -1077,19 +1077,20 @@
 r = 
-yamlPattern
+allowedFiles
 .mat
@@ -1122,13 +1122,10 @@
 if (
-jsonM
+!m
 atch
@@ -1160,162 +1160,70 @@
 ger.
-info(%22Swagger JSON ...%22);%0A        %7D else if (yamlMatcher.find()) %7B%0A            logger.info(%22Swagger YAML ...%22);%0A        %7D else %7B%0A            logger.warn(%22
+warn(%22Unknown Filetype.%22);%0A            return %22Upload failed. 
 Unkn
@@ -1228,33 +1228,32 @@
 known Filetype.%22
-)
 ;%0A        %7D%0A%0A   
@@ -1678,21 +1678,8 @@
 rawl
-JSON(new File
 (%22sr
@@ -1714,17 +1714,16 @@
 ileName)
-)
 ;%0A      
@@ -1736,30 +1736,27 @@
     
-logger.info(%22Sending: 
+return %22%7B%5C%22found%5C%22:
 %22 + 
@@ -1811,214 +1811,14 @@
 ts()
-));%0A//                Attackset attackset = Attackset.getInstance();%0A//                return attackset.getAttackSet().toString();%0A                return String.valueOf(swaggerCrawler.getNumberOfEndpoints()
+ + %22%7D%22
 );%0A 

support for Swagger yaml / yml added