package uk.me.thega.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import uk.me.thega.controller.exception.NotFoundException;

import java.util.Iterator;

@Controller
@RequestMapping(UrlMappings.ROOT_UPLOAD)
public class UploadController extends AbstractController {

    @RequestMapping(method = RequestMethod.GET)
    public String uploadGet(final ModelMap model) throws NotFoundException {
        populateGet(model);
        model.addAttribute("families", getRepository().families());
        return "upload";
    }

    @RequestMapping(value = "/{family}/{application}/{version}/new", method = RequestMethod.POST)
    public @ResponseBody String uploadPost(@PathVariable final String family, @PathVariable final String application, @PathVariable final String version, final MultipartHttpServletRequest request) {
        Iterator<String> itr =  request.getFileNames();

        while (itr.hasNext()) {
            MultipartFile mpf = request.getFile(itr.next());
            System.out.println(mpf.getOriginalFilename() + " uploaded to " + family + "->" + application + "->" + version);
        }

        return "{}";
    }

}

@@ -26,16 +26,73 @@
 oller;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -606,16 +606,135 @@
 ption;%0A%0A
+import java.io.BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0A
 import j
@@ -866,16 +866,130 @@
 ller %7B%0A%0A
+    /** The logger. */%0A    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);%0A%0A
     @Req
@@ -1335,23 +1335,16 @@
 ST)%0A    
-public 
 @Respons
@@ -1348,16 +1348,27 @@
 onseBody
+%0A    public
  String 
@@ -1531,16 +1531,47 @@
 request)
+%0A            throws IOException
  %7B%0A     
@@ -1573,16 +1573,22 @@
         
+final 
 Iterator
@@ -1601,17 +1601,16 @@
 g%3E itr =
- 
  request
@@ -1736,26 +1736,541 @@
     
-System.out.println
+createParentFolder(family, application, version);%0A%0A            // Create the file on server%0A            final String serverPath = getPathHelper().getResourcePath(family, application, version, mpf.getOriginalFilename());%0A%0A            final File serverFile = new File(serverPath);%0A            final BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(serverFile));%0A%0A            outputStream.write(mpf.getBytes());%0A            outputStream.flush();%0A            outputStream.close();%0A            logger.info
 (mpf
@@ -2318,26 +2318,28 @@
 + family + %22
+ 
 -%3E
+ 
 %22 + applicat
@@ -2345,18 +2345,20 @@
 tion + %22
+ 
 -%3E
+ 
 %22 + vers
@@ -2402,10 +2402,286 @@
 %0A    %7D%0A%0A
+    private void createParentFolder(final String family, final String application, final String version) %7B%0A        final File dir = new File(getPathHelper().getVersionPath(family, application, version));%0A        if (!dir.exists()) %7B%0A            dir.mkdirs();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add backend for file upload